Languages
[Edit]
EN

Java - get ArrayList item

0 points
Created by:
Dirask Community
6810

In this article, we would like to show you how to get the ArrayList element in Java.

Quick solution:

myArrayList.get(index);

Where:

  • myArrayList - ArrayList name,
  • index - index of the element to get access to.

 

Practical example

In this example, we use get() method to get the element with index 1 from the fruits ArrayList.

import java.util.ArrayList;

public class Example {

    public static void main(String[] args) {
        List<String> fruits = new ArrayList<>();

        // add elements to the ArrayList
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");

        System.out.println(fruits.get(1));  // get element with index 1 
    }
}

Output:

Banana

Java Collections - ArrayList

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join