Back to photostream

How to initialize an ArrayList in Java

ArrayList in Java is the most commonly used data structure for creating a dynamic size array. It extends the Abstract class and implements the Java List interface. The main difference between array and ArrayList is that the array is static(we cannot add or remove elements) while ArrayList is dynamic(we can add, remove or modify elements). In this article, we will see what is ArrayList and how to initialize an ArrayList in Java?

 

You might also be interested in ArrayList vs LinkedList

Java ArrayList Hierarchy

 

Declaring an ArrayList Class in Java

In order to use ArrayList in Java, we must import java.util.ArrayList. Below is the declaration of an ArrayList

 

public class ArrayList extends AbstractList implements List, RandomAccess, Cloneable,Serializable

 

where E denotes the element or Object type (Eg: Integer, String, etc)

 

The ArrayList class extends the AbstractList class and implements the List interface.

ArrayList Constructors

We can create ArrayList in Java Constructors in the below 3 methods:

 

Java ArrayList Features

 

- It is a resizeable dynamic array where we can add, modify or remove elements any time from the list

- Maintains a sequential order.

- It is easy to access any data from the list based on the index.

- Allows duplicate elements in the list

 

Java ArrayList Methods

In addition to the below methods, ArrayList in Java has access to all methods of the List interface.

 

Java ArrayList Generic and Non-Generic Declaration

Before JDK 1.5,

 

www.tutorialcup.com/java/arraylist-java.htm

21 views
0 faves
0 comments
Uploaded on August 11, 2021