What is Array List ?
- The ArrayList class extends AbstractList and implements the List interface.
- It is resizable array or growable array
- Duplicates are allowed
- Insertion order is preserved
- Supports heterogeneous objects(different type of objects)
- null insertion allowed (al.add(null)) //where al ---> arraylist
- Usually we use collections to hold and transfer objects from 1 location to another location(container) to provide support for these requirements,every collection class by default implements serializable and cloneable interfaces
Constructors::
1) ArrayList l=new ArrayList(); # creates an empty arraylist object with default initial capacity 10.Once arraylist reaches its max. capacity then a New Arraylist object created with
//New capacity= [current capacity * (3/2)]+1;
2) ArrayList l=new ArrayList(int initialcapacity); # create an empty arraylist with specified initial capacity.
3) ArrayList l=new ArrayList(Collection c); # create an equivalent arraylist object for the given collection.
##sample demo program ###
import java.util.ArrayList;
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList al= new ArrayList<>();
al.add(1);
al.add("string type");
al.add(1.2f);
al.add(null);
System.out.println("ArrayList elements ::"+ al);
System.out.println("ArrayList elements class ::"+ al.getClass());
System.out.println("Retrive element from ArrayList :"+al.get(1));
}
}
output:
ArrayList elements ::[1, string type, 1.2, null]
ArrayList elements class ::class java.util.ArrayList
Retrive element from ArrayList :string type
Key Points:
- ArrayList is the best choice if ever frequent operation is retrieval operation(because arraylist implements randomaccess interface)
- ArrayList is the worst choice if ever frequent operation is insertion or deletion in the middle
Please comment below to feedback or ask questions.
No comments:
Post a Comment
Please comment below to feedback or ask questions.