DEV Community

Cover image for ArrayList In Java.
Tarun Kumar J
Tarun Kumar J

Posted on • Originally published at

ArrayList In Java.

Continuing my Blogs on Java Data Structures, The next topic is ArrayLists . So, Let's know about ArrayLists and how it essentially works.

ArrayList is another tool in Java to store a large number of values that are of the same type.
In a high-level context,

They are a dynamic-array implementation of List Interface.

So when we create an ArrayList, And we add or remove elements from the list, It automatically changes the size of the array by itself, that is what a dynamic array means.

ArrayList contains various methods for adding, removing, and retrieving elements from a List.
So let's first see how to declare an ArrayList.

import java.util.ArrayList;

public class ArrayListEx{
            public static void main(String[] args){
                ArrayList<String> names = new ArrayList<>();                     
Enter fullscreen mode Exit fullscreen mode

So in the above code, we import the ArrayList first by java.util.ArrayList and then we declare the ArrayList by ArrayList<String> names = new ArrayList<>(); which creates a list called names which is of type String.

The general syntax is:-

ArrayList<Type> list = new ArrayList<>();
Enter fullscreen mode Exit fullscreen mode

Type is basically the type of values stored in the list such as Integer, Boolean, Double, etc.

Adding, Removing, and Retrieving values from the ArrayList.

To Add value into an Arraylist, we use the add() method. Let's see it by example.

import java.util.ArrayList;

public class AddingNames{
     public static void main(String[] args){

            ArrayList<String> namesList = new ArrayList<String>();  //Creates a ArrayList of name "namesList"
            System.out.println("The List is: "+namesList);    //prints the list

              The List is: [Tarun, Jim, John]
Enter fullscreen mode Exit fullscreen mode

We can add value in the list at a particular index, That is by mentioning the index value first and then the element next in the add method. Below is an example of that:-

// Note: Using the same list as above
//So now the ArrayList contains another value at index 1.

    //The Output is : [Tarun, Jake, Jim, John]
Enter fullscreen mode Exit fullscreen mode

To retrieve a value from the ArrayList we use the get() method. To retrieve an element at index 2 in the "namesList" ArrayList we use namesList.get(2).

    System.out.println(namesList.get(2));   // Prints the element at index 2.
    // Output :  Jim
Enter fullscreen mode Exit fullscreen mode

And To remove elements from an ArrayList, we use the remove() method. We can remove an element by giving the index or by the value itself as the parameter.
Let's see an example.

    namesList.remove("Jim");  //Removing the String Jim 
    namesList.remove(1);  //Removing the value at index 1

//Output : [Tarun, John]
Enter fullscreen mode Exit fullscreen mode

Of course, these aren't the only methods in the ArrayList but they are the most used ones. There are many methods such as the clear() method, which removes all the elements from the list. contains() method, which checks if the given element is in the list.

If you wish to explore other methods, You can visit the official Documentation.

Hope you got a basic understanding of what an ArrayList is and how it works.

See you in the next article and until then, Happy Coding 🙂⚡️.

Top comments (2)

vlasales profile image
Vlastimil Pospichal
List<Type> list = new ArrayList<>();
tarunj096 profile image
Tarun Kumar J

Yes, we can instance ArrayList by List as well. 👍🏻