Java ArrayList

23 Likes Comment
ArrayList in Java

Hey Shouters!! Today we have come up with the ArrayList in Java with good examples to understand. Some main points covered in this article. These are given below:

Introduction
ArrayList Class Declaration
Advantages
Methods
A Simple Example
Synchronization Of ArrayList
Convert ArrayList to LinkedList Program

Introduction

Before discussion of ArrayList some basic concepts are discussed here.

The java.util package contains one of Java’s most powerful subsystems i.e. The Collection Framework.

The Collection Framework contains interfaces and classes to provide state-of-the-art technology for managing groups of objects.

Add, Remove, Search etc. activities can be performed using Java collection framework.

The collection framework can contain 4 basic things i.e. Lists, Sets, Maps, Queues.

One interface can extend another interface, similarly, The List interface extends Collection interface to define an ordered collection with duplicates allowed.

The List interface is implemented via ArrayList class.

Java ArrayList class can be used to create containers that store lists of objects.

Java ArrayList can be considered as a growable array.

Java ArrayList gives fast iteration and fast random access.

Java ArrayList implements the new RandomAccess interface.

A very much similar to ArrayList class is Vector class.

Java ArrayList class are imported via This statement

An ArrayList container size can change during program execution.

Java ArrayList is an ordered collection (by index), but not sorted.

Java ArrayList class can contain duplicate elements.

Java ArrayList class is non-synchronized.

ArrayList Class Declaration

ArrayList class is declared as given below,

To declare an ArrayList using default constructor, You can use this syntax:

To declare an ArrayList using the parameterized constructor, You can use this syntax:

You can also specify the capacity/size of the initial ArrayList as well as create ArrayList from other collection types.

Advantages

Some of the advantages ArrayList has over arrays are,

  • It can grow dynamically.
  • It provides more powerful insertion and search mechanisms than arrays.


Methods

ADD METHOD :

This method is used to adds an item to an ArrayList.
The default version adds an item at the next available location i.e. given below,

An overloaded version allows you to specify a position at which to add the item i.e. given below,

REMOVE METHOD :

This method is used to removes an item from an ArrayList at a specified location i.e. given below,

SET METHOD :

This method is used to alters an item at a specified ArrayList location i.e. given below,

GET METHOD :

This method is used to retrieves an item from a specified location in an ArrayList i.e. given below,

SIZE METHOD :

This method is used to returns the current ArrayList size i.e. given below,

Java program to demonstrate the use of all the above methods describes above.

Here we are creating ArrayList named myAL and adding objects using add() methods as well as using index based add method, then printing all the objects using for loop. Then get(), contains(), and size() methods are used, the output is given below :

A Simple Example

Here we are creating an Student class and A_L_Example class. Declare some Student class objects and creating and ArrayList of Student class named al and adding objects using add() methods then printing all the objects using for loop. The output is given below :

Synchronization Of ArrayList

Implementation of arrayList is not synchronized is by default. It must synchronized externally using Collections.synchronizedList() method.

Convert ArrayList to LinkedList Program

ArrayList should be used in an application when we need to search objects from the list based on the index.

ArrayList performance degrades when there is lots of insert and update operation in the middle of the list.

ArrayList and Vector are similar classes only difference is Vector has all method synchronized.

To read more :

You might like

Saif

About the Author: Saif

Leave a Reply

Your email address will not be published. Required fields are marked *

Close Bitnami banner
Bitnami