ArrayList Class in Collection Framework

java.util.ArrayList;
public class ArrayList extends AbstractList implements List, RandomAccess, Cloneable, Serializable
ArrayList class provides the implementation of List interface through AbstractList class. An object of type ArrayList represents a dynamic array.
ArrayList class features:
  1. ArrayList is used for Dynamic and Resizable-array implementation of the List interface.
  2. It is used to store an “insertion ordered” group of elements.
  3. It can contain duplicate elements.
  4. Implements all optional list operations, and permits all elements, including null.
  5. It is unsynchronized.
  6. The size, isEmpty, get, set, iterator, and listIterator operations run in constant time.
  7. ArrayList’s give great performance on get() and set() methods, but do not perform well on add() and remove() methods when compared to a LinkedList.
  8. An ArrayList capacity is the size of the array used to store the elements in the list.
  9. As elements are added to an ArrayList, its capacity grows automatically.
  10. It is an Array based implementation where elements of the List can be accessed directly through get() method.
To prevent unsynchronized access to the list:
List list = Collections.synchronizedList(new ArrayList(…));
Constructor of ArrayList
  1. ArrayList()
    It is used to create  an empty list with an initial capacity of ten.
  2. ArrayList(Collection c)
              it is used to create a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
  3. ArrayList(int initialCapacity)
             it is used to create an empty list with the specified initial capacity.
Method of ArrayList class
  1. void add(int index, Object element)
             It is used to insert the specified element at the specified position in this list (optional operation).
  2. boolean add(Object o)
             It is used to append the specified element to the end of this list (optional operation).
  3. boolean addAll(Collection c)
              It is used to append 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 (optional operation).
  4. boolean addAll(int index, Collection c)
               It is used to insert all of the elements in the specified collection into this list at the specified position (optional operation).
  5. void clear()
              Removes all of the elements from this list (optional operation).
  6. boolean contains(Object o)
              Returns true if this list contains the specified element.
  7. boolean containsAll(Collection c)
              Returns true if this list contains all of the elements of the specified collection.
  8. boolean equals(Object o)
              Compares the specified object with this list for equality.

  9. Object get(int index)
              Returns the element at the specified position in this list.
  10. int hashCode()
              Returns the hash code value for this list.
  11. int indexOf(Object o)
              Returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element.
  12. boolean isEmpty()
              Returns true if this list contains no elements.
  13. Iterator iterator()
              Returns an iterator over the elements in this list in proper sequence.
  14. int lastIndexOf(Object o)
              Returns the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element.
  15. ListIterator listIterator()
              Returns a list iterator of the elements in this list (in proper sequence).
  16. ListIterator listIterator (int index)
              Returns a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list.
  17. Object remove(int index)
              Removes the element at the specified position in this list (optional operation).
  18. boolean remove(Object o)
              Removes the first occurrence in this list of the specified element (optional operation).
  19. boolean removeAll(Collection c)
              Removes from this list all the elements that are contained in the specified collection (optional operation).
  20. boolean retainAll(Collection c)
              Retains only the elements in this list that are contained in the specified collection (optional operation).
  21. Object set(int index, Object element)
              Replaces the element at the specified position in this list with the specified element (optional operation).
  22. int size()
              Returns the number of elements in this list.
  23. List subList(int fromIndex, int toIndex)
              Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
  24. Object[] toArray()
              Returns an array containing all of the elements in this list in proper sequence.
  25. Object[] toArray(Object[] a)
              Returns an array containing all of the elements in this list in proper sequence; the runtime type of the returned array is that of the specified array.
Example of ArrayList class
					   
import java.util.List; import java.util.ArrayList; import java.util.Iterator; import java.util.ListIterator; public class ArrayListDemo { public static void main(String[] args) { // ArrayList Creation ArrayList al = new ArrayList(); //Adding elements to the ArrayList al.add("Welcome"); al.add("to"); al.add("nsstudent"); al.add("."); al.add("com"); // Find the size of ArrayList element System.out.println("There are "+al.size() +" Element in this list "); //Display ArrayList element using iterator method . Iterator iter = al.iterator(); System.out.println("---Element of ArrayList --- "); while (iter.hasNext()) { System.out.print(iter.next() + " "); } System.out.println(); // Display ArrayList element using size() and get() System.out.println("---Element of ArrayList --- "); for (int i = 0; i< al.size(); i++) { System.out.print(al.get(i) + " , "); } System.out.println(); // Display ArrayList element using Iterator method ListIterator listIter = al.listIterator(); System.out.println("---Element of ArrayList --- "); while (listIter.hasNext()) { System.out.print(listIter.next() + " , "); } System.out.println(); } }
Output

There are 5 Element in this list
---Element of ArrayList ---
Welcome to nsstudent . com
---Element of ArrayList ---
Welcome , to , nsstudent , . , com ,
---Element of ArrayList ---
Welcome , to , nsstudent , . , com ,

Download this example
 



 

Latest Tutorial


Most Visited Tutorial