ArrayList in Java
π ArrayList is a class in Java Collections Framework that implements the List interface and provides a dynamic array (resizable array).
capacity of ArrayList
When an ArrayList is created, its default capacity is 10 unless specified otherwise. The ArrayList automatically resizes itself when the number of elements exceeds its current capacity.
If the current capacity is reached, the ArrayList expands its size by approximately 50% of the old capacity, allowing for the addition of new elements without the need for creating a new array.
The resizing process ensures that there is always sufficient space to accommodate new elements, and this is managed internally.
Constructors in ArrayList:
- Default Constructor ArrayList list = new ArrayList<>(); π What it does: Creates an empty ArrayList Uses a default initial capacity (typically 10 internally, but lazy allocation may apply in newer JDKs) No elements stored yet π‘ Use when:
You donβt know how many elements youβll add.
πΉ 2. Constructor with Initial Capacity
ArrayList list = new ArrayList<>(20);
π What it does:
Allocates internal array with capacity = 20
No resizing until it exceeds 20 elements
π‘ Why useful:
Improves performance when size is known in advance
Avoids repeated resizing and copying
πΉ 3. Constructor with Collection
List temp = List.of("A", "B", "C");
ArrayList list = new ArrayList<>(temp);
π What it does:
Creates a new ArrayList
Shallow copy behavior happens here .
β Only references are copied
β Objects inside are NOT duplicated
π‘ Important:
Original collection and new ArrayList are independent
Changes in one do NOT affect the other
Array vs ArrayList:
1.How to create a arraylist object in java ?
Answer:ArrayList list= new ArrayList();
2.Which package should import for using arraylist class?
Answer:import java.util.ArrayList;
3.Which inferface implemented in arraylist class ?
Answer :List Interface
4.Can you stored a value as a primitive data in arraylist ?
Answer :No.Stores Objects Only-
ArrayList nums = new ArrayList<>();
**5.What are the methods you know in arraylist ?
Answer: **add(),addAll(),removeAll(),remove(),clear(),contains(),get(),getFirst(),toArray()
6.Can you stored a value as a object in arraylist ?
Answer :yes
7.Can you please add 5 student name in arraylist and write code ?
//Answer :using add() method
public static void main(String[] args) {
ArrayListDemo ad = new ArrayListDemo();
String stu[] = { "Suresh", "John", "Thomas", "Mike", "Ara" };
ArrayList student = new ArrayList();
for (Object student1 : stu) {
student.add(student1);
}
8.How to remove the student in arraylist ? write code ?
Answer :remove(index value) - remove element from particular index
removeAll(collection) - removes all matching elements from another collection
remove(object) - removes first matching element
clear() - removes everything
package arraylist;
import java.util.ArrayList;
public class ArrayListDemo {
public static void main(String[] args) {
ArrayListDemo ad = new ArrayListDemo();
String stu[] = { "Suresh", "John", "Thomas", "Mike", "Ara","Andi" };
ArrayList student = new ArrayList();
ArrayList student2 = new ArrayList();
for (String student1 : stu) {
student.add(student1);
}
System.out.println("Student array list "+student);
ArrayList student3=student;
System.out.println("\"Student3 array list "+student3);
student2.addAll(student);
System.out.println("Student 2array list "+student2);
student.remove(1); //remove(index)removes one element by position
System.out.println(student);
student2.removeAll(student);//removes all matching elements from another collection
student.remove(student3);
student2.clear();//Remove everything
System.out.println(student);
student3.remove("Ara");//removes first matching element
System.out.println(student3);
}
}
9.ArrayList can have duplicate value ?
Answer :Yes.It allows.
10.Is ArrayList maintain the insertion order ?
Answer :The order is exactly the same as insertion.
11.How to print the value from arraylist using for loop ?
Answer :If traditional for loop is used,get(index)is used.
for non generic in enhanced for loop- Raw type iteration-Object type is used
for Generic version of for-each loop - Instead of Object,use the type declared for array list
package arraylist;
import java.util.ArrayList;
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList student = new ArrayList();
student.add("Life");
student.add("Time");
student.add("Gratitude");
student.add("Acceptance");
System.out.println("_________Using Enhanced For");
// for non generic ArrayList
for (Object name : student) {
System.out.println(name);
}
System.out.println("_______Using traditional for loop");
for (int i = 0; i < student.size(); i++) {
System.out.println(student.get(i));
}
}
}
12 How to sort a arraylist in java ?
Answer :
using Collections.sort() for ascending order
package arraylist;
import java.util.ArrayList;
import java.util.Collections;
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList student = new ArrayList();
student.add(90);
student.add(89);
student.add(54);
student.add(9);
Collections.sort(student);
System.out.println(student);
}
}
Scenario based example:
Playlist songs (normal usage)
Songs mostly accessed sequentially, not modified often.
package arraylist;
import java.util.*;
public class PlayList {
public static void main(String[] args) {
List music=new ArrayList();
music.add("Avalum nanum...");
music.add("Kpop Deamon hunter-My little soda pop.");
music.add("I am born for this.");
music.add("Chinnachiru vayathil...");
music.add("vetri kodi kattu");
play(music);
}
private static void play(List<String> music1) {
for(String song : music1) {
System.out.println("Playing......"+song);
}
}
}



Top comments (0)