In this chapter you will learn:
- What is ArrayList class?
- Methods of ArrayList class
- Programming Example
What is ArrayList class?
ArrayList is such type of storage array which is dynamic in size and can grow and shrink at runtime. It is actually a resizable array. It implements the list interface and has almost same operations as lists but it doesn’t allow null value.
Initialization
You can define ArrayList class as follow. You must include java.util.*;
in your program.
ArrayList alist=new ArrayList();
Constructors of ArrayList class
Constructors | Descriptions |
ArrayList() |
This constructor builds an empty arraylist. |
ArrayList(Collection c) |
This constructor builds an array list that is initialized with the elements of the collection c. |
ArrayList(int capacity) |
This constructor builds an array list that has the specified initial capacity. The capacity is the size of the underlying array that is used to store the elements. The capacity grows automatically as elements are added to an array list. |
Methods | Descriptions |
boolean add(E e) |
Append the element at the end of the list. |
void add(int index, E element) |
Insert the element at the specified position in the list. |
boolean addAll( |
Append entire element from one collection to the end of the list. The orders are defined as the iterator returns value. |
boolean addAll( |
Insert entire elements from one collection to list at the specified postion. |
void clear() |
Clear the list by removing all the items. |
Object clone() |
Make a clone of arraylist. |
boolean contains(Object o) |
returns true if arraylist contains specified items. |
void ensureCapacity(int minCapacity) |
Increase the capacity of arraylist. |
E get(int index) |
Returns the element at the specified index position in the list. |
int indexOf(Object o) |
Returns the index of first occurrence of element in the list. If item not found returns -1. |
boolean isEmpty() |
Returns true if list is empty. |
Iterator iterator() |
Returns an iterator over the elements in this list in proper sequence. |
int lastIndexOf(Object o) |
Returns the index of last occurrence of element in the list. If item not found returns -1. |
ListIterator<E> listIterator() |
Returns a list iterator over the elements in this list (in proper sequence). |
ListIterator<E> listIterator(int index) |
Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. |
E remove(int index) |
Removes the element at the specified position in this list. |
boolean remove(Object o) |
Removes the first occurrence of the specified element from this list, if it is present. |
boolean removeAll(Collection<?> c) |
Removes from this list all of its elements that are contained in the specified collection. |
protected void removeRange( |
Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive. |
boolean retainAll(Collection<?> c) |
Retains only the elements in this list that are contained in the specified collection. |
E set(int index, E element) |
Replaces the element at the specified position in this list with the specified element. |
int size() |
Returns the number of elements in this list. |
List subList(int fromIndex, int toIndex) |
Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. |
Object[] toArray() |
Returns an array containing all of the elements in this list in proper sequence (from first to last element). |
<T> T[] toArray(T[] a) |
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. |
void trimToSize() |
Trims the capacity of this ArrayList instance to be the list’s current size. |
Programming Example
import java.util.*; class Array_List { public static void main(String[] args) { ArrayList alist=new ArrayList(); alist.add("Table"); alist.add("Chair"); alist.add("Speaker"); alist.add("Guitar"); alist.add("Fan"); //Printing arraylist item System.out.print("All Array List Items are "); System.out.println(alist); //Adding new item at index position 2 alist.add(2, "Calculator"); System.out.println("Adding Calculator : "+alist); //Removing item Calculator alist.remove("Calculator"); System.out.println("Removing Calculator : "+alist); //Get element name at specified index position System.out.println("Element at Index Position 2 : " + alist.get(2)); //Get total size of arraylist System.out.println("ArrayList Size are : " + alist.size()); //Clear the arraylist alist.clear(); System.out.println("After clearing arraylist " + alist); } }
Output
All Array List Items are [Table, Chair, Speaker, Guitar, Fan]
Adding Calculator : [Table, Chair, Calculator, Speaker, Guitar, Fan]
Removing Calculator : [Table, Chair, Speaker, Guitar, Fan]
Element at Index Position 2 : Speaker
ArrayList Size are : 5
After clearing arraylist []
_
Adding Calculator : [Table, Chair, Calculator, Speaker, Guitar, Fan]
Removing Calculator : [Table, Chair, Speaker, Guitar, Fan]
Element at Index Position 2 : Speaker
ArrayList Size are : 5
After clearing arraylist []
_
Summary
ArrayList is very useful when you don’t know how many items have to be added in the list. It can be shrink and grow dynamically at runtime. This chapter also explains all the methods or ArrayList class with programming example. It is very easy to learn this keyword in this chapter. The next chapter is explaining one more List item LinkedList.