The following snippet gives an example that shows how to get an element from a List at a specified index, and also how to use the advanced for-each loop to iterate through all elements:

    import java.util.*;

    //...

    List<String> list = new ArrayList<String>();
    list.add("Hello!");
    list.add("How are you?");

    System.out.println(list.get(0)); // prints "Hello!"

    for (String s : list) {
        System.out.println(s);
    } // prints "Hello!", "How are you?"

Note the following:

  • Generic List<String> and ArrayList<String> types are used instead of raw ArrayList type.
  • Variable names starts with lowercase
  • list is declared as List<String>, i.e. the interface type instead of implementation type ArrayList<String>.

References

API:

  • Java Collections Framework tutorial
  • class ArrayList<E> implements List<E>
  • interface List<E>
    • E get(int index)
      • Returns the element at the specified position in this list.

Don't use raw types

  • JLS 4.8 Raw Types

    The use of raw types is allowed only as a concession to compatibility of legacy code. The use of raw types in code written after the introduction of genericity into the Java programming language is strongly discouraged. It is possible that future versions of the Java programming language will disallow the use of raw types.

  • Effective Java 2nd Edition: Item 23: Don't use raw types in new code

    If you use raw types, you lose all the safety and expressiveness benefits of generics.

Prefer interfaces to implementation classes in type declarations

  • Effective Java 2nd Edition: Item 52: Refer to objects by their interfaces

    [...] you should favor the use of interfaces rather than classes to refer to objects. If appropriate interface types exist, then parameters, return values, variables, and fields should all be declared using interface types.

Naming conventions

Variables: Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter.

Answer from polygenelubricants on Stack Overflow
🌐
W3Schools
w3schools.com › java › java_arraylist.asp
Java ArrayList
Elements in an ArrayList are actually objects. In the examples above, we created elements (objects) of type "String". Remember that a String in Java is an object (not a primitive type). To use other types, such as int, you must specify an equivalent wrapper class: Integer.
🌐
GeeksforGeeks
geeksforgeeks.org › java › arraylist-in-java
ArrayList in Java - GeeksforGeeks
Creates an empty ArrayList with default initial capacity.
Published   May 12, 2026
🌐
Programiz
programiz.com › java-programming › arraylist
Java ArrayList (With Examples)
To learn more, visit the Java ArrayList add(). How to add an element at a specified position in an ArrayList? We can also pass an index number as an additional parameter to the add() method to add an element at the specified position. For example,
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › ArrayList.html
ArrayList (Java Platform SE 8 )
April 21, 2026 - This class is a member of the Java Collections Framework. ... Constructs an empty list with the specified initial capacity. ... Constructs an empty list with an initial capacity of ten. ... Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator. ... Trims the capacity of this ArrayList instance to be the list's current size.
🌐
CodeHS
codehs.com › tutorial › 13901
Tutorial: ArrayLists in Java | CodeHS
Learn how to create and use ArrayLists in your programs!
Top answer
1 of 10
53

The following snippet gives an example that shows how to get an element from a List at a specified index, and also how to use the advanced for-each loop to iterate through all elements:

    import java.util.*;

    //...

    List<String> list = new ArrayList<String>();
    list.add("Hello!");
    list.add("How are you?");

    System.out.println(list.get(0)); // prints "Hello!"

    for (String s : list) {
        System.out.println(s);
    } // prints "Hello!", "How are you?"

Note the following:

  • Generic List<String> and ArrayList<String> types are used instead of raw ArrayList type.
  • Variable names starts with lowercase
  • list is declared as List<String>, i.e. the interface type instead of implementation type ArrayList<String>.

References

API:

  • Java Collections Framework tutorial
  • class ArrayList<E> implements List<E>
  • interface List<E>
    • E get(int index)
      • Returns the element at the specified position in this list.

Don't use raw types

  • JLS 4.8 Raw Types

    The use of raw types is allowed only as a concession to compatibility of legacy code. The use of raw types in code written after the introduction of genericity into the Java programming language is strongly discouraged. It is possible that future versions of the Java programming language will disallow the use of raw types.

  • Effective Java 2nd Edition: Item 23: Don't use raw types in new code

    If you use raw types, you lose all the safety and expressiveness benefits of generics.

Prefer interfaces to implementation classes in type declarations

  • Effective Java 2nd Edition: Item 52: Refer to objects by their interfaces

    [...] you should favor the use of interfaces rather than classes to refer to objects. If appropriate interface types exist, then parameters, return values, variables, and fields should all be declared using interface types.

Naming conventions

Variables: Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter.

2 of 10
6

A List is an ordered Collection of elements. You can add them with the add method, and retrieve them with the get(int index) method. You can also iterate over a List, remove elements, etc. Here are some basic examples of using a List:

List<String> names = new ArrayList<String>(3); // 3 because we expect the list 
    // to have 3 entries.  If we didn't know how many entries we expected, we
    // could leave this empty or use a LinkedList instead
