In this article, we will discuss ArrayList in Java and learn how to use it in our programs practically. We will find answers to – How to initialize ArrayList in Java? How to sort an ArrayList in Java? How to sort an ArrayList in Java? – and more. So, let’s quickly get started!
The java package java.util contains a class with dynamic arrays called ArrayList. The Java Collection framework provides many interfaces and classes, and one such class is ArrayList. Its implementation comes in handy when an Array requires multiple changes.
We can say that ArrayLists are pretty similar to Arrays, but with added flexibility. So, what is the difference between Array and ArrayList in Java? A traditional Array has a fixed size. It does not provide flexibility. Contrarily, an ArrayList offers the ability to add and remove elements as per requirement. Thus, the ArrayList size in Java can be changed.
Moreover, since ArrayList implements the List interface, it provides us with all the List methods. Thus, it is possible to create duplicate elements, as well. It also inherits the AbstractList class.
Let us learn how to declare ArrayList in Java. The following syntax demonstrates ArrayList declaration.
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable
ArrayList also has a bunch of constructors. Let us look at each of them below.
The following list describes the constructors that an ArrayList contains in Java.
Finally, before moving on to program examples, let us discuss some methods of ArrayList in Java to use in our programs.
There are a bunch of other ArrayList methods to learn. Once you get familiar with its concept, you can always learn more about ArrayList later.
For now, let us look at some examples to get a more in-depth understanding of ArrayLists.
In this article section, we will discuss a few ArrayList programs in Java. Let’s learn about ArrayList implementation in Java.
In this program, we will create an ArrayList arrli. We will add new elements to the arrli, remove an element, and print the ArrayList arrli. We will set the ArrayList size to 5. However, it is crucial to know that the default size of ArrayList in Java is 10.
// Java ArrayList example on how to create ArrayList in Java
import java.io.*;
import java.util.*;
class ArrayListExample {
public static void main(String[] args)
{
// set ArrayList size
int n = 5;
// Initialize ArrayList integer type in Java
ArrayList<Integer> arrli = new ArrayList<Integer>(n);
// Append new elements at
// the end of the list
for (int i = 1; i <= n; i++)
arrli.add(i);
// Print ArrayList in Java
System.out.println(arrli);
// Remove element at index 3
arrli.remove(3);
// Display the new ArrayList
//this is how to print ArrayList in Java element by element
for (int i = 0; i < arrli.size(); i++)
System.out.print(arrli.get(i) + ” “);
}
[1, 2, 3, 4, 5]
[1, 2, 3, 5]
1 2 3 5
Through this code, we learn how to compare two ArrayList in Java?
public class CompareArraylistExample1
public static void main(String args[])
//compare two ArrayList in Java
//First ArrayList
ArrayList<String> firstList=new ArrayList<String>();
//add elements to ArrayList
firstList.add(“Apple”);
firstList.add(“Pears”);
firstList.add(“Guava”);
firstList.add(“Mango”);
System.out.println(firstList);
//Second ArrayList
List<String> secondList=new ArrayList<String>();
//adds elements to the arraylist
secondList.add(“Apple”);
secondList.add(“Pears”);
secondList.add(“Guava”);
secondList.add(“Mango”);
System.out.println(secondList);
//Comparing both ArrayLists
boolean boolval = firstList.equals(secondList);
//returns true because lists are equal
System.out.println(boolval);
//adding another element in the second list
secondList.add(“Papaya”);
//again comparing both lists
boolean bool = firstList.equals(secondList);
//returns false because lists are not equal
System.out.println(bool);
[Apple, Pears, Guava, Mango]
true
false
How to sort ArrayList in Java? We can sort ArrayLists in two ways – ascending and descending. Let us look at examples for each of these sorting methods.
public class SortArrayListExample1
// create object of ArrayList class
ArrayList<String> list = new ArrayList<String>();
// add elements to the ArrayList
list.add(“Volkswagen”);
list.add(“Toyota”);
list.add(“Porsche”);
list.add(“Ferrari”);
list.add(“Mercedes-Benz”);
list.add(“Audi”);
list.add(“Rolls-Royce”);
list.add(“BMW”);
// print the unsorted ArrayList
System.out.println(“Before Sorting: “+ list);
// sort ArrayList in ascending Order
Collections.sort(list);
// print the sorted ArrayList
System.out.println(“After Sorting: “+ list);
Before Sorting: [Volkswagen, Toyota, Porsche, Ferrari, Mercedes-Benz, Audi, Rolls-Royce, BMW]
After Sorting: [Audi, BMW, Ferrari, Mercedes-Benz, Porsche, Rolls-Royce, Toyota, Volkswagen]
public class SortArrayListExample3
list.add(“Data Science”);
list.add(“Testing”);
list.add(“C#”);
list.add(“Basic Language”);
list.add(“UML”);
list.add(“Algorithms “);
list.add(“Computer Networks”);
list.add(“Python”);
// use Collection.sort() method
Collections.sort(list, Collections.reverseOrder());
Before Sorting: [Data Science, Testing, C#, Basic Language, UML, Algorithms, Computer Networks, Python]
After Sorting: [UML, Testing, Python, Data Science, Computer Networks, C#, Basic Language, Algorithms]
Now, after understanding some basic programs, you can try some advanced programs too. Try writing your own code for the following –
How to reverse ArrayList in Java?
How to convert Array to ArrayList in Java?
How to convert ArrayList to Array in Java?
How to remove duplicates from ArrayList in Java?
In this article, we tried answering questions like – What is an ArrayList? How to use ArrayLists? We also discussed various ArrayList functions. We also learned how to sort an ArrayList in Java. We successfully understood the internal working of ArrayList in Java.
If you want to learn more about Java then check out Jigsaw Academy’s Master Certificate In Full Stack Development – a 170 hour-long live online course. It is the first & only program on Full Stack Development with Automation and AWS Cloud. Happy learning!
Fill in the details to know more
What Is the Use of Wrapper Class in Java?
March 22, 2023
What Is Clean Coding in Java?
March 21, 2023
What Are the New Features of Java 17?
What Is File Handling in Java?
March 16, 2023
What Is Data and Time Function in Java?
March 11, 2023
Top 10 Emerging Technologies Blogs To Read In 2023
December 15, 2022
History of Java Programming Language| UNext
November 18, 2022
An Advanced Guide to Shell Scripting in Unix!
November 15, 2022
Java Tutorial For Beginners
September 28, 2022
What Is Data Abstraction?
August 24, 2022
Encapsulation In Oops With Example
August 23, 2022