In this blog post we will first cover what is an ArrayList in Java, when to use an ArrayList and show you how to use it.
What is ArrayList in Java?
Java ArrayList is a collection that implements from the List interface. It lets you add various types of objects into it and also remove them. In addition, it offers various methods to access and manipulate the objects it holds. You can see the full list of all the available methods in its’Ā documentation.
ArrayList in Java is an ordered list. It means that first object inserted into the list can be found under index 0. The second object is under index 1 and so on.
Java ArrayList has the ability to dynamically grow and shrink its sizeĀ (also known as capacity). It is basically a resizableĀ array. When you first initialise an ArrayList in Java then it is empty. As you start adding objects to it, its’ size grows automatically. Therefore, you never have to worry if there is enough space to fit all the needed objects into the list. In addition, ArrayList can shrink its’ size when objects get removed.
To point out, Java ArrayList is notĀ synchronized. In case there are multiple threads reading from/writing to the ArrayList, you might get some unexpected results.
When to use Java ArrayList?
Java ArrayList could be used when there is a need to resize a collection on the fly. For example, when you don’t know how many objects could potentially be added to the collection. When using ArrayList in Java, you never need to worry about the initial size of the list, about adding more space to it or shrinking its’ size.
Furthermore, you could use ArrayList when keeping order of the objects in a collection is important. For example you might need it to display them somewhere in the front end.
Also, ArrayList would be used when you need to retrieve objects directly from a collection by using their index.
How to use ArrayList in Java?
ArrayList in Java is simple to use. To create an ArrayList we can initialise an empty list or set it with anĀ initial size. Another option is to create an ArrayList with objects from another collection.
Now, lets take a look at some examples of how to create an ArrayList in Java and show some possible usages of it.
First, lets create an empty list of animals and add some animals into it. Then, we will print out how many animals are in the list.
// Creating an empty list of animals List<String> animals = new ArrayList<>(); // Add animals to the list animals.add("dog"); animals.add("rat"); animals.add("cat"); // Display how many animals we have in the list System.out.println("Number of animals in the list: " + animals.size());
The output is the following:
Number of animals in the list: 3
Now, lets try to retrieve the first animal from the list:
String firstAnimal = animals.get(0); System.out.println("First animal in the list: " + firstAnimal);
The output:
First animal in the list: dog
We can also iterate over the list to display all the animals using foreach:
for (String animal : animals) { System.out.println(animal); }
… or the Java 8 way:
// Display all animals in the list (using Java 8) animals.forEach(animal -> System.out.println(animal));
The output of the both:
dog rat cat
Now lets see how to add another list of animals into the already existing list:
// Create a list with another list of animals List<String> otherAnimals = new ArrayList<>( Arrays.asList("monkey", "sebra") ); // Add the objects of otherAnimals to the animals list and display it animals.addAll(otherAnimals); animals.forEach(animal -> System.out.println(animal));
The output:
dog rat cat monkey sebra
As a final example lets remove the cat from the list:
// Display if list contains a cat System.out.println("(Before remove) List contains a cat: " + animals.contains("cat")); // Remove a cat from the list animals.remove("cat"); // Display if list contains a cat System.out.println("(After remove) List contains a cat: " + animals.contains("cat"));
The output:
(Before remove) List contains a cat: true (After remove) List contains a cat: false
Conclusion
ArrayList in Java is an unsynchronized and ordered collection. Its’ size grows dynamically when objects get added to it and shrinks when they get removed. Therefore, you never have to worry if there is enough space allocated for the objects.
Be the first to reply