Things to Know About ArrayLists

DZone 's Guide to

Things to Know About ArrayLists

Want to make your Arrays more dynamic? Get a glimpse of Java's ArrayLists, including a practical demonstration and even a video showing it in action.

· Java Zone ·
Free Resource

The ArrayList is one of the more interesting things a Java programmer can learn. If you have experience coding in Java, you should know what how to use Array or List. However, if you are new to Java, things might not be as easy.

So, what is Java ArrayList?

ArrayList is a dynamic Array that enables a programmer to store any number of items. Sound confusing? If yes, then we first need to understand static and dynamic concepts in computer science.

Image title

Static and Dynamic Data Structure

Data structures like Array are either static or dynamic. For example, an array can be used to store a group of elements. Generally, arrays are static in nature and hence will not allow you to add a new element. But arrays can also be set to dynamic, meaning you can alter its length and add new elements on the fly.

In Java, you can declare an array by using the following code:

Int[] Arr; //declaring array

Arr = new int[5]; //initializing an array with the size of 5.

Arr can now store 5 elements. You cannot add elements to the Arr array, making it static in nature.

So, how do you tackle the problem of creating a dynamic array? Java ArrayList does the job!


ArrayList expands the List interface, helping you take full advantage of it. Using the Array data structure can be good for some situations, but ArrayList provides better control over the data used in the program and lets you easily expand on it.

ArrayList improves on how an array works. As a programmer, the flexibility of adding new elements can be used extensively in different scenarios. ArrayList can also be useful in managing program memory consumption, as unnecessary elements are discarded when they are not needed. This improved memory consumption is great for real-time applications.

However, it should be noted that ArrayList should never be used for information that you don’t want to change. For example, if you are storing the number of months, i.e., 1 to 12, it is wise to use an array that provides static behavior.

Importing the Right Package

To make proper use of the ArrayList, you first need to import the package from the java.util library.

import java.util.ArrayList;

Once imported, all you need to do is create a new instance by using the following line of code:

ArrayList myList = new ArrayList();

This will create a new ArrayList of the name myList.


After we have created the ArrayList, we can use different methods on it. Let’s go through some of them.

add(object A)





As you can see, we have used the add() method to add new elements to the ArrayList myList. However, we need to understand that all of them are objects and not pure data types. The first three elements are string objects and the fourth one is an integer object. If you want to add an element at a certain place, you need to use add method with passing two arguments.

add(int index, Object A) -- the first argument is the index and the 2nd argument is the object itself.

get(int index)

With the get() method, you can easily retrieve information with the help of an index. All you need pass is the index value and the element value will be retrieved.


⇒ five. //ArrayList index starts from 0.

remove(Object A)

With the help of remove(), you can remove elements. The remove method takes index as an argument and will help you manage program resources well.

myList.remove(0); //this will remove “new-item” string object.

There are many other methods that you can use on ArrayList. Some of them are listed below.

  • set(int index, Object A)

  • int indexOf(Object A)

You can find the whole list here.

Iterating Over the ArrayList

Is there a way to iterate over ArrayList? If yes, how to do so? You can use Iterator() object. All you need to do is import the Iterator class from the java.util library by using the following line of code.

import java.util.Iterator;

As mentioned earlier, the Iterator class has some methods that you can utilize to iterate over ArrayList.

hasNext() is the perfect method to work with ArrayList. All you need to run a loop and check if it returns true or false.

First, you need to attach the ArrayList to the iterator object.

Iterator ity = myList.iterator();

Now, we can use the “ity” iterator object.

while(ity.hasNext()) {

The next() method enables to go through all the elements in the list.

A quick example of how it looks like.

import java.util.*;

public class LearnArrayList {
   public static void main(String args[]) {
    ArrayList<String> myList= new ArrayList<String>();

/*Adding new elements to the myList*/

/* Quick way of displaying all the arraylist items */
System.out.println("Currently the array list has following elements:"+myList);

/*Removing elements*/

/*Adding elements using index*/
myList.add(0, "C++");
myList.add(1, "C");

System.out.println("The whole List is as follows:"+myList);

/*Removing element using index*/

System.out.println("Current array list is:"+myList);


We hope that you now have a better idea on how to make proper use of Java ArrayList. If you are still feeling confused, then you can watch typedeph, a software engineer from the US, work on an ArrayList datastructure.

Want to add something interesting to the story? Feel free to comment below!

arraylist, data structures, dynamic, java

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}