ArrayList and ListIterator

An ArrayList is basically a array of objects that are managed within a List interface. This basically means that Java will do the management of the List and you just need to either add to that list/change the values/remove items.

The way that the ArrayList is defined is using the Generics syntax, since a ArrayList can be of any type.

ArrayList<String> names = new ArrayList<String>();

this will create a object called “names” that have a array of type String and to add names to the array you use the “add” method, the “add” method can either just have the ArrayList type (in this case a String) as a single parameter or a index into the ArrayList with the ArrayList type again.. e.g.

// normal add
names.add("Genux");
// adding at a set point
names.add("1","Genux at point 1");

to remove at a set point, there is a method called “remove” that will either take a index point or a class (e.g. will compare against a class within the ArrayList to see if there is anything that equals, if so remove from the ArrayList), in the example below I am just going to use the index point to remove from the ArrayList

names.remove(0);

you can also alter any of the names within the ArrayList with using the method “set”, here I am altering the index point 0 within the array to have the String value of “Genux new start”

names.set(0,"Genux new start");

The ListIterator is when you can iterate through a List (interface implemented object e.g ArrayList), to iterate through a list you basically go from one to the next to the next etc. the ArrayList object called names has a method that will return the list iterator object for the ListIterator to go through, the hasNext method within the ListIterator returns true if there is another value/object after the present one, the “next” method within the ListIterator returns a value/object of the type of ListIterator and then goes to the next value within the List, here is some code that uses the above that uses the hasNext and next methods.

ListIterator<String> arrayListIt = names.listIterator();
while (arrayListIt.hasNext())
{
	System.out.println("Names :"+ arrayListIt.next());
}

here is the source code in full, if you save as “ArrayListTutorial.java”

import java.util.ArrayList;
import java.util.ListIterator;
 
public class ArrayListTutorial {
 
	public static void main(String[] args) {
			ArrayList<String> names = new ArrayList<String>();
			// add names in a standard way, placing on the end in this case the start position = 0 
			names.add("Genux");
			// adding a set point
			names.add(1,"Genux at point 1");
 
			System.out.println("Starting point of the Array of names");
 
			// a ListIterator is a template (e.g. can use any type String/Integer etc) 
			// that will loop through a List object (in this case the ArrayList has implemented a List interface)
			ListIterator<String> arrayListIt = names.listIterator();
			while (arrayListIt.hasNext())
			{
				System.out.println("Names :"+ arrayListIt.next());
			}
 
			System.out.println("Alerting the starting name");
			// change the start of the array list e.g. point 0
			names.set(0,"Genux new start");
 
			// loop through the array list with a iterator
			arrayListIt = names.listIterator();
			while (arrayListIt.hasNext())
			{
				System.out.println("Names :"+ arrayListIt.next());
			}
 
			System.out.println("Removing index 0 from the ArrayList of names");
			names.remove(0);
			// loop through the array list with a iterator
			arrayListIt = names.listIterator();
			while (arrayListIt.hasNext())
			{
				System.out.println("Names :"+ arrayListIt.next());
			}
	}
}

and the output would be

Starting point of the Array of names
Names :Genux
Names :Genux at point 1
Alerting the starting name
Names :Genux new start
Names :Genux at point 1
Removing index 0 from the ArrayList of names
Names :Genux at point 1

to compile if you save the java code above as “ArrayListTutorial.java”

javac ArrayListTutorial.java
java ArrayListTutorial

Leave a Reply

Your email address will not be published. Required fields are marked *