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
🌐
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.
🌐
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 last element of ArrayList with use of get(index) method by passing index = size - 1. Below is the implementation of the above approach: ... // Java code to find first and last element // of ArrayList import java.util.ArrayList; public ...
🌐
Baeldung
baeldung.com › home › java › java streams › how to get the last element of a stream in java?
How to Get the Last Element of a Stream in Java? | Baeldung
January 8, 2024 - Let’s create a List of string values and use its size function to determine how many elements to skip to reach the last element. Here is the example code getting the last element using skip:
🌐
Level Up Lunch
leveluplunch.com › java › examples › get-last-element-in-list
Get last element in list | Level Up Lunch
October 16, 2014 - This snippet will get the last element in a ArrayList using straight up java. 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); }
🌐
Codemia
codemia.io › knowledge-hub › path › how_to_get_the_last_value_of_an_arraylist
How to get the last value of an ArrayList
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
W3Docs
w3docs.com › java
How to get the last value of an ArrayList
You can also use the get(int index) method with a negative index to get the value at the end of the list. For example, you can use list.get(-1) to get the last value of the list.
🌐
W3Schools
w3schools.com › java › ref_linkedlist_getlast.asp
Java LinkedList getLast() Method
The getLast() method returns the last item in a list. Tip: Use the getFirst() method to get the first item in a list. ... T refers to the data type of items in the list. ... If you want to use W3Schools services as an educational institution, ...
Find elsewhere
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › util › List.html
List (Java SE 21 & JDK 21)
January 20, 2026 - Gets the last element of this collection. ... If this List is not empty, the implementation in this interface returns the result of calling get(size() - 1).
🌐
Mendix
community.mendix.com › link › spaces › microflows › questions › 98144
Get last element from list | Mendix Forum
January 22, 2020 - The Mendix Forum is the place where you can connect with Makers like you, get answers to your questions and post ideas for our product managers.
🌐
Mkyong
mkyong.com › home › java › java – get the last element of a list
Java - Get the last element of a list - Mkyong.com
October 15, 2019 - package com.mkyong.test; import java.util.Arrays; import java.util.List; public class JavaExample1 { public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); System.out.println(list.get(list.size() - 1)); System.out.println(list.get(list.size() - 2)); System.out.println(list.get(list.size() - 3)); System.out.println(list.get(list.size() - 4)); System.out.println(list.get(list.size() - 5)); } } Output · 5 4 3 2 1 · Python has reverse indexing, where -1 refer to the last element of a list, hope Java can implement it in the future release, then we can simplify the code like this: List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); System.out.println(list.get(-1)); // 5 System.out.println(list.get(-2)); // 4 System.out.println(list.get(-3)); // 3 System.out.println(list.get(-4)); // 2 System.out.println(list.get(-5)); // 1 ·
🌐
LeetCode
leetcode.com › problems › find-first-and-last-position-of-element-in-sorted-array
Find First and Last Position of Element in Sorted Array - LeetCode
Can you solve this real interview question? Find First and Last Position of Element in Sorted Array - Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value. If target is not found in the array, return [-1, -1]. You must ...
🌐
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 - Then get(list.size()-1) method to get the last element of the ArrayList. Following is the Java program to get the first and last elements from an ArrayList:
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-find-the-last-occurrence-of-an-element-in-a-java-list
How to find the last occurrence of an element in a Java List?
June 10, 2025 - In the below example, we will create a list of integers and then use a for loop to find the last occurrence of an element in that list. import java.util.ArrayList; import java.util.List; public class LastOccurrenceInList { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); list.add(3); int elementToFind = 3; int lastIndex = -1; for (int i = list.size() - 1; i >= 0; i--) { if (list.get(i).equals(elementToFind)) { lastIndex = i; break; } } System.out.println("Last occurrence of " + elementToFind + " is at index: " + lastIndex); } }
🌐
Javatechnote
javatechnote.com › home › how to get the last value of an arraylist in java.
How to get the last value of an ArrayList in java.
June 13, 2024 - If it not empty then list.size()-1 give the index position of last element. get() method use this index value and print the last value.
🌐
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 - But remember, that you need to use size() method for ArrayList, not length, length is used to get the length of an array. Find First and Last in ArrayList: ... // java program print first and last element of a List import java.util.ArrayList; import java.util.List; public class FindFirstLast { public static void getFirstLat(List<Integer> list) { // Displaying ArrayList elements System.out.println("ArrayList contains: " + list); // Logic to get the last element from ArrayList if (list != null && !list.isEmpty()) { System.out.println("First element is: " + list.get(0)); System.out.println("Last element is: " + list.get(list.size() - 1)); return; } } public static void main(String[] args) { /* Creating ArrayList of Integer and adding elements to it */ List<Integer> al = new ArrayList<Integer>(); al.add(3); al.add(1); al.add(4); al.add(5); al.add(2); getFirstLat(al); } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › find-the-last-element-of-a-stream-in-java
Find the last element of a Stream in Java - GeeksforGeeks
July 12, 2025 - Get the stream of elements in which the first element is to be returned. To get the last element, you can use the reduce() method to ignore the first element, repeatedly, till there is no first element.
🌐
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.
Top answer
1 of 6
2
Hey Jorge! I'll try to explain each part of the challenge in an easier way to understand, sometimes the wording can confuse students: For the first part of the challenge, you are wanted to set bestSport to whatever is at index 0 in the sports array. Note that you will need to do this after the bestSport variable has been initialized. For the second part of the challenge, you want to set numberOfSports to the length of the sports array. To do this, you should use the length() method off the sports array. For the final part of the challenge, you want to set lastSport to whatever is at index 0 in the sports array. To do this, you should use the numberOfSports variable. This will give you a number starting from 1, of how long the array is. As we're working in indexes, we want to start from 0 and we can do this by taking 1 away from numberOfSports. Note that in this challenge, there isn't any specific ruling and in fact, you could cheat a bit and just set the bestSport variable to "Basketball", like you originally did. This will still pass the challenge but it's best practise to try and do it the proper way ;) Hope it helps and good luck with the challenge! If you get stuck along the way or if there's something you want clarified, feel free to give me a shout :)
2 of 6
2
Oh I didn't actually check the challenge, sorry. Hmm I have no idea where that soccer came from. But with this code I passed the challenge without any problem: ```java String[] sports = { "Basketball", "Baseball", "Tennis" }; String bestSport = sports[0]; int numberOfSports = sports.length; String lastSport = sports[numberOfSports - 1]; ```
🌐
BeginnersBook
beginnersbook.com › 2014 › 10 › how-to-get-the-last-element-of-arraylist
How to get the last element of Arraylist?
September 11, 2022 - There are times when we need to get the last element of an ArrayList, this gets difficult when we don’t know the last index of the list. In this tutorial we are going to see an example to get the last element from ArrayList. 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)); } } }