Java - Lists

aigle-levant · June 28, 2024

ArrayList is a handy, modern tool used in Java. Here’s how we use it…

ArrayList

Lists in Java can be created using the utility ArrayList. To use it in our program, we import it and then create an object of its class.

While creating the ArrayList object, we specify the type of elements we’d store in it along with its name.

import java.util.ArrayList;

public class Test
{
	public static void main(String[] args)
	{
		ArrayList <String> list = new ArrayList<>();
	}
}

Operations

We can perform the following operations using our ArrayList object in Java :

  • add(itemName) : Adds an items to the list
  • remove(index) : Removes the item located at index in the list
  • remove(dataType.valueOf(itemName)) : Removes the item from list
  • get(index) : Returns the item located at index in list
  • indexOf(itemName) : Returns the index of the first occurrence of item in list
  • size() : Returns the size of the list
  • contains(itemName) : Checks if a value exists in a list or not. Returns a Boolean value depending on it.

If we try to retrieve information from a non-existent place, IndexOutOfBoundsException error occurs. It often happens when we retrieve beyond the size of a list.

Remember : Index always starts from 0, not 1!

References

A reference is the value of a list variable that points to the location that contains that information.

When a method copies a list for its own usage [we do this by adding it to its parameters], it receives the references from the list’s copy, and thus, reference to the real value of a reference-type variable as well.

Using this, the method can modify the value of the original variable [here, its the list]. The list used as parameter for a method is the same list used in program to call that method.

Variable types

Variables in Java are divided into 2 categories :

  • Value type [primitive] : These hold their actual values
  • Reference type : Contain a reference to the location that contains the value[s] relating to that variable

Hence, when we create a list, ArrayList assumes that all the variables contained in it are reference types.

Iterating an ArrayList object

For loop

To iterate an ArrayList object, we could use a for-loop.

A flaw with this method is that we’ll have to keep track of the list index while iterating [plus un-needed complexity for such a simple action].

//assume we've created an ArrayList object
//of type String named brands
for (int i=0; i<brands.size();i++)
{
	System.out.println(i);
}

While loop

Or, we could even go for a while loop - an even lengthier option!

//assume we've created an ArrayList object
//of type String named brands
int i = 0;
while (i<brands.size())
{
	String name = brands.get(i);
	System.out.println(name);
	i++;
}

For-each loop

Or, we could go for an easier method by using for-each loop. It acts like a for-loop where we print each item of a list, except it doesn’t require you to use its index.

We mainly use it if we want to display every item in a list.

//assume we've created an ArrayList object
//of type String named brands
for (String i:list)
{
	System.out.println(i);
}