JAVA ArrayList Constructors
java.util.ArrayList
public class ArrayList
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.
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("Sashi");
al.add("Satyajit");
al.add(20);
al.add(null);
System.out.println(al); //[Sashi,Satyajit,20,null]
al.remove(1);
al.add(1,"Sasmita");
al.add("Nilan");
System.out.println(al); //[Sashi,Sasmita,20,null,Nilan]
}
}
classArrayListExample1
{
public static void main(String[] args)
{
ArrayList al=new ArrayList();
al.add("Sashi");
al.add("Satyajit");
al.add(20);
al.add(null);
System.out.println(al); //[Sashi,Satyajit,20,null]
al.remove(1);
al.add(1,"Sasmita");
al.add("Nilan");
System.out.println(al); //[Sashi,Sasmita,20,null,Nilan]
}
}
Output
[Sashi,Satyajit,20,null]
[Sashi,Sasmita,20,null,Nilan]
Thanks & Regards:
Trilochan Tarai
SILAN Technology
java8s.com
0674-2361252
No comments:
Post a Comment