10 Examples of ArrayList in Java (original) (raw)

ArrayList Example in Java

Hello guys, In this Java ArrayList Tutorial, we will see 10 common usage of ArrayList with examples. You will learn how to add elements in ArrayList, how to remove elements from ArrayList, how to check if ArrayList contains a given object, how to sort an ArrayList of objects and several other ArrayList functions which we use daily as a Java programmer. 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.

10 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 classArrayListTest {

public static voidmain(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