Arraylists – Java Collections Framework

Hello guys, welcome to my new article series where I am going to discuss Java Collections Framework. To start it off, today I am going to talk about Arraylists in Java.

Why Arraylist?

You should always choose arraylists over arrays. We need to specify a fixed-length with an array. We don’t have to do that with Arraylists. Arraylists can grow or shrink according to the number of elements in the ArrayList. Arraylist class implements the List Interface in Java.

Creating an Arraylist

1. First Way
ArrayList<String> myArrayList = new ArrayList<String>();
2. Second Way
ArrayList<String> myArrayList = new ArrayList<>();

Adding Elements to the Arraylist

        // This adds the element to the end of the arraylist
        myArrayList.add("The Coding Cricketer");
        // This adds the element to the second index of the arraylist
        myArrayList.add(1, "Thisura");

So, adding Thisura to the first index of the ArrayList pushes Thenuka to the second index automatically.

Updating Elements of an Arraylist

        //Here is the current arraylist
        System.out.println(myArrayList);
        
        //Updating the first element of the arraylist
        myArrayList.set(0, "Java Arraylists");
        //After updating the arraylist
        System.out.println(myArrayList);

Here is the console output.

[The Coding Cricketer, Thisura, Thenuka]
[Java Arraylists, Thisura, Thenuka]

Removing Elements in the Arraylist

1. Removing by the value
        //Here is the current arraylist
        System.out.println(myArrayList);
        //Removing the first element of the arraylist
        myArrayList.remove("Thenuka");
        //After updating the arraylist
        System.out.println(myArrayList);

Here is the console output.

[Java Arraylists, Thisura, Thenuka]
[Java Arraylists, Thisura]
2. Removing by the index
        //Here is the current arraylist
        System.out.println(myArrayList);
        //Removing the first element of the arraylist
        myArrayList.remove(2);
        //After updating the arraylist
        System.out.println(myArrayList);

Here is the console output

[Java Arraylists, Thisura, Thenuka]
[Java Arraylists, Thisura]

Iterating through an Arraylist

        // This adds the element to the end of the arraylist
        myArrayList.add("The Coding Cricketer");
        myArrayList.add("Mahela");
        myArrayList.add("Sanga");
        myArrayList.add("Angie");
        myArrayList.add("Dilshan");
        
        //Iterating through the arraylist
        for (String element: myArrayList) {
            System.out.println(element);
        }

And the console output is down below

The Coding Cricketer
Mahela
Sanga
Angie
Dilshan

Sorting an Arraylist

We are using the Collections class in Java to do the sorting.

        // This adds the element to the end of the arraylist
        myArrayList.add("The Coding Cricketer");
        myArrayList.add("Mahela");
        myArrayList.add("Sanga");
        myArrayList.add("Angie");
        myArrayList.add("Dilshan");
        //Sorting the arraylist
        Collections.sort(myArrayList);
        //Iterating through the arraylist
        for (String element: myArrayList) {
            System.out.println(element);
        }

Here is the console output with the sorted ArrayList

Angie
Dilshan
Mahela
Sanga
The Coding Cricketer

Other Important Methods

1. Finding the value from the index
        // This adds the element to the end of the arraylist
        myArrayList.add("The Coding Cricketer");
        myArrayList.add("Mahela");
        myArrayList.add("Sanga");
        myArrayList.add("Angie");
        myArrayList.add("Dilshan");
        //Finding the value in the second index
        System.out.println(myArrayList.get(2));

This is going to print Sanga to the console.

2. Finding the index from the value
        // This adds the element to the end of the arraylist
        myArrayList.add("The Coding Cricketer");
        myArrayList.add("Mahela");
        myArrayList.add("Sanga");
        myArrayList.add("Angie");
        myArrayList.add("Dilshan");
        //Finding the index with the value
        System.out.println(myArrayList.indexOf("Angie"));

This is going to print 3 to the console.

3. Getting the size of the ArrayList
        // This adds the element to the end of the arraylist
        myArrayList.add("The Coding Cricketer");
        myArrayList.add("Mahela");
        myArrayList.add("Sanga");
        myArrayList.add("Angie");
        myArrayList.add("Dilshan");
        //Getting the size of the arraylist
        System.out.println(myArrayList.size());

This is going to print 5 to the console.

4. Checking if the ArrayList contains a certain value
        // This adds the element to the end of the arraylist
        myArrayList.add("The Coding Cricketer");
        myArrayList.add("Mahela");
        myArrayList.add("Sanga");
        myArrayList.add("Angie");
        myArrayList.add("Dilshan");
        //Returning a boolean if a certain value is in the arraylist
        System.out.println(myArrayList.contains("Mahela"));

This prints true to the console.

5. Clearing the ArrayList
        // This adds the element to the end of the arraylist
        myArrayList.add("The Coding Cricketer");
        myArrayList.add("Mahela");
        myArrayList.add("Sanga");
        myArrayList.add("Angie");
        myArrayList.add("Dilshan");
        //Printing the current arraylist
        System.out.println("Printing the current arraylist - " + myArrayList);
        //Finding the index with the value
        myArrayList.clear();
        //After clearing the arraylist
        System.out.println("After clearing the arraylist - " + myArrayList);

Here is the console output

Printing the current arraylist - [The Coding Cricketer, Mahela, Sanga, Angie, Dilshan]
After clearing the arraylist - []

These are the basic things you guys should know about ArrayLists. I hope you guys learned something new from the article. If you did, drop a like and share the article for anyone who might be interested in learning these things. Thank you for reading the article. Stay safe ✌

Published by Thisura Thenuka

I am a passionate software engineering student. But cricket is my first love ❤

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: