General hierarchy:
java.lang.Object
java.util.AbstractCollection
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]
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc