ArrayList Java

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

In java we have library “java.util”; this library provides class “ArrayList”. Arraylist is of variable data length structure so that it helps in storing elements in an array dynamically.

Also read – sort both primitive and object array

Arraylist size can grow if collection grows and shrinks if object is removed. It cannot be used for primitive data types like char, int,.. etc

Java collection framework provides an architecture to store and manipulate group of objects and it includes interfaces, classes and algorithms.

ArrayList Java

In the above figure, green boxes represent interfaces and yellow boxes represent implementation classes.

Also read – sort array in java

Here’s a java program on arraylist,

import java.io.IOException;
import java.util.*;

public class ArrayListDemo 
{
   public static void main(String[] args) throws IOException 
   {
      // ArrayList size 
      int size = 10;
      // declaring ArrayList with size 
      ArrayList<Integer> al = new ArrayList<Integer>(size);
      // Appending the new element at the end of the list 
      for(int a = 2; a <= size; a++) 
         al.add(a); 

      // print elements 
      System.out.println(al); 

      // remove element at index 3 
      al.remove(3); 

      // printing ArrayList after deleting index 3 
      System.out.println(al); 

      // print elements one by one 
      for(int a = 0; a < al.size(); a++) 
         System.out.print(al.get(a) + " ");
   }
}

Output:

[2, 3, 4, 5, 6, 7, 8, 9, 10]
[2, 3, 4, 6, 7, 8, 9, 10]
2 3 4 6 7 8 9 10


Hierarchy of arraylist

arraylist java

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

Also read – how to remove elements from arraylist

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

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

Class ArrayList<E>
java.lang.Object
     java.util.AbstractCollection<E>
          java.util.AbstractList<E>
               java.util.ArrayList<E>

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

Subclasses
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.


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.

MethodsDescription
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

Interface
java.util.Collection<E>

Interface
java.util.List<E>

java.util.ArrayList<E>
♦ 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 – iterate array using foreach loop

  • 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 docs

Related Posts