by BehindJava

What is an ArrayList Data Structure in Java

Home » datastructures » What is an ArrayList Data Structure in Java

In this tutorial, we are going to learn about the ArrayList in detail.

ArrayList

  • Have a look at ArrayList documentation which has detailed description.
  • It uses a dynamic array to store the duplicate elements and maintains the insertion order.
  • The ArrayList class maintains the insertion order and is non-synchronized which means it is not thread-safe.
  • The elements stored in the ArrayList class can be randomly accessed.
  • Cannot create the arrayList with primitive data types such as int, float, char etc.

Consider the following example.

import java.util.ArrayList;
import java.util.List;

public class BehindJava {
	public static void main(String ars[]) {
		List<String> list1 = new ArrayList<>();
		list1.add("Mango");
		list1.add("Apple");
		list1.add("Banana");
		list1.add("Grapes");
		list1.add("Banana");
		list1.forEach(n -> System.out.print(n + " "));
		System.out.println();
		List<Integer> list2 = new ArrayList<>();
		list2.add(5);
		list2.add(4);
		list2.add(3);
		list2.add(2);
		list2.add(1);
		list2.forEach(m -> System.out.print(m+ " "));
	}
}

Output:

Mango Apple Banana Grapes Banana 
5 4 3 2 1