ArrayList Java

Hello everyone!! Welcome to flower brackets blog. Today we are going to learn arraylist java.

Introduction : arraylist java

arraylist java

So here is the hierarchy of arraylist class. Arraylist class extends abstractlist. Abstractlist implements list interface.

Also read – how are arrays stored in memory

And list interface extends collection interface. Collection interface extends iterable interface.

Here whatever methods available in the list interface and collection interface will be supported by the arraylist class.

Java arraylist class declaration


public class ArrayList<E>
       extends AbstractList<E>
             implements List<E>, RandomAccess, Cloneable, Serializable

Class ArrayList<E>


Implemented interfaces

Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess


AttributeList, RoleList, RoleUnresolvedList

Initialize Integer ArrayList Java

ArrayList<Integer> al = new ArrayList<Integer>();

Initialize String ArrayList Java

ArrayList<String> strlist=new ArrayList<String>();

ArrayList class inherits methods from classes below,

• java.util.AbstractList
• java.lang.AbstractCollection
• java.util.Object
• java.util.List

The class arraylist belongs to is java.util package. As you can see above letter E is nothing but type.

It can be string, integer, employee object etc., and some of the implemented interfaces are as shown above.

Moving on some of the known subclasses, you can see above.

What is arraylist?

Arraylist is an ordered collection. It contains group of objects, often referred to as elements.

arraylist class java

Above you can see above. There are four objects. In 0th index there is object “Obj 1”, in first index there is object “Obj 2” and so on.

Based on the index position we can access the object. Arraylist implements all optional list operations and allows duplicate elements.

Also we can insert null elements and arraylist maintains insertion order. Arraylist has a variable length that changes dynamically.

Also read – sort HashMap by value

It automatically resizes itself to accommodate the amount of data you have. You don’t even need to specify the size of an arraylist when you create it.

Because it can use a default size that will adjust as the amount of the data changes.

Unlike arrays, whose size is hard to change these arraylists are dynamically created and used. You can increase or decrease or clear arraylist.

Arraylist can support objects and can support generic data types but they cannot support primitive data types.

Performance wise arraylists uses an array as its underlying data structure the performance of arrays and arraylist is almost identical.

They are both extremely fast and they can both insert and retrieve data in constant time, that is, big O of 1, O(1).

Also read – array java

So performance is really a factor in deciding which one to use. But that said, if you have a lot of data in your application and super performance in 10, then arrays are slightly faster.

Now in arraylist the resize operation which happens behind the scenes you don’t really see that or another that’s happening.

It will decrease the performance very slightly because it involves copying all the data from the old array to the new one when it outgrows an old array.

Here’s an example on arraylist,

import java.util.*;

public class ArrayListDemo
   public static void main(String[] args)
      // arraylist of string elements
      ArrayList<String> al = new ArrayList<String>();
      // adding elements to arraylist
      // printing arraylist elements
      System.out.println("--------------ArrayList Class Java Example-------------------");
      System.out.println("Displaying arraylist elements : " + al);
      // now adding elements at given index
      // now removing elements from the arraylist   
      System.out.println("After adding and removing elements : " + al);
      // removing element from the given index
      System.out.println("The final arraylist looks like this : " + al);


arraylist java

Methods supported by arraylist

Arraylists in java implements all the methods available in the list interface. So what all methods supported by the list will be supported by the arraylist.

boolean add ( Object e )appends the specified element to the end of this list (optional operation).
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 (optional operation).
void add( int index, Object element )inserts the specified element at the specified position in this list (optional operation).
boolean addAll( int index, Collection C )inserts all of the elements in the specified collection into this list at the specified position (optional operation).
void trimToSize()this method is used to trim the capacity of the instance of the ArrayList to the list’s current size.
boolean remove( Object o )removes the first occurrence of the specified element from this list, if it is present (optional operation).
boolean removeAll( Collection c )removes from this list all of its elements that are contained in the specified collection (optional operation).
boolean retainAll( Collection c )retains only the elements in this list that are contained the specified collection (optional operation).
Object clone()this method is used to return a shallow copy of an ArrayList.
boolean contains( Object o )returns true if this list contains the specified element.
boolean containsAll( Collection c )returns true if this list contains all of the elements of the specified collection.
E get( int index )returns the element at the specified position in this list
int indexOf( Object o )returns the index of the first occurrence of the specified element in this list, or - 1 if this list does not contain the element.
int lastIndexOf( Object o )returns the index of the last occurrence of the specified element in this list, or - 1 if this list does not contain the element.
void clear( )removes all of the elements from this list (optional operation).
boolean isEmpty( )returns true if this list contains no elements.
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.
Iterator iterator( )returns an iterator over the elements in this list in proper sequence.
ListIterator listIterator( )returns a list iterator over the elements in this list (in proper sequence).
ListIterator listIterator( int index )returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
Object[ ] toArray( )returns an array containing all of the elements in this list in proper sequence (from first to last element).
Object[ ] toArray(Object[ ] O)is also used to return an array containing all of the elements in this list in the correct order same as the previous method.

Arraylist constructors






♦ ArrayList()
♦ ArrayList(c:Collection<? extends E>)
♦ ArrayList(initialCapacity: int)
♦ trimToSize(): void

Arraylist belongs to java.util package. Arraylist implements list interface. So whatever methods available in list interface are implemented by arraylist.

Arraylist has one extra method trimToSize() and it has three constructors they are,

Also read – 2d array java

  • ArrayList() – empty constructor which will create empty list with default initial capacity.
  • ArrayList(c:Collection<? extends E>) – this one is parameterized constructor which accepts collection. It will create the arraylist from an existing collection.
  • ArrayList(initialCapacity: int) – this accepts “int” value. it creates empty lists with the specified initial capacity.

Reference – Oracle help center

Related Posts