Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Java Dictionary

  1. Home
  2. Java Dictionary
  3. new ArrayList<>() / list.add() / set() / get()

new ArrayList<>() / list.add() / set() / get()

Methods for creating a variable-length list and adding, updating, and retrieving elements. Unlike arrays, ArrayList can dynamically change its size, making it widely used when the number of elements is not known in advance.

Syntax

import java.util.ArrayList;

// Creates an empty ArrayList.
new ArrayList<Type>();

// Appends an element to the end of the list.
list.add(E e);

// Inserts an element at the specified position.
list.add(int index, E e);

// Replaces the element at the specified position.
list.set(int index, E element);

// Returns the element at the specified position.
list.get(int index);

Method List

MethodDescription
new ArrayList<>()Creates an empty variable-length list. The type argument specifies the type of elements to store in the list.
add(E e)Appends an element to the end of the list. Returns true if the element was added successfully.
add(int index, E e)Inserts an element at the specified index. All subsequent elements are shifted one position to the right.
set(int index, E element)Replaces the element at the specified index with a new value. Returns the element that was previously at that position.
get(int index)Returns the element at the specified index. Indices are zero-based.

Sample Code

import java.util.ArrayList;

// Create an ArrayList and add elements.
ArrayList<String> fruits = new ArrayList<>();
fruits.add("apple");
fruits.add("banana");
fruits.add("cherry");
System.out.println(fruits); // Prints "[apple, banana, cherry]".

// Insert an element at a specific position.
fruits.add(1, "blueberry"); // Inserts at index 1.
System.out.println(fruits); // Prints "[apple, blueberry, banana, cherry]".

// Retrieve elements using get().
System.out.println(fruits.get(0)); // Prints "apple".
System.out.println(fruits.get(2)); // Prints "banana".

// Update an element using set().
String old = fruits.set(0, "avocado"); // Replaces the first element.
System.out.println(old);    // Prints the previous value: "apple".
System.out.println(fruits); // Prints "[avocado, blueberry, banana, cherry]".

// Example using a list of numbers.
ArrayList<Integer> nums = new ArrayList<>();
for (int i = 1; i <= 5; i++) {
    nums.add(i * 10);
}
System.out.println(nums); // Prints "[10, 20, 30, 40, 50]".

// Accessing an out-of-bounds index throws an exception.
// System.out.println(nums.get(10)); // Throws IndexOutOfBoundsException.

Notes

ArrayList is the most commonly used collection class in Java. Internally it is backed by an array, so appending elements (add()) and retrieving elements (get()) run in O(1) time. However, inserting an element in the middle requires shifting all subsequent elements, so it runs in O(n) time.

Primitive types (int, double, etc.) cannot be stored directly. Use their wrapper classes (Integer, Double, etc.) instead. Thanks to Java's autoboxing feature, writing nums.add(1) automatically converts the value to Integer.

To remove elements or check the list size, see remove() / clear() / size(). To search for elements, see contains() / indexOf().

If you find any errors or copyright issues, please .