How to Add Buttons and Text to Your JavaFX Project
How to Combine Transitions in JavaFX
Keep Things Simple with Java Classes

Use Array Lists in Java

To create an array list in Java, you declare an ArrayList variable and call the ArrayList constructor to instantiate an ArrayList object and assign it to the variable:

ArrayList friends = new ArrayList();

You can optionally specific a capacity in the ArrayList constructor:

ArrayList friends = new ArrayList(100);

Note that the capacity is not a fixed limit. The ArrayList class automatically increases the list’s capacity whenever necessary.

You can use the generics feature to specify the type of elements the array list is allowed to contain:

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

Adding elements

You use the add method to add objects to the array list:

friends.add("Bob Mitchell");

If you specified a type when you created the array list, the objects you add via the add method must be of the correct type.

You can insert an object at a specific position in the list by listing the position in the add method:

ArrayList<String> nums = new ArrayList<String>();
nums.add(2, "Two and a half");

After these statements execute, the nums array list contains the following strings:

Two and a half

If you use the add method to insert an element at a specific index position and there is not already an object at that position, the add method throws the unchecked exception IndexOutOfBoundsException.

Accessing elements

To access a specific element in an array list, use the get method and specify the index value (beginning with zero) of the element that you want to retrieve:

for (int i = 0; i < nums.size(); i++)

Here, the size method is used to set the limit of the for loop’s index variable.

You can also use an enhanced for statement, which lets you retrieve the elements without bothering with indexes or the get method:

for (String s : nums)

Here, each String element in the nums array list is printed to the console.

To determine the index number of a particular object in an array list when you have a reference to the object, use the indexOf method:

for (String s : nums)
    int i = nums.indexOf(s);
    System.out.println(Item " + i + ": " + s);

Here, an enhanced for loop prints the index number of each string along with the string.

Updating elements

Use the set method to replace an existing object with another object within an array list. For example:

ArrayList<String> nums = new ArrayList<String>();
nums.set(0, "Uno");

Here, an array list is created with a single string whose value is One. Then, the value of the first element is replaced with the value Uno.

Deleting Elements

To remove all the elements, use the clear method:


To remove a specific element based on the index number, use the remove method:


Here, the first element in the array list is removed.

If you don’t know the index of the object you want to remove, but you have a reference to the actual object, you can pass the object to the remove method:


The removeRange method removes more than one element from an array list based on the starting and ending index numbers. This method removes all elements between the elements you specify, but not the elements you specify. Thus, removeRange(5, 8), for example, removes elements 6 and 7, but elements 5 and 8 aren’t removed.

You can also use the removeAll method to remove all the objects in one collection from another collection. A similar method, retainAll, removes all the objects that are not in another collection.

Note that the clear method and the various remove methods don’t actually delete objects; they simply remove the references to the objects from the array list. Like any other objects, the objects in a collection are deleted automatically by Java’s garbage collector after the objects are no longer being referenced by the program.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Commonly Used Java Code
How to Use Default Methods in Java
How to Use a Package Statement in Java
What Are Java Arrays?
How to Use a Conditional Operator in Java