0% found this document useful (0 votes)
4 views2 pages

Array List

An ArrayList in Java is a dynamic array that can grow and shrink as elements are added or removed, utilizing methods like add(), get(), set(), and remove(). It provides efficient operations for managing elements, with time complexities for various operations outlined. Additionally, it demonstrates how to convert between ArrayLists and arrays for both object and primitive types.

Uploaded by

sandhyakontham05
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Array List

An ArrayList in Java is a dynamic array that can grow and shrink as elements are added or removed, utilizing methods like add(), get(), set(), and remove(). It provides efficient operations for managing elements, with time complexities for various operations outlined. Additionally, it demonstrates how to convert between ArrayLists and arrays for both object and primitive types.

Uploaded by

sandhyakontham05
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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)

You might also like