Java ArrayList - datacouch-io/spark-java GitHub Wiki

The ArrayList class in Java, located within the java.util package, provides a dynamic and resizable array-like structure.

In contrast to built-in arrays, ArrayLists offer the advantage of adjustability. Arrays have fixed sizes, requiring the creation of a new array to add or remove elements. In contrast, ArrayLists allow for the flexible addition and removal of elements. The syntax for using ArrayLists differs slightly:

import java.util.ArrayList; // Import the ArrayList class

ArrayList<String> cars = new ArrayList<String>(); // Create an ArrayList object

Adding Items

The ArrayList class offers numerous helpful methods. For instance, to append elements to an ArrayList, employ the add() method:

import java.util.ArrayList;

public class MyClass { 
  public static void main(String[] args) { 
    ArrayList<String> cars = new ArrayList<String>();
    cars.add("Volvo");
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Mazda");
    System.out.println(cars);
  } 
}

Accessing an Item

To access an element within an ArrayList, utilize the get() method by specifying the index number:

cars.get(0);

Note: Java follows zero-based indexing, meaning that array indexes begin at 0. Consequently, [0] denotes the first element, [1] represents the second, and so forth.

Modifying an Item

To alter an element, make use of the set() method while specifying the index number:

cars.set(0, "Opel");

Removing an Item

For removal of an element, rely on the remove() method, specifying the index number:

cars.remove(0);

To eliminate all elements within the ArrayList, apply the clear() method:

cars.clear();

Determining ArrayList Size

To determine the number of elements in an ArrayList, employ the size() method:

cars.size();

Iterating Through an ArrayList

Iterate through ArrayList elements using a for loop and specify the number of iterations with the size() method:

public class MyClass { 
  public static void main(String[] args) { 
    ArrayList<String> cars = new ArrayList<String>();
    cars.add("Volvo");
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Mazda");
    for (int i = 0; i < cars.size(); i++) {
      System.out.println(cars.get(i));
    }
  } 
}

You can also utilize the for-each loop for iterating through an ArrayList:

public class MyClass { 
  public static void main(String[] args) { 
    ArrayList<String> cars = new ArrayList<String>();
    cars.add("Volvo");
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Mazda");
    for (String i : cars) {
      System.out.println(i);
    }
  } 
}

Working with Other Types

ArrayList elements are, in fact, objects. In the previous examples, we created objects of type "String." It's important to note that, in Java, a String is an object, not a primitive type. To work with other types, such as integers, you must specify an equivalent wrapper class, like Integer. For other primitive types, use Boolean for boolean values, Character for characters, Double for doubles, and so on:

import java.util.ArrayList;

public class MyClass { 
  public static void main(String[] args) { 
    ArrayList<Integer> myNumbers = new ArrayList<Integer>();
    myNumbers.add(10);
    myNumbers.add(15);
    myNumbers.add(20);
    myNumbers.add(25);
    for (int i : myNumbers) {
      System.out.println(i);
    }
  } 
}

Sorting an ArrayList

In the java.util package, another valuable class is Collections, which includes the sort() method for arranging lists alphabetically or numerically:

import java.util.ArrayList;
import java.util.Collections;  // Import the Collections class

public class MyClass { 
  public static void main(String[] args) { 
    ArrayList<String> cars = new ArrayList<String>();
    cars.add("Volvo");
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Mazda");

    Collections.sort(cars);  // Sort cars

    for (String i : cars) {
      System.out.println(i);
    }
  } 
}
// Outputs:
// BMW
// Ford
// Mazda
// Volvo
⚠️ **GitHub.com Fallback** ⚠️