ArrayList In Java.
Tarun Kumar J
Posted on September 15, 2020
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<>();
}
}
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<>();
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"
namesList.add("Tarun");
namesList.add("Jim");
namesList.add("John");
System.out.println("The List is: "+namesList); //prints the list
/*Output:
The List is: [Tarun, Jim, John]
*/
}
}
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
namesList.add(1,"Jake");
//So now the ArrayList contains another value at index 1.
System.out.println(namesList);
//The Output is : [Tarun, Jake, Jim, John]
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
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
System.out.println(namesList);
//Output : [Tarun, John]
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 🙂⚡️.
Posted on September 15, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.