Disclosure: This article may contain affiliate links. When you purchase, we may earn a commission.

10 Examples of ArrayList in Java for Beginners

ArrayList Example in Java
In this Java ArrayList Example, we will see how to add elements in ArrayList, how to remove elements from ArrayList, ArrayList contains Example, and several other ArrayList functions which we use daily. ArrayList is one of the most popular classes from the Java Collection framework along with HashSet and HashMap and a good understanding of the ArrayList class and methods is imperative for Java developers. ArrayList is an implementation of List Collection which is ordered and allows duplicates

ArrayList is also index-based and provides constant-time performance for common methods e.g. get(). Apart from being very popular among Java programmers, ArrayList is also a very popular interview topic.

Questions like the difference between Vector and ArrayList and LinkedListvs ArrayList is hugely popular in various Java interviews, especially with 2 to 3 years of experience. Along with Vector, this is one of the first collection classes many Java programmers use. 

By the way, we have already seen some ArrayList tutorials e.g. ArrayList sorting example,  converting Array to ArrayList,  looping through ArrayList which is good to understand ArrayList in Java.





Java ArrayList Examples

In this section, we will see actual code examples of various ArrayList functionality like add, remove, contains, clear, size, isEmpty, etc. You can use this Java program to play with ArrayList yourself. For example, you can run this Java program in your favorite IDE like IntelliJIDEA, NetBeans, Eclipse, or even on any online Java Editor. 

10 Examples of ArrayList in Java


And, here is our complete Java program which demonstrates how to work with ArrayList. 


  
import java.util.ArrayList;
import java.util.Arrays;

/**
 *
 * Java ArrayList Examples - list of frequently used examples in ArrayList e.g. adding
 * elements, removing elements, contains examples, etc
 * @author
 */

public class ArrayListTest {

    public static void main(String args[]) {
     
        //How to create ArrayList in Java - example
        ArrayList<String> list = new ArrayList<String>();
     
        //Java ArrayList add Examples
        list.add("Apple");
        list.add("Google");
        list.add("Samsung");
        list.add("Microsoft");
   
        //Java ArrayList contains Example, equals method is used to check if
        //ArrayList contains an object or not
        System.out.println("Does list contains Apple :" + list.contains("Apple"));
        System.out.println("Does list contains Verizon :" + list.contains("Verizon"));
     
        //Java ArrayList Example - size
        System.out.println("Size of ArrayList is : " + list.size());
     
        //Java ArrayList Example - replacing an object
        System.out.println("list before updating : " + list);
        list.set(3, "Bank of America");
        System.out.println("list after update : " + list);
     
        //Java ArrayList Example - checking if ArrayList is empty
        System.out.println("Does this ArrayList is empty : " + list.isEmpty());
     
        //Java ArrayList Example - removing an Object from ArrayList
        System.out.println("ArrayList before removing element : " + list);
        list.remove(3); //removing fourth object in ArrayList
        System.out.println("ArrayList after removing element : " + list);
     
       //Java ArrayList Example - finding index of Object in List
        System.out.println("What is index of Apple in this list : " + list.indexOf("Apple"));
     
        //Java ArrayList Example - converting List to Array
        String[] array = list.toArray(new String[]{});
        System.out.println("Array from ArrayList : " + Arrays.toString(array));
     
        //Java ArrayList Example : removing all elements from ArrayList
        list.clear();
        System.out.println("Size of ArrayList after clear : " + list.size());
    }
 
}

Output:
Does the list contains Apple: true
Does the list contains Verizon: false
The size of ArrayList is: 4
list before updating : [Apple, Google, Samsung, Microsoft]
list after update : [Apple, Google, Samsung, Bank of America]
Does this ArrayList is empty: false
ArrayList before removing element : [Apple, Google, Samsung, Bank of America]
ArrayList after removing element : [Apple, Google, Samsung]
What is an index of Apple in this list : 0
Array from ArrayList : [Apple, Google, Samsung]
Size of ArrayList after clear : 0


These were some frequently used examples of ArrayList in Java. As a Java developer, you should be familiar with these common ArrayList operations so that you can use the ArrayList class in your Java programmer whenever you want. We have seen ArrayList contains an example that used the equals method to check if an object is present in ArrayList or not. We have also seen how to add, remove and modify the contents of ArrayList, etc.


Other Java Collection tutorial and Interview Questions


1 comment:

  1. Good tutorial for any beginners, who doesn't know anything about ArrayList in Java. I would like to see some practical example of using Java ArrayList, than operating over them e.g. How about using ArrayList for storing list of employees in a department? than you have different question to answer e.g.
    does a particular employee belongs to this department?
    sorting them based upon their age? etc

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.