WikiGalaxy

Personalize

Introduction to Java ArrayList

Overview:

The ArrayList class in Java is part of the java.util package and provides a resizable-array implementation of the List interface. It is widely used due to its dynamic size, ease of use, and ability to store duplicate elements.

Creating an ArrayList

Example:

To create an ArrayList, you need to instantiate it using the ArrayList class. Here is a simple example:


import java.util.ArrayList;
class Example1 {
  public static void main(String[] args) {
    ArrayList names = new ArrayList();
    names.add("Alice");
    names.add("Bob");
    System.out.println(names);
  }
}
      

Console Output:

[Alice, Bob]

Adding Elements

Example:

Elements can be added to an ArrayList using the add() method. This method appends the specified element to the end of the list.


import java.util.ArrayList;
class Example2 {
  public static void main(String[] args) {
    ArrayList numbers = new ArrayList();
    numbers.add(10);
    numbers.add(20);
    numbers.add(30);
    System.out.println(numbers);
  }
}
      

Console Output:

[10, 20, 30]

Accessing Elements

Example:

You can access elements in an ArrayList using the get() method. This method takes an index as an argument and returns the element at that position.


import java.util.ArrayList;
class Example3 {
  public static void main(String[] args) {
    ArrayList fruits = new ArrayList();
    fruits.add("Apple");
    fruits.add("Banana");
    System.out.println(fruits.get(1));
  }
}
      

Console Output:

Banana

Modifying Elements

Example:

The set() method allows you to modify an element at a specific index. It takes two arguments: the index of the element to replace and the element to be stored.


import java.util.ArrayList;
class Example4 {
  public static void main(String[] args) {
    ArrayList cars = new ArrayList();
    cars.add("Toyota");
    cars.add("Honda");
    cars.set(1, "Ford");
    System.out.println(cars);
  }
}
      

Console Output:

[Toyota, Ford]

Removing Elements

Example:

Elements can be removed from an ArrayList using the remove() method. This method can take either the index of the element to be removed or the element itself.


import java.util.ArrayList;
class Example5 {
  public static void main(String[] args) {
    ArrayList animals = new ArrayList();
    animals.add("Dog");
    animals.add("Cat");
    animals.remove("Dog");
    System.out.println(animals);
  }
}
      

Console Output:

[Cat]

Iterating Over an ArrayList

Example:

You can iterate over an ArrayList using a for-each loop. This loop simplifies the iteration process and makes the code more readable.


import java.util.ArrayList;
class Example6 {
  public static void main(String[] args) {
    ArrayList colors = new ArrayList();
    colors.add("Red");
    colors.add("Green");
    colors.add("Blue");
    for (String color : colors) {
      System.out.println(color);
    }
  }
}
      

Console Output:

Red
Green
Blue

Checking if an ArrayList Contains an Element

Example:

The contains() method checks if an ArrayList contains a specific element. It returns true if the element is found, otherwise false.


import java.util.ArrayList;
class Example7 {
  public static void main(String[] args) {
    ArrayList cities = new ArrayList();
    cities.add("New York");
    cities.add("Los Angeles");
    System.out.println(cities.contains("New York"));
  }
}
      

Console Output:

true

Getting the Size of an ArrayList

Example:

To determine the number of elements in an ArrayList, use the size() method. This method returns an integer representing the number of elements.


import java.util.ArrayList;
class Example8 {
  public static void main(String[] args) {
    ArrayList books = new ArrayList();
    books.add("1984");
    books.add("Animal Farm");
    System.out.println(books.size());
  }
}
      

Console Output:

2

Clearing an ArrayList

Example:

The clear() method removes all elements from an ArrayList, leaving it empty.


import java.util.ArrayList;
class Example9 {
  public static void main(String[] args) {
    ArrayList languages = new ArrayList();
    languages.add("Java");
    languages.add("Python");
    languages.clear();
    System.out.println(languages);
  }
}
      

Console Output:

[]

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025