ArrayList Class in Java with Programming Example

ArrayListIn this chapter you will learn:

  1. What is ArrayList class?
  2. Methods of ArrayList class
  3. 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.

Constructors of ArrayList class

ConstructorsDescriptions
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 of ArrayList Class
MethodsDescriptions
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(
Collection<? extends E> c)
Append entire element from one collection to the end of the list. The orders are defined as the iterator returns value.
boolean addAll(
int index, Collection<? extends E> c)
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(
int fromIndex, int toIndex)
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

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 []
_

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.


Leave a Reply

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