ArrayList Get Extract Elements Java

Note: Before going through this ArrayList Get Extract Elements Java, it is advised to read first ArrayList Introduction where all the methods, class signature, constructors and history are given.

4 programs are given on ArrayList.

  1. ArrayList Methods: In this program, the basic operations on array list are performed like addition, checking the list is empty or not, removing the elements, knowing the index number of an element etc.
  2. ArrayList Iterators: In this example, different styles of retrieving and printing the array list elements are described.
  3. ArrayList Operations: In this program, operations like reversing, sorting, searching, swapping, shuffling, filling, knowing maximum and minimum values are performed. Finally, an array of array lists is created and the elements are printed.
  4. ArrayList Special: In this program, operations like converting array list to array, array list to string, copying elements of one array into another, converting array to array list and finally cloning of an array list are performed.

This is the second program of the above four – ArrayList Iterators.

Example on ArrayList Get Extract Elements
import java.util.*;
public class ArrayListIterators
{
  public static void main(String args[])
  {   
    ArrayList al1 = new ArrayList();
    al1.add("Jyostna");
    al1.add("Pradeep");
    al1.add("Chandu");
    al1.add("Srinivas");
    al1.add("Murthy");

    System.out.println("Elements before set: " + al1);
    al1.set(1, "Ganga");
    System.out.println("Elements after set: " + al1);

    List al2 = al1.subList(1,3);
    System.out.println("\nElements of sublist al2: " + al2);

    ArrayList al3 = new ArrayList();
    al3.addAll(al2);
    System.out.println("\nElements of al3: " + al3);

                                 // ITERATING WITH ITERATOR
    System.out.print("\nElements with Iterator: ");
    Iterator it1 = al1.iterator();
    while(it1.hasNext())
    {
      System.out.print(it1.next() + " ");
    }
                                 // ITERATING WITH LISTITERATOR
    System.out.print("\nElements with ListIterator: ");
    ListIterator it2 = al1.listIterator(2);
    while(it2.hasNext())
    {
      System.out.print(it2.next() + " ");
    }
                                // ITERATING WITH BASIC FOR LOOP
    System.out.print("\n\nElements with basic for loop: ");
    for(int i = 0; i < al3.size(); i++)
    {
      System.out.print(al3.get(i) + " ");
    }
                               // ITERATING WITH ENHANCED FOR LOOP
    System.out.print("\nElements with foreach loop: ");
    for(String str : al3)
    {
      System.out.print(str + " ");
    }
  }
}


ArrayList Get Extract Elements
Output Screenshot on ArrayList Get Extract Elements

       ArrayList al1 = new ArrayList();
       al1.add("Jyostna");

A generics ArrayList object al1 is created that stores only strings. With add() method a few strings are added.

       al1.set(1, "Ganga");

The set() method replaces the original string value "Pradeep" with "Ganga". "Pradeep" exists at index number 1. Remember, the elements added are given index numbers by default, first one being 0.

        List al2 = al1.subList(1,3);

Using subList() method, inherited from List interface, a few elements from the list can be extracted. Now the list al2 contains the elements 1 and 2 ( 3-1) of al1. The subList() method returns List and not ArrayList.

       ArrayList al3 = new ArrayList();
       al3.addAll(al2);

Another generics array list al3 is created and added with all the elements of al2. addAll() method inherited from Collection interface (and overridden by List interface) adds all the elements at a single stretch.

       Iterator it1 = al1.iterator();
       while(it1.hasNext())
       {
         System.out.print(it1.next() + " ");
       }

itertator() method, inherited from Collection, returns an object of Iterator. it1 contains all the elements of al1. With hasNext() and next() methods, all the elements are printed.

       ListIterator it2 = al1.listIterator(2);
       while(it2.hasNext())
       {
         System.out.print(it2.next() + " ");
       }

listItertator() method, inherited from List, returns an object of ListIterator. Observe the parameter 2. it2 contains all the elements of al1 starting from index number 2. With hasNext() and next() methods, all the elements are printed.

       for(int i = 0; i < al3.size(); i++)
       {
         System.out.print(al3.get(i) + " ");
       }

ArrayList al3 elements are printed using general for loop.

       for(String str : al3)
       {
         System.out.print(str + " ");
       }

The above code prints the elements using enhanced for loop (popularly known as foreach) introduced with J2SE 5.

All Iterators in a Nutshell

Enumeration Iterator
ListIterator Arrays foreach
Collections foreach Enumeration vs Iterator
Iterator vs ListIterator

General Exceptions raised by collections classes

IllegalStateException ConcurrentModificationException
UnsupportedOperationException NullPointerException
NoSuchElementException

2 thoughts on “ArrayList Get Extract Elements Java”

  1. Sir, in ArrayListIterator program in line no. 46, u cannot declare for(String str : a13),,,,,,you have to declare as for(Object str : a13)…..because in collections everything will be treated as object know sir

Leave a Comment

Your email address will not be published.