names.add("Alice");
names.add("Bob");
names.add("Charlie");
System.out.println(names.get(2)); // prints "Charlie"
System.out.println(names); // prints the whole list
for (String name: names) {
    System.out.println(name);  // prints the names in turn.
}
🌐
Medium
medium.com › @Bharat2044 › what-is-arraylist-in-java-and-how-to-create-and-all-predefined-methods-and-different-ways-to-print-fa3cc9f86f8b
What is ArrayList in java and how to create and all predefined methods and different ways to print ArrayList element? | by Bharat | Medium
April 3, 2024 - Here are some of the most commonly ... main(String[] args) { ArrayList<String> list = new ArrayList<>(); list.add("Apple"); list.add("Banana"); System.out.println(list); // Output: [Apple, Banana] } }...
Find elsewhere
🌐
Lawrence
www2.lawrence.edu › fast › GREGGJ › CMSC150 › 062ArrayLists › ArrayLists.html
Working with ArrayLists
ArrayList<int> C = new ArrayList<int>(); // Illegal - int is not an object type · The workaround for this is that Java provides a class equivalent for every one of the primitive types. For example, there is an Integer class corresponding to the int type and a Double class corresponding to ...
🌐
Runestone Academy
runestone.academy › ns › books › published › csawesome › Unit7-ArrayList › topic-7-1-arraylist-basics.html
7.1. Intro to ArrayLists — CSAwesome v1
You do not have to import any classes that are in the java.lang package. To declare a ArrayList use ArrayList<Type> name where Type, called a type parameter is the type of objects you want to store in the ArrayList. For example a variable naming an ArrayList meant to hold Strings is declared as ArrayList<String> as shown in the code below.
🌐
CodeAhoy
codeahoy.com › java › array-list
Java ArrayList Tutorial with Examples | CodeAhoy
February 20, 2021 - ArrayLists are dynamically sized which means that developers don’t need to specify the capacity or the maximum size when declaring ArrayLists. As elements are added and removed, it grows or shrinks its size automatically. Contrast this to standard arrays in Java e.g.
🌐
Sentry
sentry.io › sentry answers › java › create arraylist from array in java
Create ArrayList from array in Java | Sentry
The easiest way to convert to an ArrayList is to use the built-in method in the Java Arrays library: Arrays.asList(array).
🌐
Stanford
web.stanford.edu › class › archive › cs › cs108 › cs108.1082 › 106a-java-handouts › HO49ArrayList.pdf pdf
CS106A, Stanford Handout #49 Fall, 2004-05 Nick Parlante ArrayList
study the Object type in more detail soon when we look at Java's "inheritance" features. For now, Object works as a generic pointer type that effectively means "any type of ... For example, suppose we have three Bear objects: momma, poppa, and baby. We create a · "bears" ArrayList and add pointers to the three bears to the ArrayList...
Top answer
1 of 16
2789

It would be simpler if you were to just declare it as a fixed-size List (but with mutable elements) - does it have to be an ArrayList?

List<String> places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");

Or if you have only one element:

List<String> places2 = Collections.singletonList("Buenos Aires");

This would mean that places2 is immutable (trying to change it in any way will cause an UnsupportedOperationException exception to be thrown).

To make a variable-size list that is a concrete ArrayList you can create an ArrayList from the fixed-size list:

ArrayList<String> places = new ArrayList<>(Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));

And import the correct package:

import java.util.Arrays;
2 of 16
2240

Actually, probably the "best" way to initialize the ArrayList is the method you wrote, as it does not need to create a new List in any way:

ArrayList<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");

The catch is that there is quite a bit of typing required to refer to that list instance.

There are alternatives, such as making an anonymous inner class with an instance initializer (also known as an "double brace initialization"):

ArrayList<String> list = new ArrayList<String>() {{
    add("A");
    add("B");
    add("C");
}};

However, I'm not too fond of that method because what you end up with is a subclass of ArrayList which has an instance initializer, and that class is created just to create one object -- that just seems like a little bit overkill to me.

What would have been nice was if the Collection Literals proposal for Project Coin was accepted (it was slated to be introduced in Java 7, but it's not likely to be part of Java 8 either.):

List<String> list = ["A", "B", "C"];

Unfortunately it won't help you here, as it will initialize an immutable List rather than an ArrayList, and furthermore, it's not available yet, if it ever will be.

🌐
GeeksforGeeks
geeksforgeeks.org › java › initialize-an-arraylist-in-java
Initialize an ArrayList in Java - GeeksforGeeks
July 11, 2025 - Below are the various methods to initialize an ArrayList in Java ... Example 1: This example demonstrates how to create an ArrayList using the add() method.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › util › ArrayList.html
ArrayList (Java SE 21 & JDK 21)
January 20, 2026 - java.util.ArrayList<E> Type Parameters: E - the type of elements in this list · All Implemented Interfaces: Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess, SequencedCollection<E> Direct Known Subclasses: AttributeList, RoleList, RoleUnresolvedList ·
🌐
Udemy
blog.udemy.com › home › it & development › software development › java arraylist example: how to use arraylists in java
Java Arraylist Example: How to Use Arraylists in Java - Udemy Blog
April 14, 2026 - Working with a Java ArrayList example means learning to declare, populate, access, and iterate over collections of data in code. This article covers array creation, element access, array initializers, for loops, and copying arrays.
🌐
Career Karma
careerkarma.com › blog › java › arraylist java: a beginner’s guide
ArrayList Java: A Beginner’s Guide | Career Karma
December 1, 2023 - You can do that using either a for loop or a for-each loop, depending on your preference. Let’s say that we want to print out all the Beatles songs in our list to the console, with each value appearing on a new line.
🌐
DataCamp
datacamp.com › doc › java › arraylist
Java ArrayList
Getting ArrayList Size: Use the size() method to find the number of elements in the ArrayList.
🌐
Quora
quora.com › In-Java-whats-the-best-way-to-create-and-initialize-an-ArrayList-Whats-the-best-way-to-do-it-in-one-line
In Java, what's the best way to create and initialize an ArrayList? What's the best way to do it in one line? - Quora
Answer (1 of 10): The obvious methods have been listed by other members, like [code ]Arrays.asList(oneString, twoString, threeString).[/code] I looked at the mature Java library called “Guava” and saw this method: newArrayList(E... elements); That seems to be the most elegant approach, ...