ArrayList and ArrayList methods

Lab Objective

In this lab, we will demonstrate how to declare and initialize ArrayList, and how to manipulate ArrayList using built-in methods. By the end of this lab, learners will be able to use ArrayList and arraylist methods.

Introduction

Key Points about ArrayList in Java:

  • An ArrayList is a resizable array, also called a dynamic array. It grows its size to accommodate new elements, and shrinks its size when the elements are removed.

  • An ArrayList internally uses an array to store the elements. Just like arrays, it allows you to retrieve the elements by index.

  • A Java ArrayList allows for duplicate and null values.

  • A Java ArrayList is an ordered collection, and maintains the insertion order of the elements.

  • You cannot create an ArrayList of primitive types such as int, char, etc.; you need to use boxed types such as Integer, Character, Boolean, etc.

  • A Java ArrayList is not synchronized. If multiple threads try to modify an ArrayList simultaneously, the final outcome will be non-deterministic. You must explicitly synchronize access to an ArrayList if multiple threads are going to modify it.

Instructions:

Example 1: Creating an ArrayList and Adding New Elements

This example shows:

  • How to create an ArrayList using the ArrayList() constructor.

  • How to add new elements to an ArrayList using the add() method.

Create a class named “CreateArrayListExample” and add the code below:

Output

Example 2: Creating an ArrayList From Another Collection

This example shows:

  • How to create an ArrayList from another collection using the ArrayList(Collection c) constructor.

  • How to add all of the elements from an existing collection to the new ArrayList using the addAll() method.

Create a new class named “CreateArrayListFromCollectionExample” and add the code below in that class.

Output

Example 3: Accessing Elements from an ArrayList

This example shows:

  • The use of the isEmpty() method to check if an ArrayList is empty.

  • The use of the size() method to find the size of an ArrayList.

  • The use of the get() method to access an element at a particular index in an ArrayList.

  • The use of the set() method to modify the element at a particular index in an ArrayList.

Create a new class named “CreateArrayListFromCollectionExample” and add the code below in that class.

Output

Example 4: Removing Elements from an ArrayList

This example shows:

  • How to remove an element at a given index in an ArrayList | remove(int index).

  • How to remove an element from an ArrayList | remove(Object o).

  • How to remove all of the elements from an ArrayList that exist in a given collection | removeAll().

  • How to remove all of the elements matching a given predicate | removeIf().

  • How to clear an ArrayList | clear().

Create a new class named “RemoveElementsFromArrayList” and add the code below in that class.

Output

Example 5: Iterating over an ArrayList

The following example shows how to iterate over an ArrayList using:

  • iterator().

  • iterator() and forEachRemaining() method.

  • listIterator().

  • Simple for loop.

  • Enhanced for loop with index.

Create a new class named “IterateOverArrayList” and add the code below in that class.

Output

import java.util.ArrayList; import java.util.List;

public class SearchElementsInArrayListExample { public static void main(String[] args) { List<String> names = new ArrayList<>(); names.add("John"); names.add("Alice"); names.add("Bob"); names.add("Steve"); names.add("John"); names.add("Steve"); names.add("Maria");

}

Last updated