Java ArrayList

  • ArrayList is an implementation class of List interface.
  • Underlying data structure is resizable array.
  • Duplicates are allowed.
  • Insertion order is preserved.
  • Null insertion is possible.
  • Heterogeneous data items are allowed.
  • Implements Serializable and Clonable and RandomAccess interface.

 

General hierarchy:

    java.lang.Object
            
      java.util.AbstractCollection
         
       java.util.AbstractList
         
        java.util.ArrayList
         
public class ArrayList
extends AbstractList
implements List, RandomAccess, Cloneable, Serializable

ArrayList Constructors

 

1. ArrayList() : Create an empty ArrayList object with default initial capacity 10. Once
ArrayList reaches its max capacity then new ArrayList will be created with a new
capacity.

new capacity=(current capacity*3/2)+1
i.e (10*3/2)+1=16
after this new ArrayList is created the previous one will be deleted and it will be treated as
garbage.
 

2. ArrayList(int initialcapacity) : Constructs an empty ArrayList with the specified
initial capacity given by the user.

3. ArrayList(Collection c) : For every Collection object, we can create an equivalent
ArrayList.

 

ArrayListExample1.java

importjava.util.*;
classArrayListExample1
    {
             public static void main(String[] args)
                {
                 ArrayList al=new ArrayList();
                 al.add("Tapu");
                 al.add("Satyajit");
                 al.add(20);
                al.add(null);
                System.out.println(al); //[Tapu,Satyajit,20,null]
                 al.remove(1);
                 al.add(1,"Sasmita Samant");
                 al.add("Nilan");
                System.out.println(al); //[Tapu,Sasmita,20,null,Nilan]
                 }
    }

Output
[Tapu,Satyajit,20,null]
[Tapu,Sasmita Samant,20,null,Nilan]

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Projects

SILAN Technology
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment