Working with ArrayList in Java ( Adding,Removing elements)

By Saruque Ahamed Mollick

Working with ArrayList in Java is very useful, But we have to know how to add elements, remove elements and update or replace elements of an ArrayList so that we can work as per our desire with Java ArrayList.



In this post, we are going to learn how to add elements to Java ArrayList as well as how to remove elements from an ArrayList.

Finding an ArrayList in Java is Empty or Not

Create an ArrayList in Java first:

The below code will create an ArrayList

import java.util.ArrayList;
public class Arraylistproblems {
    public static void main(String args[]){
        ArrayList<String> list=new ArrayList<String>();
        }
    
}

Adding Elements in ArrayList

The below Java code will add elements to our ArrayList

import java.util.ArrayList;
public class Arraylistproblems {
    public static void main(String args[]){
        ArrayList<String> list=new ArrayList<String>();
        list.add("CodeSpeedy");
        list.add("ArrayList");
        list.add("Java");
        
        
    }
    
}

Retrieve Elements from ArrayList

The below code will retrieve elements from our ArrayList

import java.util.ArrayList;
public class Arraylistproblems {
    public static void main(String args[]){
        ArrayList<String> list=new ArrayList<String>();
        list.add("CodeSpeedy");
        list.add("ArrayList");
        list.add("Java");
        for(String x: list){
            System.out.println(x);
        }
                
    }
    
}

Output:

run:
CodeSpeedy
ArrayList
Java
BUILD SUCCESSFUL (total time: 0 seconds)

The above code might be difficult to understand for the beginners as we used a loop to print the elements.
Instead of that, we may also use the below code. It will be easier to understand.

import java.util.ArrayList;
public class Arraylistproblems {
    public static void main(String args[]){
        ArrayList<String> list=new ArrayList<String>();
        list.add("CodeSpeedy");
        list.add("ArrayList");
        list.add("Java");
        System.out.print(list);
        
        
    }
    
}

Output:

run:
[CodeSpeedy, ArrayList, Java]BUILD SUCCESSFUL (total time: 0 seconds)

How to Modify Element or Elements of an ArrayList in Java

Remove or Delete Elements from an ArrayList in Java

The below java code will delete an element from the ArrayList

import java.util.ArrayList;
public class Arraylistproblems {
    public static void main(String args[]){
        ArrayList<String> list=new ArrayList<String>();
        list.add("CodeSpeedy");
        list.add("ArrayList");
        list.add("Java");
        System.out.println(list);
        list.remove(2);
        System.out.println("after removing:");
        System.out.println(list);
        
    }
    
}

Output:

run:
[CodeSpeedy, ArrayList, Java]
after removing:
[CodeSpeedy, ArrayList]
BUILD SUCCESSFUL (total time: 0 seconds)

list.remove(index) —  we put 2 as our index so the element on index 2 got removed by this method.
How to Increase and Decrease Current Capacity (Size) of ArrayList in Java

 

Remove all the elements from an ArrayList in Java:

The below code will clear all the elements from an ArrayList

import java.util.ArrayList;
public class Arraylistproblems {
    public static void main(String args[]){
        ArrayList<String> list=new ArrayList<String>();
        list.add("CodeSpeedy");
        list.add("ArrayList");
        list.add("Java");
        System.out.println(list);
        list.clear();
        System.out.println("after Clearing:");
        System.out.println(list);
        
    }
    
}

Output:

run:
[CodeSpeedy, ArrayList, Java]
after Clearing:
[]
BUILD SUCCESSFUL (total time: 0 seconds)

How To Convert An ArrayList to Array In Java

In this post, we used String type ArrayList. But you can choose Integer too if you need to work with Integer elements.

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

You just have to write Integer instead of String just like below

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

Leave a Reply

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