Lesson - Array List in Java
Lesson - Array List in Java
Lesson - Array List in Java
ArrayList in Java
Pre-Requisites
• Basics of Java
• Arrays in Java
• If we know the count of objects to be created. Then, in that case, an array of such objects is created and all
the objects are stored in that array. Now, if there is a situation where we do not have information about the
count of objects to be stored and in which format, then we have to use the feature of java called “Collections”.
• A group of individual objects which are represented as a single unit is known as the collection of the objects.
• In other words, Java Collection is a single unit of objects.
• This collections framework provides a set of interfaces and classes to implement various data structures and
algorithms.
What is Framework ?
• Using frameworks saves time and reduces the risk of errors. You don't need to write everything from scratch.
• It is non-synchronized.
Methods of ArrayList
Method Description
This is used to insert the specified element at the
void add(int
ht ps:/ www.javatpoint.com/java-ar aylist-ad -method index, E element) specified position in a list.
boolean isEmpty()
It returns true if the list is empty, otherwise false.
int lastIndexOf(Object o) It is used to return the index in this list of the last
occurrence of the specified element, or -1 if the list
does not contain this element.
boolean contains(Object o) It returns true if the list contains the specified element.
boolean removeAll(Collection<?> c) It is used to remove all the elements from the list.
protected void removeRange(int fromIndex, IIt is used to remove all the elements lies within the
int toIndex) given range.
void replaceAll(UnaryOperator<E> operator) It is used to replace all the elements from the list with
https:/ www.javatpoint.com/java-ar aylist-removerange-method the specified element.
void sort(Comparator<? super E> c) It is used to sort the elements of the list on the basis
of the specified comparator.
import java.util.ArrayList;
import java.io.*;
import java.util.*;
public class ExampleArray{
}
}
Approach 1 – We can use the custom function. Here we will reverse the ArrayList by considering it as a simple
array and traversing it in reverse order.
Steps –
1) Iterate the ArrayList from 0 to n/2 index
2) Swap the first and last element in each iteration
3) Once the loop ends, it return the reversed ArrayList
import java.util.ArrayList;
import java.io.*;
import java.util.*;
list.add(new Integer(0));
list.add(new Integer(10));
list.add(new Integer(3));
list.add(new Integer(5));
list.add(new Integer(22));
list.add(new Integer(10));
System.out.println("Before reverse: ");
print(list);
System.out.println("");
for (int i = 0; i < list.size() / 2; i++) {
Integer t = list.get(i);
list.set(i, list.get(list.size() - i - 1));
list.set(list.size() - i - 1, t);
}
System.out.println("After reverse: ");
print(list);
}
Space Complexity –
If there are N numbers in arraylist array arr, then complexity will be O(1).
Approach 2 – We can use the inbuilt Collections function. Collections is a class in Java which contains different
in-built functions for sorting, reversing, etc. We will use the Collections.reverse() function to reverse the ArrayList.
import java.io.*;
import java.util.*;
list.add(new Integer(0));
list.add(new Integer(10));
list.add(new Integer(3));
list.add(new Integer(5));
list.add(new Integer(22));
list.add(new Integer(10));
System.out.print("Before reverse: ");
print(list);
System.out.println("");
Collections.reverse(list);
System.out.print("After reverse: ");
print(list);
}