To determine the number of items stored in an arraylist object, you use this method.

To determine the number of items stored in an arraylist object, you use this method.

What is an Array? An array is a container object that holds a fixed number of values of a single type. For example, you are going to create an array for student marks. The Marks are stored as integer value so you can create an integer array that holds all student marks. Suppose some students have marks from an examination but other students have a grade. A grade is a string value like "A+", "A" etc. What do you do in this situation? You should create two arrays, one for marks that is an integer array and another for grade that is a string array. Now you have two arrays so you can't retrieve the result in students sequence because you have marks in the first array then grades in the second array. So when you have multiple types set then you can use an ArrayList .

ArrayList is one of the most flexible data structure from Java Collections. Arraylist is a class which implements List interface . It is one of the widely used because of the functionality and flexibility it offers. It is designed to hold heterogeneous collections of objects. The capacity of an ArrayList is the number of elements the ArrayList can hold. As elements are added to an ArrayList, the capacity is dynamically increased as required through reallocation. It can contain duplicate elements too. Elements in this collection can be accessed using an integer index. Indexes in this collection are zero-based . It allows random access because array works at the index basis. Moreover, it maintains insertion order.

How to add an Items in an ArrayList ?

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"); } }

How to print arraylist element?

// 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

How to find length/size of ArrayList in Java?

By using size() method of ArrayList class we can easily determine the size of the ArrayList. This method returns the number of elements in an ArrayList Object.

// 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.

Difference between ArrayList.clear() and removeAll(collection)?

To determine the number of items stored in an arraylist object, you use this method.

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 !!"); } }