The following is part of the List interface (which ArrayList implements):

E e = list.get(list.size() - 1);

E is the element type. If the list is empty, get throws an IndexOutOfBoundsException. You can find the whole API documentation here.

Answer from Johannes Schaub - litb on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ find-first-and-last-element-of-arraylist-in-java
Find first and last element of ArrayList in java - GeeksforGeeks
July 11, 2025 - Get the first element of ArrayList with use of get(index) method by passing index = 0. Get the last element of ArrayList with use of get(index) method by passing index = size - 1.
Discussions

java - Get last added element Arraylist - Stack Overflow
How can we get the last added element in ArrayList. I find this that explain how to get the last element, but is the last added element always the last element ? More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - Get last element in arraylist - Stack Overflow
In the below code, it calls an element exactly once. How can i make it print out "this is the last element in the arraylist" when everything has been removed, and there is only one element remainin... More on stackoverflow.com
๐ŸŒ stackoverflow.com
6.1.8 LAST ELEMENT IN ARRAY
can you post a cleaner version of you code? itโ€™s very hard to find any errors of it looks like this. More on reddit.com
๐ŸŒ r/codehs
4
7
January 21, 2021
Last item in c#
๐ŸŒ r/csharp
62
10
September 16, 2022
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2014 โ€บ 10 โ€บ how-to-get-the-last-element-of-arraylist
How to get the last element of Arraylist?
September 11, 2022 - import java.util.ArrayList; import java.util.List; public class ArrayListExample { public static void main(String[] args) { /* Creating ArrayList of Strings and adding * elements to it */ List<String> al = new ArrayList<String>(); al.add("Ajay"); al.add("Becky"); al.add("Chaitanya"); al.add("Dimple"); al.add("Rock"); // Displaying ArrayList elements System.out.println("ArrayList contains: "+al); // Logic to get the last element from ArrayList if (al != null && !al.isEmpty()) { System.out.println("Last element is:"); System.out.println(al.get(al.size()-1)); } } }
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ how-to-get-first-and-last-elements-from-arraylist-in-java
How to get first and last elements from ArrayList in Java?
September 1, 2025 - Therefore, if you pass 0 to this method, you can get the first element of the current ArrayList, and if you pass list.size()-1, you can get the last element.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ java โ€บ java arraylist get last element
How to Get the Last Element From an ArrayList in Java | Delft Stack
February 12, 2024 - The result, comprising the original ArrayList and the last element, is printed to the console. ... By employing the size() and get() methods, we have efficiently obtained the last element from an ArrayList.
๐ŸŒ
Codemia
codemia.io โ€บ home โ€บ knowledge hub โ€บ how to get the last value of an arraylist
How to get the last value of an ArrayList | Codemia
January 25, 2025 - Another way to get the last element is using a ListIterator. This is particularly useful when you want to traverse the list in reverse order. The ListIterator can be obtained by calling the listIterator() method on the ArrayList.
๐ŸŒ
Sabe
sabe.io โ€บ blog โ€บ java-last-element-arraylist
How to get the Last Element of an ArrayList in Java - Sabe.io
April 18, 2022 - We can do this by subtracting 1 from the size of the list, since that should return us the last element. ... JAVAimport java.util.Arrays; import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<Integer> ...
Find elsewhere
๐ŸŒ
Reactgo
reactgo.com โ€บ home โ€บ how to get the last element of an arraylist in java
How to get the last element of an ArrayList in Java | Reactgo
October 12, 2023 - To get the last element of a ArrayList in Java, we can use the list.get() method by passing its index list.size()-1.
๐ŸŒ
W3docs
w3docs.com โ€บ home โ€บ code snippets โ€บ java โ€บ how to get the last value of an arraylist
How to get the last value of an ArrayList | W3docs
To get the last value of an ArrayList in Java, you can use the size() method to get the size of the list and then access the value at the index size() - 1.
๐ŸŒ
JavaGoal
javagoal.com โ€บ home โ€บ getlast() method
getlast java - getlast() arraylist to get last element of list
June 6, 2021 - getLast() method: This getlast java method is used get the last element from LinkedList. It returns the object & get last element of list.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ get-first-and-last-elements-from-arraylist-in-java
Get first and last elements from ArrayList in Java - GeeksforGeeks
July 11, 2025 - The last element can be accessed using get(size() - 1). ... Create an ArrayList and add elements to it.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 729652 โ€บ java โ€บ element-ArrayList-QuakeEntry
How do I get the last element of an ArrayList<QuakeEntry> ? (Beginning Java forum at Coderanch)
April 25, 2020 - Write the void method OnePassBubbleSort that has 2 parameters, an ArrayList of type QuakeEntry name quakeData and an int named numSorted that represents the number of times this method has already been called on this ArrayList and thus also represents the number of the elements that are guaranteed to already be where they belong when the ArrayList is sorted by magnitude. This method makes one pass of bubble sort on the ArrayList. It should take advantage of the fact that the last numSorted elements are already in sorted order.
๐ŸŒ
tutorialpedia
tutorialpedia.org โ€บ blog โ€บ how-to-get-the-last-value-of-an-arraylist
How to Get the Last Value of an ArrayList in Java: Simple Methods Explained โ€” tutorialpedia.org
Since ArrayList uses zero-based indexing, the index of the last element is size() - 1. Use ArrayList.get(index) with this index to retrieve the last element.
๐ŸŒ
Java67
java67.com โ€บ 2015 โ€บ 06 โ€บ how-to-get-first-and-last-elements-form-ArrayList-java.html
How to get first and last elements form ArrayList in Java | Java67
It's pretty straightforward, all you need to do is call get(0) to get the first element and call get(size() - 1) to retrieve the last element. In this example, we have an ArrayList of video games, some of the super hit titles of Nintendo era, ...
๐ŸŒ
Ek
apnews.com.oye.mlga.ek.gov.ng โ€บ home โ€บ updates โ€บ how to get the last value of an arraylist
How to get the last value of an ArrayList - Viral Frenzy Report
April 19, 2026 - All you need to do is use size() to get the last value of the Arraylist. For ex. if you ArrayList of integers, then to get last value you will have to ... Remember, elements in an Arraylist can be accessed using index values.
๐ŸŒ
Level Up Lunch
leveluplunch.com โ€บ java โ€บ examples โ€บ get-last-element-in-list
Get last element in list | Level Up Lunch
October 16, 2014 - It will check if the ArrayList is empty and if the size is greater than 0 returning the last element in the collection. @Test public void get_last_element_in_list_with_java () { List<String> strings = new ArrayList<String>(); strings.add("one"); strings.add("two"); strings.add("three"); String lastElement = null; if (!strings.isEmpty()) { lastElement = strings.get(strings.size() - 1); } assertEquals("three", lastElement); }
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ collections framework โ€บ java arraylist โ€บ java arraylist.lastindexof()
Java Arraylist.lastIndexOf() with Examples - HowToDoInJava
January 13, 2023 - ArrayList<String> list = new ...gary","alex","harry")); int lastIndex = list.lastIndexOf("alex"); System.out.println(lastIndex); lastIndex = list.lastIndexOf("hello"); System.out.println(lastIndex);...