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
For other primitive types, use: Boolean for boolean, Character for char, Double for double, etc: Create an ArrayList to store numbers (add elements of type Integer): import java.util.ArrayList; public class Main { public static void main(String[] ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › ArrayList.html
ArrayList (Java Platform SE 8 )
April 21, 2026 - Java™ Platform Standard Ed. 8 ... public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable
🌐
CodeHS
codehs.com › tutorial › 13901
Tutorial: ArrayLists in Java | CodeHS
Learn how to create and use ArrayLists in your programs!
🌐
GeeksforGeeks
geeksforgeeks.org › java › arraylist-in-java
ArrayList in Java - GeeksforGeeks
ArrayList is not thread-safe. To make it thread-safe, we must wrap it manually using Collections.synchronizedList(). Java · import java.util.ArrayList; class Main { public static void main (String[] args) { // Creating an ArrayList ArrayList<Integer> a = new ArrayList<>(); // Adding Element in ArrayList a.add(1); a.add(2); a.add(3); // Printing ArrayList System.out.println(a); } } Output ·
Published   May 12, 2026
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.
}
Find elsewhere
🌐
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 ways to create an ArrayList: Default Constructor: ArrayList<String> list1 = new ArrayList<>(); import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> arrayList = new ArrayList<>(); ...
🌐
Runestone Academy
runestone.academy › ns › books › published › csawesome › Unit7-ArrayList › topic-7-1-arraylist-basics.html
7.1. Intro to ArrayLists — CSAwesome v1
The full name of ArrayList is thus java.util.ArrayList but rather than type that out all the time, in any class where we want to use ArrayList we will usually import it with an import statement.
🌐
IONOS
ionos.com › digital guide › websites › web development › java arraylist
How to use Java ArrayList - IONOS
November 3, 2023 - Op­er­a­tions such as adding or removing elements are not performed on Ar­rayLists with Java operators, but via pre­de­fined methods. We’ll show you the most common ArrayList methods below. After creating the ArrayList “colors” (String type), we’ll add various elements using the .add() method. import java.util.ArrayList; class Main { public static void main(String[] args){ ArrayList<String> colors = new ArrayList<>(); colors.add("blue"); colors.add("red"); colors.add("green"); System.out.println("ArrayList: " + colors); } }Java
🌐
Career Karma
careerkarma.com › blog › java › arraylist java: a beginner’s guide
ArrayList Java: A Beginner’s Guide | Career Karma
December 1, 2023 - Setting one up is quite different to how you’d declare an array, because it uses the Java List interface. Open up a Java file and paste in the following code into your main class: import java.util.ArrayList; ArrayList<String> songs = new ...
🌐
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 ...
🌐
DataCamp
datacamp.com › doc › java › arraylist
Java ArrayList
The add() method is used to insert elements, remove() deletes an element, and get() retrieves an element by its index. import java.util.ArrayList; public class IterateArrayListExample { public static void main(String[] args) { ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(10); ...
🌐
Reddit
reddit.com › r/androidstudio › way to "import" an arraylist so it can be called in multiple .java files?
r/AndroidStudio on Reddit: Way to "import" an ArrayList so it can be called in multiple .java files?
October 30, 2022 -

I have an ArrayList called EmployeeList that I have the code for creation set up in a .java file called addEmployeeActivity.java, but I also need to refer to it in my Main Activity. Is there a way to set up my code so that two copies of the same ArrayList exist in both .java files?

Would copying the code from addEmployeeActivity.java to MainActivity.java suffice?

🌐
DEV Community
dev.to › jps27cse › array-list-java-collections-1m76
ArrayList (Java Collections) - DEV Community
March 14, 2022 - import java.util.*; public class Array_List { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); // Creating arrayList Scanner scanner = new Scanner(System.in); System.out.println("Enter the Lenght: "); ...
🌐
Java Programming
java-programming.mooc.fi › part-3 › 2-lists
Lists - Java Programming
For an ArrayList to be used, it first needs be imported into the program. This is achieved by including the command import java.util.ArrayList; at the top of the program.
🌐
Coderanch
coderanch.com › t › 661245 › java › set-arraylist
Is this the best way to set up an arraylist? (Beginning Java forum at Coderanch)
January 25, 2016 - JavaRanch-FAQ HowToAskQuestion... ... When you use a Collection type you should always specify the type of the Collection. List<Integer> myList = new ArrayList<Integer>();...
🌐
Programiz
programiz.com › java-programming › arraylist
Java ArrayList (With Examples)
It is because we cannot use primitive types while creating an arraylist. Instead, we have to use the corresponding wrapper classes. Here, Integer is the corresponding wrapper class of int. To learn more, visit the Java wrapper class. import java.util.ArrayList; class Main { public static void main(String[] args){
🌐
Java Journey
java-journey.com › 2026 › 01 › 13 › java-collections-framework-efficient-data-management-in-java
Java Collections Framework: Efficient Data Management in Java – Java Journey
March 30, 2026 - In Java programming, handling groups of objects efficiently is a common requirement. The Java Collections Framework (JCF) provides a set of interfaces, classes, and algorithms to store, retrieve, and manipulate data. By using collections, developers can handle lists, sets, maps, and queues ...