Introduction To ArrayList

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.

  1. What is an ArrayList, and how is it different from a traditional Array?
  2. How to use ArrayList in Java?
  3. ArrayList Declaration
  4. ArrayList Constructors
  5. ArrayList Methods In Java
  6. ArrayList Examples In Java

1) What is an ArrayList, and how is it different from a traditional Array?

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.

2) How to use ArrayList in Java?

Let us learn how to declare ArrayList in Java. The following syntax demonstrates ArrayList declaration.

3) 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.

4) ArrayList Constructors

The following list describes the constructors that an ArrayList contains in Java.

  1. ArrayList()
  2. This constructor builds an empty ArrayList.
  3. ArrayList(Collection<? extends E> c)
  4. It builds an ArrayList initialized with Collection c’s elements.
  5. ArrayList(int capacity)
  6. It creates an ArrayList that has the specified initial capacity.

Finally, before moving on to program examples, let us discuss some methods of ArrayList in Java to use in our programs.

5) ArrayList Methods In Java

  1. void add(int index, E element)
  2. This method adds the mentioned element at the specified position in a list. Now, you know how to add elements in ArrayList in Java.
  3. void clear()
  4. It removes all elements from the list.
  5. E get(int index)
  6. It fetches the element from the mentioned list position.
  7. boolean contains(Object o)
  8. This function returns true if the list contains the mentioned element.
  9. boolean remove(Object o)
  10. It removes the first occurrence of the particular element.
  11. int size()
  12. This method returns the number of elements that the list contains.

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.

6) ArrayList Examples In Java

In this article section, we will discuss a few ArrayList programs in Java. Let’s learn about ArrayList implementation in Java.

Program To Create, Manipulate, And Display ArrayList

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 

System.out.println(arrli); 

//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) + ” “); 

Output

[1, 2, 3, 4, 5]

[1, 2, 3, 5]

1 2 3 5

Program To Compare Two ArrayLists

Through this code, we learn how to compare two ArrayList in Java?

import java.util.*;  

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

}  

}  

Output

[Apple, Pears, Guava, Mango]

[Apple, Pears, Guava, Mango]

true

false

Program To Sort ArrayLists

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.

Program To Sort ArrayList In Ascending Order

import java.util.*;   

public class SortArrayListExample1  

{   

public static void main(String args[])   

{   

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

}   

}  

Output

Before Sorting: [Volkswagen, Toyota, Porsche, Ferrari, Mercedes-Benz, Audi, Rolls-Royce, BMW]

After Sorting: [Audi, BMW, Ferrari, Mercedes-Benz, Porsche, Rolls-Royce, Toyota, Volkswagen]

Program To Sort ArrayList In Descending Order

import java.util.*;   

public class SortArrayListExample3  

{   

public static void main(String args[])   

{   

// create object of ArrayList class  

ArrayList<String> list = new ArrayList<String>();   

// add elements to the ArrayList   

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”);  

// print the unsorted ArrayList   

System.out.println(“Before Sorting: “+ list);   

// sort ArrayList in ascending Order   

// use Collection.sort() method   

Collections.sort(list, Collections.reverseOrder());   

// print the sorted ArrayList   

System.out.println(“After Sorting: “+ list);   

}   

}  

Output

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?

Conclusion

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!

ALSO READ

SHARE
share

Are you ready to build your own career?