What is an ArrayList?
An ArrayList in Java is a resizable array — it can grow and
shrink dynamically when elements are added or removed.
Create an ArrayList
ArrayList<String> cars =new ArrayList<>();
can use methods like add(), get(), set(), and remove() to
manage your list of elements.
Add An Element [Link](“nithin”);
Add element at specified index [Link](0,”Ram”);
Access an element [Link](1); // index value
Change an element [Link](1,”Shiv”);
Remove an element [Link](0); // index value
To remove all elements at once [Link]();
Size [Link]();
Sort [Link](cars);
Operation Time Comment
add() O(1) Fast
get() Random
O(1)
access
set() Replace
O(1)
element
remove(index) Shifts
O(n)
elements
contains() Linear
O(n)
search
we created elements (objects) of type "String", Integer for
int. For other primitive types, use: Boolean for
boolean, Character for char, Double for double
ArrayList → Array
// For Integer list
int[] arr = [Link]().mapToInt(i -> i).toArray();
// For String list
String[] arr = [Link](new String[0]);
Array → ArrayList
// For object arrays (e.g., String, Integer)
List<Integer> list = new ArrayList<>([Link](arr));
// For primitive arrays (e.g., int[])
List<Integer> list = [Link](arr).boxed().toList();
Space
Operation Explanation
Complexity
new ArrayList<>() O(1) Just creates an empty list (no elements yet)
add(E e) O(1) Expands capacity occasionally when full
get(int index) O(1) No extra space used
set(int index, E e) O(1) Replaces element in place
Removes element, shifts others — no new
remove(int index) O(1)
space used
contains(Object o) O(1) Uses constant extra space (loop variable)
Overall Space (for n
O(n) Stores n object references internally
elements)