ArrayList in Java

Waqar Ali
Waqar Ali
Nov 5 · 5 min read

ArrayList:

Arraylist class implements List interface and it is based on an Array data structure. It is widely used because of the functionality and flexibility it offers. Most of the developers choose Arraylist over Array as it’s a very good alternative of traditional java arrays. ArrayList is a resizable-array implementation of the List interface. It implements all optional list operations, and permits all elements, including null.

Why ArrayList is better than Array?

The limitation with array is that it has a fixed length so if it is full you cannot add any more elements to it, likewise if there are number of elements gets removed from it the memory consumption would be the same as it doesn’t shrink.

On the other ArrayList can dynamically grow and shrink after addition and removal of elements (See the images below). Apart from these benefits ArrayList class enables us to use predefined methods of it which makes our task easy. Let’s see the diagrams to understand the addition and removal of elements from ArrayList and then we will see the programs.

Adding Element in ArrayList at specified position:

Removing Element from ArrayList:

ArrayList class declaration:

Let’s see the declaration for java.util.ArrayList class.

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

How to create an ArrayList?

We can create an ArrayList by writing a simple statement like this:

This statement creates an ArrayList with the name alist with type “String”. The type determines which type of elements the list will have. Since this list is of “String” type, the elements that are going to be added to this list will be of type “String”.

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

Similarly we can create ArrayList that accepts int elements.

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

The important points about Java ArrayList class are:

  • Java ArrayList class can contain duplicate elements.
  • Java ArrayList class can contain Heterogenous Objects.
  • Java ArrayList class maintains insertion order.
  • Java ArrayList class is non synchronized.
  • Java ArrayList allows random access because array works at the index basis.
  • In Java ArrayList class, manipulation is slow because a lot of shifting needs to occur if any element is removed from the array list.

Note: You cannot use primitive types in ArrayList, you have to use Wrapper Classes.

Constructors of Java ArrayList:

  1. ArrayList() : It is used to build an empty array list.
  2. ArrayList(Collection<? extends E> c) : It is used to build an array list that is initialized with the elements of the collection c.
  3. ArrayList(int capacity) : It is used to build an array list that has the specified initial capacity.

Methods of Java ArrayList:

Some of the methods of ArrayList are describes below:

  • void add(E element) : It is used to insert the sp
  • void add(int index, E element) : It is used to insert the specified element at the specified position in a list.
  • E get(int index) : It is used to fetch the element from the particular position of the list.
  • void clear() : It is used to remove all of the elements from this list.
  • boolean isEmpty() : It returns true if the list is empty, otherwise false.
  • int indexOf(Object o) : It is used to return the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element.
  • int lastIndexOf(Object o) : It is used to return the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element.

ArrayList Documentation:

To see the complete documentation of ArrayList including constructors and all of its methods visit the link below:

https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

How to add elements to an ArrayList?

We add elements to an ArrayList by using add() method, this method has couple of variations, which we can use based on the requirement. For example: If we want to add the element at the end of the List then simply do it like this:

alist.add("Steve"); //This will add "Steve" at the end of List

To add the element at the specified location in ArrayList, we can specify the index in the add method like this:

alist.add(3, "Steve"); //This will add "Steve" at the fourth position

Lets write the complete code:

import java.util.*;  
class JavaExample{
public static void main(String args[]){
ArrayList<String> alist=new ArrayList<String>();
alist.add("Steve");
alist.add("Tim");
alist.add("Lucy");
alist.add("Pat");
alist.add("Angela");
alist.add("Tom");

//displaying elements
System.out.println(alist);

//Adding "Steve" at the fourth position
alist.add(3, "Steve");

//displaying elements
System.out.println(alist);
}
}

Output:

[Steve, Tim, Lucy, Pat, Angela, Tom]
[Steve, Tim, Lucy, Steve, Pat, Angela, Tom]

Note: Since the index starts with 0, index 3 would represent fourth position not 3.

How to remove elements from ArrayList?

We use remove() method to remove elements from an ArrayList, Same as add() method, this method also has few variations.

For example:

import java.util.*;
class JavaExample{
public static void main(String args[]){
ArrayList<String> alist=new ArrayList<String>();
alist.add("Steve");
alist.add("Tim");
alist.add("Lucy");
alist.add("Pat");
alist.add("Angela");
alist.add("Tom");
//displaying elements
System.out.println(alist);
//Removing "Steve" and "Angela"
alist.remove("Steve");
alist.remove("Angela");
//displaying elements
System.out.println(alist);
//Removing 3rd element
alist.remove(2);
//displaying elements
System.out.println(alist);
}
}

Output:

[Steve, Tim, Lucy, Pat, Angela, Tom]
[Tim, Lucy, Pat, Tom]
[Tim, Lucy, Tom]

Example:

  1. import java.util.ArrayList;
  2. class ArrayList1{
  3. public static void main(String args[]){
  4. ArrayList<String> list=new ArrayList<String>();//Creating arraylist
  5. list.add(“Waqar”);//Adding object in arraylist
  6. list.add(“Usama”);
  7. list.add(“Uzair”);
  8. list.add(“Imtiaz”);
  9. //Invoking arraylist object
  10. System.out.println(list);
  11. }
  12. }
  13. }
[Waqar, Usama, Uzair, Imtiaz]

Iterating Collection through Iterator interface:

Let’s see an example to traverse ArrayList elements using the Iterator interface.

  1. import java.util.ArrayList;
  2. class ArrayList2{
  3. public static void main(String args[]){
  4. ArrayList<String> list=new ArrayList<String>();//Creating arraylist
  5. list.add(“Waqar”);//Adding object in arraylist
  6. list.add(“Usama”);
  7. list.add(“Uzair”);
  8. list.add(“Imtiaz”);
  9. //Traversing list through Iterator
  10. Iterator itr=list.iterator();
  11. while(itr.hasNext()){
  12. System.out.println(itr.next());
  13. }
  14. }
  15. }
Waqar
Usama
Uzair
Imtiaz

    Waqar Ali

    Written by

    Waqar Ali

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade