Week 146 — What is an ArrayList?

Question of the Week #146
What is an ArrayList?
14 Replies
Eric McIntyre
Eric McIntyre5d ago
The JDK provides various Lists to store indexed data in memory. A List allows iterating over its elements (in order), getting and setting elements at a specified index, adding and removing elements. Lists generally allow duplicates (i.e. the same element can be present multiple times). One such List implementation is ArrayList which is based on an array that can be recreated (with a bigger length) automatically when the elements in it would exceed the length of the array. Due to that, most operations (getting/setting elements at a specific index, adding/removing elements at the end, iterating over it) are fast compared to other List implementations.
List<String> someStrings = new ArrayList<>();
someStrings.add("Hello");
someStrings.add("World");
someStrings.add(1, "beautiful");//add an element at a specific index
someStrings.set(0, "Hallo");

// print all strings
for(String str : someStrings) {
System.out.println(str);
}

// print all strings in reverse order
for(String str : someStrings.reversed()) {
System.out.println(str);
}

someStrings.removeLast();
System.out.println(someStrings);
List<String> someStrings = new ArrayList<>();
someStrings.add("Hello");
someStrings.add("World");
someStrings.add(1, "beautiful");//add an element at a specific index
someStrings.set(0, "Hallo");

// print all strings
for(String str : someStrings) {
System.out.println(str);
}

// print all strings in reverse order
for(String str : someStrings.reversed()) {
System.out.println(str);
}

someStrings.removeLast();
System.out.println(someStrings);
📖 Sample answer from dan1st
Eric McIntyre
Eric McIntyre5d ago
An ArrayList<E> is a subclass of List<E> which stores it's data in an array format. This means it has O(1) time complexity for random access, but offers more flexibility than traditional array data type in Java by allowing for efficient insertion and deletion operations. Code example:
List<Integer> numbers = new ArrayList<Integer>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
int[] numbersArray = numbers.toArray();
List<Integer> numbers = new ArrayList<Integer>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
int[] numbersArray = numbers.toArray();
Submission from pritt_0780
Eric McIntyre
Eric McIntyre5d ago
An ArrayList is sort of List that can hold different datatypes of variables. it is an array that can manage those data types as well as reshrink when needed. it can maximize and increase size by itself whenever new element is added to it
Eric McIntyre
Eric McIntyre5d ago
Good example: import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); list.add("Banana"); list.add("Apple"); list.add("Orange"); } }
Submission from the_user_joseph
Eric McIntyre
Eric McIntyre5d ago
is a way to store array sequentially way , listing an array to same sequentially in which it was created example:
List<String> nomes = new ArrayList<>();
nomes.add("ramon");
nomes.add("java");
nomes.add("discord");

nomes.remove(0);
nomes.size();

nomes.get(1);
List<String> nomes = new ArrayList<>();
nomes.add("ramon");
nomes.add("java");
nomes.add("discord");

nomes.remove(0);
nomes.size();

nomes.get(1);
this was a way to demonstrate ArrayList
Submission from ramond.s
Eric McIntyre
Eric McIntyre5d ago
An ArrayList in Java is a resizable array (a dynamic data structure) from the java.util package that allows you to store objects, automatically grow/shrink in size, and provides methods to add, remove, and access elements by index.
Submission from 1b6a_
Eric McIntyre
Eric McIntyre5d ago
ArrayList is a member of the Java collection hierarchy that can grow dynamically as more elements are added. It uses an array in its implementation, and every time it becomes full it doubles in size.
ArrayList<String> example = new ArrayList<>();

example.add("Hello");
example.add("world");

System.out.println(example.get(0)); // Hello
System.out.println(example.get(1)); // world

System.out.println(example.size()); // 2

example.remove(1);

System.out.println(example.get(1)); // IndexOutOfBounds exception
ArrayList<String> example = new ArrayList<>();

example.add("Hello");
example.add("world");

System.out.println(example.get(0)); // Hello
System.out.println(example.get(1)); // world

System.out.println(example.size()); // 2

example.remove(1);

System.out.println(example.get(1)); // IndexOutOfBounds exception
An advantage to using an array in the implementation for a list is that getting values from the list will always be a constant operation. But if you are doing a lot of adding/removing elements then a LinkedList would be a better option.
Submission from giinko0103
Eric McIntyre
Eric McIntyre5d ago
An array list is a dynamic array that doesn't have a size and allows you to add elements very smoothly (without having to manually set the indexes and assigning each element)
Eric McIntyre
Eric McIntyre5d ago
Code example is:
ArrayList<String> al = new ArrayList<>(); // Can be any type.

