ArrayList Get Extract Elements Java

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


ArrayList Get Extract Elements
Output Screenshot on ArrayList Get Extract Elements

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

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.

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.

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.

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.

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.

ArrayList al3 elements are printed using general for loop.

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. shreyas

    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 Reply

Your email address will not be published. Required fields are marked *