Java ArrayList class

Java Arraylist tutorial

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 ?

How to print arraylist element?


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.


How to get specific ArrayList item?


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?


Since ArrayList indexes start from zero, aList.get(0) return the first item of ArrayList.

How to get the last element of Arraylist?

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.removeAll() example:


In case of removeAll, you should pass the same ArrayList as argument.

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

ArrayList.clear() and removeAll

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)):

Here we pass the argument as string object "Monday", so it will remove from the collection.

Remove by index :

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.


Sort an ArrayList in the reverse Order?


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()

Converting ArrayList to Array

ArrayList class has a method called toArray() that we can use to convert an ArrayList to Arrays.


Convert an array to ArrayList


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.


Convert ArrayList to List


The following Java program illustrates most of the above mentioned methods in a single program
 (C) 2021    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About