In the previous post, we have learnt about collection interface and list interface. In this post, we will talk about ArrayList. ArrayList is a child class of list interface. 

Topics covered:

 

    • Introduction of ArrayList in java.

 

    • ArrayList Constructors.

 

    • ArrayList Methods.

 

    • RandomAccess Interface.

 

    • Difference between ArrayList and Vector.

 

    • Cheat code for ArrayList

 

 

Introduction of ArrayList

list interface

 

    1. The underlying data structure is resizable Array or growable array.

 

    1. Duplicates are allowed.

 

    1. Insertion order is preserved.

 

    1. Heterogeneous objects are allowed. – Except for TreeSet and TreeMap everywhere, heterogeneous objects are allowed.

 

    1. Null insertion is possible.

 

 

ArrayList Constructors:

 

 

    1. ArrayList l = new ArrayList();

 

 

Creates an empty ArrayList objects with default initial capacity 10. Once ArrayList reaches its max capacity, a new ArrayList will be created with new capacity.

 

New capacity = (current capacity * 3/2) + 1

 

 

    1. ArrayList l = new ArrayList(int initial_capacity);

 

It is used to build an ArrayList that has the specified initial capacity.

 

    1. ArrayList l = new ArrayList(Collection c);

 

It is used to build an ArrayList that is initialized with the elements of the collection c.

Every collection implements the serializable and cloneable interface.

Usually, we can use collections to hold and transfer objects from one place to another and to provide this requirement, every collection already supports serializable and cloneable interfaces.

ArrayList and vector implement RandomAccess Interface so we can access any element at the same speed.

So, if our main operation is retrieval then highly go for ArrayList.

 

RandomAccess Interface:

 

 

    1. It is present in java.util package.

 

    1. Supported by ArrayList and vector.

 

    1. Has no methods and it is a marker interface.

 

random access interface

If our main operation is insertion/deletion from the middle then ArrayList is not recommended.

 

ArrayList Methods

if the content is not visible, click on the image and zoom it to read.

arrayList Methods

ArrayList Example

Difference between ArrayList and Vector

 

 

ArrayList Vector
Methods are non-synchronized in nature. Methods are synchronized in nature.
Objects are thread safe. Objects are not thread safe.
Performance is relatively high. Performance is comparatively low.
Introduced in JDK 1.2 Introduced in JDK 1.0
It is non-legacy class. It is a legacy class.

Since methods of ArrayList is non-synchronized so threads are not required to wait to operate on ArrayList hence relatively performance is high.

At a time, only one thread is allowed to operate on vector object so threads are required to wait to operate hence relatively performance is low.

How to get the Synchronized version of an ArrayList object?

ArrayList l = new ArrayList(); 

By default ArrayList object is non-synchronized but we can get the synchronized version of ArrayList by using collection class SynchronizedList() method.

Public static list SynchonizedList(List l)

List l1 = Collections.SynchronizedList(l);

ArrayList cheat codes

arraylist cheat code

For more Read here Documentation 

LEAVE A REPLY

Please enter your comment!
Please enter your name here