import java.util.*;
class TestClass
{
public static void main (String[] args) throws java.lang.Exception
{
// create an array list Object
ArrayList aList = new ArrayList();
aList.add("Sunday"); //adding item
aList.add("Monday");
aList.add("Tuesday");
}
}
// create an array list Object
ArrayList aList = new ArrayList();
aList.add("Sunday");
aList.add("Monday");
aList.add("Tuesday");
Iterator ir=aList.iterator();
while(ir.hasNext()){
System.out.println(ir.next());
}
//Output:
Sunday
Monday
Tuesday
// create an array list Object
ArrayList aList = new ArrayList();
aList.add("Sunday");
aList.add("Monday");
aList.add("Tuesday");
System.out.println(aList.size());
//Output: 3
How to get specific ArrayList item?
// create an array list Object
ArrayList aList = new ArrayList();
aList.add("Sunday");
aList.add("Monday");
aList.add("Tuesday");
System.out.println(aList.get(1));
//Output:Monday
Elements in ArrayList can be accessed using an integer index and ArrayList indexes start from zero. So aList.get(1) return the second item from ArrayList.
How to get the first item of Arraylist?
// create an array list Object
ArrayList aList = new ArrayList();
aList.add("Sunday");
aList.add("Monday");
aList.add("Tuesday");
System.out.println(aList.get(0));
//Output:Sunday
Since ArrayList indexes start from zero, aList.get(0) return the first item of ArrayList.
How to get the last element of Arraylist?
// create an array list Object
ArrayList aList = new ArrayList();
aList.add("Sunday");
aList.add("Monday");
aList.add("Tuesday");
System.out.println(aList.get(aList.size()-1));
The aList.size() return the total items in an ArrayList. Since ArrayList indexes start from zero, aList.size()-1 return the last item of ArrayList.
How to remove all elements from Java ArrayList?
You can use two different method to empty an arraylist in Java. They are ArrayList.clear() and ArrayList.removeAll()
//ArrayList.clear() example:
// create an array list Object
ArrayList aList = new ArrayList();
aList.add("Sunday");
aList.add("Monday");
aList.add("Tuesday");
System.out.println(aList.size());
aList.clear();
System.out.println(aList.size());
//Output:
3
0
ArrayList.removeAll() example:
// create an array list Object
ArrayList aList = new ArrayList();
aList.add("Sunday");
aList.add("Monday");
aList.add("Tuesday");
System.out.println(aList.size());
aList.removeAll(aList);
System.out.println(aList.size());
//Output:
3
0
In case of removeAll, you should pass the same ArrayList as argument.
ArrayList.clear() VS. removeAll(collection)?
The methods clear() and removeAll(collection) serve two different purposes. The clear() method will go through the underlying Array and set each entry to null while removeAll(collection) will go through the ArrayList checking for collection and remove(Object) it if it exists. So it is confirm that clear() is much faster since it doesn't have to deal with all those extra method calls. How to remove a specific item from ArrayList? In general an object can be removed in two ways from an ArrayList (or generally any List), by index (remove(int)) and by object (remove(Object)).
Remove by object (remove(Object)):
// create an array list Object
ArrayList aList = new ArrayList();
aList.add("Sunday");
aList.add("Monday");
aList.add("Tuesday");
aList.remove("Monday");
Here we pass the argument as string object "Monday", so it will remove from the collection.
Remove by index :
// create an array list Object
ArrayList aList = new ArrayList();
aList.add("Sunday");
aList.add("Monday");
aList.add("Tuesday");
aList.remove(1);
Here we pass the argument as index remove(1) "Monday", so it will remove from the collection.
How to sort an ArrayList in Java?
In ArrayList, elements are placed as they are inserted. But while coding, you often need them in some order. in order to sort an ArrayList, we use sort() method of Collections class.
// create an array list Object
ArrayList aList = new ArrayList();
aList.add("Sunday");
aList.add("Monday");
aList.add("Tuesday");
System.out.println("Before sort.....");
Iterator ir=aList.iterator();
while(ir.hasNext()){
System.out.println(ir.next());
}
Collections.sort(aList);
System.out.println("After sort.....");
ir=aList.iterator();
while(ir.hasNext()){
System.out.println(ir.next());
}
Output:
Before sort.....
Sunday
Monday
Tuesday
After sort.....
Monday
Sunday
Tuesday
Sort an ArrayList in the reverse Order?
// create an array list Object
ArrayList aList = new ArrayList();
aList.add("Sunday");
aList.add("Monday");
aList.add("Tuesday");
Collections.sort(aList,Collections.reverseOrder());
Iterator ir=aList.iterator();
while(ir.hasNext()){
System.out.println(ir.next());
}
//Output:
Tuesday
Sunday
Monday
Search an item in Java ArrayList
You can check if a value exists in Java ArrayList using the following methods: ArrayList.contains(), ArrayList.indexOf() and ArrayList.lastIndexOf()
// create an array list Object
ArrayList aList = new ArrayList();
aList.add("Sunday");
aList.add("Monday");
aList.add("Tuesday");
//Using contains method
if (aList.contains("Monday")) {
System.out.println("Account found");
} else {
System.out.println("Account not found");
}
//using indexOf method
int val = aList.indexOf("Monday");
if (val !=-1) {
System.out.println("Item found");
} else {
System.out.println("Item not found");
}
//using lastIndexOf method
int val = aList.lastIndexOf("Monday");
if (val !=-1) {
System.out.println("Item found");
} else {
System.out.println("Item not found");
}
Converting ArrayList to Array
ArrayList class has a method called toArray() that we can use to convert an ArrayList to Arrays.
// create an array list Object
ArrayList aList = new ArrayList();
aList.add("Sunday");
aList.add("Monday");
aList.add("Tuesday");
String[] arr = (String[])aList.toArray(new String[aList.size()]);
for(String item : arr)
System.out.println(item);
//Output:
Sunday
Monday
Tuesday
Convert an array to ArrayList
// create an array Object
String days[]={"Sunday", "Monday", "Tuesday"};
ArrayList < String> arrList= new ArrayList < String>(Arrays.asList(days));
for(String item : arrList)
System.out.println(item);
//Output:
Sunday
Monday
Tuesday
Convert an ArrayList to a String
The String join() method help you to convert an ArrayList to String. The java string join() method returns a string joined with given delimiter. In string join method, delimiter is copied for each elements.
// create an array list Object
ArrayList aList = new ArrayList();
aList.add("Sunday");
aList.add("Monday");
aList.add("Tuesday");
String result = String.join(",", aList);
System.out.println(result);
//Output:Sunday,Monday,Tuesday
Convert ArrayList to List
// create an array list Object
ArrayList aList = new ArrayList();
aList.add("Sunday");
aList.add("Monday");
aList.add("Tuesday");
List list = aList;
Iterator ir=list.iterator();
while(ir.hasNext()){
System.out.println(ir.next());
//Output:
Sunday
Monday
Tuesday
The following Java program illustrates most of the above mentioned methods in a single program.
import java.util.*;
class TestClass
{
public static void main (String[] args) throws java.lang.Exception
{
//create new ArrayList Object
ArrayList aList = new ArrayList();
aList.add("Sunday");
aList.add("Monday");
aList.add("Wednesday");
//using Iterator to see all elemnets in ArrayList
Iterator < String> itr = aList.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
//adding element at specified index
aList.add(2,"Tuesday");
System.out.println(aList);
//Retrieve ArrayList elements by index
System.out.println(aList.get(2));
//Search an item in ArrayList
if(aList.contains("Monday"))
System.out.println("Item Found");
else
System.out.println("Item not Found");
//Remove a single element from ArrayList by index
aList.remove(1);
System.out.println(aList);
//Remove all elements from ArrayList
aList.clear();
//Check Vector is ArrayList or not
if(aList.isEmpty())
System.out.println("ArrayList Empty !!");
else
System.out.println("ArrayList Contains: " + aList.size() + " elements !!");
}
}