ArrayList in Java with Example

The ArrayList in Java is a class which is present in java.util package. The ArrayList is a part of Collection Framework and it extends AbstractList and implements the List interface. Most of the developers use ArrayList over Array because it overcomes the limitation of Arrays.


ArrayList in Java


Why we use ArrayList over Arrays?

In Java, the Arrays are of a fixed length, we cannot grow or shrink the Arrays size once we declared it, which means that you must know in advance how many elements in an array will hold. But sometimes you may not know how large an array you need. To handle this situation, the collection framework defines ArrayList.

ArrayList can dynamically increase or decrease in size. ArrayList is created with an initial size when this size is increased, the collection is automatically enlarged and when objects are removed the array may be shrunk.

Arrays contain the only homogenous type of elements whereas we can store both homogenous as well as heterogeneous elements in ArrayList. Apart from these benefits, ArrayList contains prefined methods which make the task more easy for developers.



Constructors in ArrayList:

ArrayList has three constructors which are shown below:

1. ArrayList():  The first constructor is used to builds an empty ArrayList.

2. ArrayList(Collection c): The second constructor is used to builds an array list that is initialized with the elements of the collection c.

3. ArrayList(int capacity): The third constructor is used to builds an array list that has the specified initial capacity. The capacity is the size of the array that is used to store the elements. The capacity grows dynamically as elements are added to an array list.

Methods of ArrayList class:

ArrayList contains prefined methods which make the task more easy for developers. The few important methods of ArrayList class is discussed below:

1. add(Object obj): This method is used to add an object obj to the  ArrayList.

2. add(int index, Element e): This method is used to insert an element at the specified position in an ArrayList.

3. remove(Object obj): This method is used to remove an element from ArrayList.

4. remove(int index, Element e):  This method is used to remove an element from a given index.

5. protected void removeRange(int fromIndex, int toIndex): This method is used to remove the elements from the list which lies in the given range. 

5. int size(): This method is used to find the size of the ArrayList.

6. void clear(): This method is used to remove all of the elements from the ArrayList.

7. boolean isEmpty(): This method is used to check the list is empty or not if the list is empty it returns true otherwise it returns false.

8. boolean contains(Object obj): This method is used to check whether the given object obj is present in the array list if it's there then it returns true otherwise it returns false.

9. int indexOf(Object obj): This method is used to find out the index position of the element, if the list does not contain this element then this method returns the value -1.

10. set(int index, Element e): This method is used to replace the element at the specified position in this list with the specified element.

11. Object get(int index): This method is used to returns the element at the specified position in the list.


How to Create an ArrayList?

We can create an ArrayList by writing a simple statement which is shown below:

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

This statement creates an array list with the name al which is of type "String". The type determines which type of elements the ArrayList should contain. Since it is of type String, the elements that are going to be added in this list will be of String type.

Java ArrayList example to add elements:

We can add elements to an array list by using two methods add() or add(int index, Element e). If you want to add an element at the end of the list then you can use add() method. If you want to add an element at the specified position in the list then you can use add(int index, Element e).

Let's understand this with a simple example:

import java.util.*;
class Example{
public static void main(String args[]){

//creating an array list
ArrayList<String> al = new ArrayList<String>();

//adding elements to the list
al.add("Amit");
al.add("Rahul");
al.add("Raam");
al.add("Shyam");
al.add("Tom");
al.add("Karan");

//displaying elements 
System.out.pritnln("After adding the above elements the list will be :" +al);

al.add(2, "Prashant");

//displaying elements after adding at a given index.
System.out.println("After adding the element at a given index the list will be: " + al);
}
}

Output:

After adding the above elements the list will be : 
[Amit, Rahul, Raam, Shyam, Tom, Karan] 
After adding the element at a given index the list will be:
[Amit, Rahul, Prashant, Raam, Shyam, Tom, Karan]


Java ArrayList example to remove elements:

We can use remove() method to remove the element from an ArrayList. Let's understand this with an example.

import java.util.*;
class Example{
public static void main(String args[]){

//creating an array list
ArrayList<String> al = new ArrayList<String>();

//adding elements to the list
al.add("Amit");
al.add("Rahul");
al.add("Raam");
al.add("Shyam");
al.add("Tom");
al.add("Karan");

//remove Rahul and Raam
System.out.println("After removing Raam and Rahul the list will be:" +al);

//remove the 2nd element 
al.remove(1);

//displaying elements after removing above element
System.out.println("After removing the 2nd element the list will be: " + al);
}
}
Output:
After removing the Raam and Rahul the list will be : 
[Amit, Shyam, Tom, Karan]
After removing the 2nd element the list will be:
[Amit, Tom, Karan]


Java ArrayList example to remove all the elements available in the list:

We can use the clear() method to remove all the elements from an ArrayList. Let's understand this with an example

import java.util.*;
class Example{
public static void main(String args[]){
//creating an array list
ArrayList<String> al = new ArrayList<String>();

//adding elements to the list
al.add("Amit");
al.add("Rahul");
al.add("Raam");
al.add("Shyam");
al.add("Tom");
al.add("Karan");

//displaying initial elements
System.out.pritnln("After adding above elements the initial list will be:" +al);

al.clear();

//displaying elements after remove all the elements from a list.
System.out.println("After removing all elements the list will be: " + al);
}
}
Output:
After adding the above elements the initial list will be: 
[Amit, Rahul, Raam, Shyam, Tom, Karan]
After removing all the elements  the list will be: [ ]


Java ArrayList example to check list is empty or not:

We can use the isEmpty() method to check list is empty or not. Let's understand this with an example.

import java.util.*;  
 class Example{
  public static void main(String [] args){  

          ArrayList<String> al=new ArrayList<String>();  
          System.out.println("Is ArrayList Empty: "+al.isEmpty());  

          al.add("Amit");    
          al.add("Rahul");    
          al.add("Raam");    

          System.out.println("After Insertion");  
          System.out.println("Is ArrayList Empty: "+al.isEmpty());   
       }
    }
Output:

Is ArrayList Empty: true
After Insertion
Is ArrayList Empty: False

Java ArrayList example to find the length of ArrayList:

We can use the size() method to find the length of an ArrayList. Let's understand this with an example.

import java.util.*;
class Example{
public static void main(String args[]){

//creating an array list
ArrayList<String> al = new ArrayList<String>();

System.out.println("Initially, the size of list will be: " +al.size());

//adding elements to the list
al.add("Amit");
al.add("Rahul");
al.add("Raam");
al.add("Shyam");
al.add("Tom");
al.add("Karan");

//displaying size of an array list
System.out.pritnln("After adding above elements the size of the list will be :" +al.size());
}
}
Output:

Initially, the size of the list will be: 0
After adding the above elements the size of the list will be: 6



You may also like:

Inheritance in Java with Example
Encapsulation in Java OOPs with Example
Object Serialization in Java with Example
MVC Architecture explained with Example
Abstract class in Java with Example


ArrayList in Java with Example ArrayList in Java with Example Reviewed by Prashant Srivastava on February 18, 2019 Rating: 5

No comments:

Powered by Blogger.