❮ Previous Next ❯

Java ArrayList

 

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]

❮ Previous Next ❯

Follow Us




  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved