Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Just released, a free O’Reilly book on Reactive Microsystems: The Evolution of Microservices at Scale. Brought to you in partnership with Lightbend.

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

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.

Operations

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

add(object A)

myList.add(“new-item”);

myList.add(“placement”);

myList.add(“five”);

myList.add(5);


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.

myList.get(2)

⇒ 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()) {
System.out.println(ity.next());
}


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*/
myList.add("Ruby");
myList.add("Java");
myList.add("JavaScript");

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

/*Removing elements*/
myList.remove("Ruby");


/*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*/
myList.remove(1);

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


Conclusion

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!

Strategies and techniques for building scalable and resilient microservices to refactor a monolithic application step-by-step, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:
java ,arraylist ,dynamic ,data structures

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}