JAVA - ArrayList

Introduction to ArrayList

“ArrayList” can be found in the ‘java. util’ package and it is a class that can be resizable. As we all know built-in array size cannot be modified. If you want to add/remove an element you have to create a new array. But in ArrayList, you can add and remove elements whenever you want. ArrayList is more flexible than the traditional way and it inherits the AbstractList class and implements the List interface.

Photo by Ari He on Unsplash

Creating an array list and adding items.

Example:

import java.util.ArrayList; public class ArrayListEx{
ArrayList<String> books = new ArrayList<String>(); //Create an ArrayList object
books.add("Harry Potter");
books.add("Sherlock Holmes");
books.add("Famous Five");
books.add("Secret Seven");
System.out.println(books);
}
}

Characteristics of ArrayList

  • The ArrayList class can have duplicate elements and it maintains insertion order.
  • Non synchronized.
  • Allows random access since the array works on an index basis.
  • The ArrayList class extends AbstractList class that implements the List interface.
  • List interface extends the Collection and also iterable interfaces in order of hierarchy.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store