0% found this document useful (0 votes)
17 views

1 Array List

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

1 Array List

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

2) List:

 It is the Child Interface of Collection.


 If we want to Represent a Group of Individual Objects as single entity where
Duplicates are allowed
and Insertion Order Preserved. Then we should go for List.
 We can Preserve Insertion Order and we can Differentiate Duplicate Object by
using
Index. Hence Index will Play Very Important Role in List.

ArrayList(class):
=========
 The Underlying Data Structure for ArrayList is Resizable Array OR Growable Array.
 Duplicate Objects are allowed.
 Insertion Order is Preserved.
 Heterogeneous Objects are allowed (Except TreeSet Everywhere
Heterogeneous Objects are allowed).
 null Insertion is Possible.
 Implements Serializable and Cloneable Interfaces and RandomAccessInterface.

ArrayList is the Best Choice if we want to Perform Retrieval Operation Frequently.


 But ArrayList is Worst Choice if Our Frequent Operation is Insertion OR Deletion
in the
Middle. Because it required Several Shift Operations Internally.

Example :

package list;

import java.util.ArrayList;

public class ArrayListDemo {

public static void main(String[] args) {

ArrayList al=new ArrayList();


al.add(11);
al.add(22);
al.add("Santosh");
al.add(11);
al.add("Bikkad");
al.add("Santosh");
al.add(null);
System.out.println(al);
}
}

Constructors:
1) ArrayList l = new ArrayList();
 Creates an Empty ArrayList Object with Default Initial Capacity 10.
 If ArrayList Reaches its Max Capacity then a New ArrayList Object will be Created
with

New Capacity = (Current Capacity * 3/2)+1


=(10 * 3/2)+1
=(5 * 3)+1
=15+1
=16

if 16 elements are also added then new capacity will be

=(16*3/2)+1
=(8*3)+1
=24+1
=25

Example :

package list;

import java.util.ArrayList;

public class ArrayListDemo {

public static void main(String[] args) {

ArrayList al=new ArrayList();


al.add(11);
al.add(22);
al.add("Santosh");
al.add(11);
al.add("Bikkad");
al.add("Santosh");
al.add(null);
al.add(56);
al.add(67);
al.add(78);
al.add(89);
System.out.println(al);
}
}

2) ArrayList l = new ArrayList(initial Capacity);


Creates an Empty ArrayList Object with specified Initial Capacity.

Example : initial capacity =12.

if 12 elements are also added then new capacity will be


new capacity = (12 *3/2)+1
=(6 * 3 )+1
=18+1
=19

Example :

package list;

import java.util.ArrayList;

public class ArrayListDemo {

public static void main(String[] args) {

ArrayList al=new ArrayList(12);


al.add(11);
al.add(22);
al.add("Santosh");
al.add(11);
al.add("Bikkad");
al.add("Santosh");
al.add(null);
al.add(56);
al.add(67);
al.add(78);
al.add(89);
al.add(57);
al.add(100);
System.out.println(al);
}
}

3) ArrayList l = new ArrayList(Collection c);


 Creates an Equalent ArrayList Object for the given Collection Object.

Example :

package list;

import java.util.ArrayList;
import java.util.HashSet;

public class ArrayListDemo {

public static void main(String[] args) {

HashSet hs=new HashSet();

ArrayList al=new ArrayList(hs);

}
}

You might also like