Languages
[Edit]
EN

Java - get ArrayList item

0 points
Created by:
isherwood
599

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

Alternative titles

  1. Java - access ArrayList item
  2. Java - get ArrayList element
  3. Java - access ArrayList element
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

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