4 Demos to Understand Java ArrayList Easily

What is an ArrayList?

The ArrayList is a Java class that is inherited from (or extends) the AbstractList class. The ArrayList class implements the List interface.

>>See an example of ArrayList

Main points about the ArrayList Java:

  • A Java ArrayList is a dynamic array; an array that can grow after it is created.
  • The standard arrays are not dynamic i.e. once a standard array is created it cannot grow. These are of fixed size and you cannot add or remove elements after these are created whereas the ArrayList allows that.
  • ArrayList in Java gives fast iteration.
  • It also gives fast random access.
  • It is not synchronized.
  • The ArrayList provides more powerful insertion than the standard arrays.
  • Search mechanism in ArrayList is better than standard arrays.
  • The ArrayList is comparable to the vector class, in that it is the dynamic array as well as to the standard arrays.
  • The ArrayList manipulation is slower as a lot of shifting occur.

An ArrayList size method example

How to use ArrayList class in Java Program?

In order to use the ArrayList class in your projects, you must import this class as follows:

Import Java.util.ArrayList;

Declaring an ArrayList

After importing the Java ArrayList class, it is time to declare an object of the ArrayList class. The default way is:

ArrayList array_name = new ArrayList();

An ArrayList “array_name” of the ten size is created. The ArrayList default constructor creates an array of the ten size.

Other methods of declaration by using other constructors

You may also use other constructors of the ArrayList to declare an array as follows:

(1)    By specifying size:

ArraList array_name = new ArrayList (size)

An array with the given number in the integer will be created.

(2)    Create ArrayList from the other collection type:

ArrayList array_name = new ArrayList ( Collection c) ;

Java ArrayList example

The following example shows creating an ArrayList object. It adds five elements and then a for loop is used to display the ArrayList elements. See the example and code by clicking the image or link below:

Java ArrayList

Experience this online



The output will be:

10

20

30

40

50

A few useful ArrayList methods

Following are a few useful ArrayList methods with examples.

ArrayList add Example

The example below shows creating an ArrayList object. It adds elements to the specific index position by using the ArrayList add method.

Experience this online



The output will be:

0

10

20

25

30

40

50

As you can see, two array elements though added at the last in above sequence, however, its position is controlled by giving the specific index positions.

ArrayList size method example

The following example shows how to use the ArrayList size method.

Experience this online



The output will be:

The size of ArrayList is: 5

ArrayList contains method example

The contains method of the ArrayList is used to search in the ArrayList.

The following example shows how to use the ArrayList contains method. The contains method returns a Boolean. The returned result will be true if the given term is found.

Experience this online



The output will be:

The term is found in ArrayList? true

The term is found in ArrayList? false

 

Read Further: Java vector | Java arrays


Was this article helpful?

Related Articles

Leave A Comment?