al.add("A string");
al.add("Another string");
ArrayList<String> al = new ArrayList<>(); // Can be any type.

al.add("A string");
al.add("Another string");
Submission from publicstaticstring
Eric McIntyre
Eric McIntyre5d ago
An arrayList is a collection of objects neatly structured in the same type. Its size can grow or shrink dynamically opposed to a normal array. It allows for indexed access and structured data.
Submission from bentekeb
Eric McIntyre
Eric McIntyre5d ago
an arraylist is a dynamic version of the list which allows you to dynamically insert elements into it while handling resizing automatically. basically a super version of the list. here is a quick example showing actually how this works
package com.example;

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

public class App {
// creating a new array list
private static List<String> list = new ArrayList<>();

public static void main(String[] args) {
System.out.println("Number of elements in the list: " + list.size());

// insert element at runtime dynamically
list.addLast("Wow im an element");

System.out.println("Number of elements in the list: " + list.size());
}
}
package com.example;

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

public class App {
// creating a new array list
private static List<String> list = new ArrayList<>();

public static void main(String[] args) {
System.out.println("Number of elements in the list: " + list.size());

// insert element at runtime dynamically
list.addLast("Wow im an element");

System.out.println("Number of elements in the list: " + list.size());
}
}
as you can see, we are able to insert different elements of the type at runtime into our list. thank you for reading
Submission from phoenix12__
Eric McIntyre
Eric McIntyre5d ago
ArrayList is a standard Java collection that represents a mutable and resizable list of elements, accessible by index. It is internally backed by a simple Object[] array, and a new one is automatically and transparently created when the current array's capacity is no longer sufficient to hold new elements. ArrayList is similar to the old (“legacy”) Vector class. The main difference is that Vector is synchronized, while ArrayList is unsynchronized. Like all Java collections, ArrayList is a generic class; therefore, you should always use it parameterized, such as ArrayList<String> or ArrayList<Integer>. The elementary usage is like this:
import java.util.ArrayList;

public class ArrayListSample {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<String>();
names.add("John");
names.add("Robert");
names.add("Mary");

System.out.println("Size: " + names.size()); // Size: 3
System.out.println(names.get(0)); // John
System.out.println(names.get(1)); // Robert
System.out.println(names.get(2)); // Mary

names.remove(0); // removes John

System.out.println("Size: " + names.size()); // Size: 2
System.out.println(names.get(0)); // Robert (now at index 0)
System.out.println(names.get(1)); // Mary (now at index 1)

names.set(0, "ROBERT");
System.out.println(names.get(0)); // ROBERT (the element 0 has been mutated)
}
}
import java.util.ArrayList;

public class ArrayListSample {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<String>();
names.add("John");
names.add("Robert");
names.add("Mary");

System.out.println("Size: " + names.size()); // Size: 3
System.out.println(names.get(0)); // John
System.out.println(names.get(1)); // Robert
System.out.println(names.get(2)); // Mary

names.remove(0); // removes John

System.out.println("Size: " + names.size()); // Size: 2
System.out.println(names.get(0)); // Robert (now at index 0)
System.out.println(names.get(1)); // Mary (now at index 1)

names.set(0, "ROBERT");
System.out.println(names.get(0)); // ROBERT (the element 0 has been mutated)
}
}
The latest official Javadoc of ArrayList is at: https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/ArrayList.html
⭐ Submission from and.bin
Eric McIntyre
Eric McIntyre5d ago
The java.util.ArrayList class is one of the original members of the Java Collections Framework. It's an implementation of java.util.List, backed by an array (the size of the backing array is the list's capacity). It is also one of the most generally useful Collection classes, and is the de facto "default" List implementation used by much Java code. As a list, it implements indexed access of its elements. It implements all optional operations of java.util.List and java.util.Collection, and allows null elements. Because it is backed by an array, operations, such as size, set, and get run in constant time. Operations that require elements to be moved within the array, such as insertion and removal, require longer. Also, if elements are added beyond the current capacity of the list, a new backing array of sufficient size must be created, and all the elements of the old array moved. Because of these characteristics, users must strike a balance between memory usage and performance. If the number of elements is known (or approximately known) at creation time, specifying the initialCapacity in the constructor can allow the ArrayList to be "tuned" for the most common usage, avoiding wasted memory and extra allocations.
Eric McIntyre
Eric McIntyre5d ago
The ArrayList is not a thread-safe structure. If multiple threads need to access the same object, use something like Collections.synchronizedList(myArrayList) to get a thread-safe wrapper around the list. Attempting to manually synchronize access to the list is error prone, and should be avoided in most cases.
⭐ Submission from dangerously_casual

Did you find this page helpful?