The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed.
Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold.
Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.
Example of Java ArrayList class
import java.util.*; class TestCollection1{ public static void main(String args[]){ ArrayList<String> al=new ArrayList<String>();//creating arraylist al.add("Ravi");//adding object in arraylist al.add("Vijay"); al.add("Ravi"); al.add("Ajay"); Iterator itr=al.iterator();//getting Iterator from arraylist to traverse elements while(itr.hasNext()){ System.out.println(itr.next()); } } }
Apart from the methods inherited from its parent classes, ArrayList defines following methods:
1. void add(int index, Object element)
Inserts the specified element at the specified position index in this list. Throws IndexOutOfBoundsException if the specified index is is out of range (index < 0 || index > size()).
2. boolean add(Object o)
Appends the specified element to the end of this list
3. boolean addAll(Collection c)
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s iterator. Throws NullPointerException if the specified collection is null.
4. boolean addAll(int index, Collection c)
Inserts all of the elements in the specified collection into this list, starting at the specified position. Throws NullPointerException if the specified collection is null
5. void clear()
Removes all of the elements from this list.
6 .Object clone()
Returns a shallow copy of this ArrayList.
7 .boolean contains(Object o)
Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).
8. void ensureCapacity(int minCapacity)
Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
9 . Object get(int index)
Returns the element at the specified position in this list. Throws IndexOutOfBoundsException if the specified index is is out of range (index < 0 || index >= size()).
10. int indexOf(Object o)
Returns the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element.
11 . int lastIndexOf(Object o)
Returns the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element.
Two ways to iterate the elements of collection in java
- By Iterator interface.
- By for-each loop.
In the above example, we have seen traversing ArrayList by Iterator. Let’s see the example to traverse ArrayList elements using for-each loop.
Iterating the elements of Collection by for-each loop
import java.util.*; class TestCollection2{ public static void main(String args[]){ ArrayList<String> al=new ArrayList<String>(); al.add("Ravi"); al.add("Vijay"); al.add("Ravi"); al.add("Ajay"); for(String obj:al) System.out.println(obj); } } Output: Ravi Vijay Ravi Ajay
User-defined class objects in Java ArrayList
class Student{ int rollno; String name; int age; Student(int rollno,String name,int age){ this.rollno=rollno; this.name=name; this.age=age; } }
import java.util.*; public class TestCollection3{ public static void main(String args[]){ //Creating user-defined class objects Student s1=new Student(101,"Sonoo",23); Student s2=new Student(102,"Ravi",21); Student s2=new Student(103,"Hanumat",25); ArrayList<Student> al=new ArrayList<Student>();//creating arraylist al.add(s1);//adding Student class object al.add(s2); al.add(s3); Iterator itr=al.iterator(); //traversing elements of ArrayList object while(itr.hasNext()){ Student st=(Student)itr.next(); System.out.println(st.rollno+" "+st.name+" "+st.age); } } } Test it Now 101 Sonoo 23 102 Ravi 21 103 Hanumat 25