Java ArrayList Get example shows how to get an element from ArrayList in Java. The example also shows how to get element with and without cast.
Java ArrayList is a part of the Java Collection framework.
How to get element from ArrayList in Java?
Java ArrayList get
method returns the element at the specified index of the ArrayList.
1 |
public E get(int index) |
This method returns the element at the specified index.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.util.ArrayList; public class ArrayListGetExample { public static void main(String[] args) { ArrayList<String> aList = new ArrayList<>(); aList.add("One"); aList.add("Two"); aList.add("Three"); /* * ArrayList index starts at 0 */ System.out.println("ArrayList first element: " + aList.get(0) ); /* * Last element of the ArrayList is * at ArrayList.size() - 1 index */ System.out.println("ArrayList last element: " + aList.get(aList.size() - 1)); } } |
Output
1 2 |
ArrayList first element: One ArrayList last element: Three |
As you can see from the output, the ArrayList index starts from 0. So the first element of the ArrayList is located at index 0. The last element of the ArrayList is located at index (total size of the ArrayList – 1) index so in the above example at index 2.
If you try to access index which is out of the range (i.e. index < 0 or index >= ArrayList size), get method throws IndexOutOfBoundsException exception.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.ArrayList; public class ArrayListGetExample { public static void main(String[] args) { ArrayList<String> aList = new ArrayList<>(); aList.add("One"); aList.add("Two"); aList.add("Three"); //this will throw IndexOutOfBoundsException //System.out.println(aList.get(-1)); //this will also throw IndexOutOfBoundsException System.out.println(aList.get(3)); } } |
Output
1 2 3 4 |
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3 at java.util.ArrayList.rangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at com.javacodeexamples.collections.arraylist.ArrayListGetExample.main(ArrayListGetExample.java:20) |
You can use for loop or enhanced for loop to get all the elements and print ArrayList elements one by one as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.ArrayList; public class ArrayListGetExample { public static void main(String[] args) { ArrayList<String> aList = new ArrayList<>(); aList.add("One"); aList.add("Two"); aList.add("Three"); for(String str : aList) System.out.println(str); for(int i = 0; i < aList.size(); i++) System.out.println(aList.get(i)); } } |
Output
1 2 3 4 5 6 |
One Two Three One Two Three |
Make sure to specify type when you declare an ArrayList. If ArrayList is declared without type, the get method returns an object of the Object class and you will have to cast it down to the appropriate type before you can use any methods of that class.
1 2 3 4 5 |
ArrayList aList = new ArrayList(); aList.add("string"); //compilation error! aList.get(0).toLowerCase(); |
You will get “The method toLowerCase() is undefined for the type Object” compilation error. That is because since we have not specified any type while declaring the ArrayList, the get method returns object of Object class. The explicit cast will be required to cast the element to the appropriate type as given below.
1 2 3 4 5 |
ArrayList aList = new ArrayList(); aList.add("string"); //Downcast to String from Object System.out.println( ((String)aList.get(0)).toLowerCase() ); |
This example is a part of the Java ArrayList tutorial with examples.
References:
1. ArrayList Javadoc
Hi
Can you do example for an ArrayList that has many objects of atype, and you need to print/access one of them and them?
Also, if you wanted just one variable in that object to print or take, from one or many of the objects.
The array list is in another Class, and it is also private.
Thanks again 🙂
Hi Meepa,
To print an object from the ArrayList, get the object using the index as shown in the example, and print it. Make sure that the Class implements the toString method, otherwise it will not print any useful details.
I hope this answers your question. Good luck and thanks.