If you have a double array named numbers, you can use:

firstNum = numbers[0];
lastNum = numbers[numbers.length-1];

or with ArrayList

firstNum = numbers.get(0);
lastNum = numbers.get(numbers.size() - 1); 
Answer from K Richardson on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-get-last-element-in-array-in-java
How to Get Last Element in Array in Java? - GeeksforGeeks
July 23, 2025 - // Access the last element // in ... "Strawberry", "Blueberry"}; // Access the last element String l = s[s.length - 1]; // Print the last element System.out.println("" + l); } } ... Example 3: If we try to access the last element ...
Top answer
1 of 8
122

If you have a double array named numbers, you can use:

firstNum = numbers[0];
lastNum = numbers[numbers.length-1];

or with ArrayList

firstNum = numbers.get(0);
lastNum = numbers.get(numbers.size() - 1); 
2 of 8
11

In Java, you can get the first and last elements of an array using the following approaches:

1. Using Array Indexing:

To get the first element, you can access it using index 0. To get the last element, you can access it using the index array.length - 1. Here's an example:

int[] array = {1, 2, 3, 4, 5};

int firstElement = array[0];
int lastElement = array[array.length - 1];

System.out.println("First Element: " + firstElement);
System.out.println("Last Element: " + lastElement);

2. Using ArrayList (if the array is converted to ArrayList):

If you have converted the array to an ArrayList, you can use the get() method to access the first and last elements. To get the first element, use arrayList.get(0). To get the last element, use arrayList.get(arrayList.size() - 1). Here's an example:

import java.util.ArrayList;

int[] array = {1, 2, 3, 4, 5};
ArrayList<Integer> arrayList = new ArrayList<>();

// Convert the array to ArrayList
for (int i : array) {
    arrayList.add(i);
}

int firstElement = arrayList.get(0);
int lastElement = arrayList.get(arrayList.size() - 1);

System.out.println("First Element: " + firstElement);
System.out.println("Last Element: " + lastElement);

Both approaches allow you to retrieve the first and last elements of an array in Java. Choose the one that best fits your needs and the type of data structure you are working with.

🌐
Baeldung
baeldung.com › home › java › java array › get the first and the last elements from an array in java
Get the First and the Last Elements From an Array in Java | Baeldung
May 12, 2024 - Moreover, we can determine the index of the last element by subtracting one from the length property: array.length – 1. So next, let’s see an example of how to access the first and last elements from a String array:
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]; ```
🌐
LabEx
labex.io › tutorials › java-how-to-access-last-index-in-java-arrays-418023
How to access last index in Java arrays | LabEx
The most straightforward method to access the last element in a Java array is using the index array.length - 1. public class LastElementDemo { public static void main(String[] args) { int[] numbers = {10, 20, 30, 40, 50}; int lastElement = ...
🌐
Java Tutorial
javase.org › how-to-get-last-element-of-array-in-java
How to Get Last Element of Array in Java? - Java Tutorial
July 9, 2020 - Use (array length – 1) as index and access the element of the array. last_element = arr[length-1] public class Example { public static void main(String[] args) { int[] arr = {5, 8, 4, 2, 7, 6, 9}; int length = arr.length; int last_element = arr[length-1]; System.out.print(last_element); } } 9
🌐
Baeldung
baeldung.com › home › java › java array › checking if an element is the last element while iterating over an array
Checking if an Element is the Last Element While Iterating Over an Array | Baeldung
June 27, 2025 - However, arrays don’t implement the Iterable interface in Java. We know Iterator is available in Iterable or Stream instances. Therefore, we can convert the array to an Iterable or Stream to obtain an Iterator. List implements the Iterable interface. So, let’s can convert an object array to a List and get its Iterator: Iterator<String> it = Arrays.asList(MY_ARRAY).iterator(); StringBuilder sb = new StringBuilder(); while (it.hasNext()) { sb.append(it.next()); if (it.hasNext()) { sb.append("[END]"); } else { sb.append("->"); } } assertEquals(EXPECTED_RESULT, sb.toString());
Find elsewhere
🌐
Quora
quora.com › How-do-I-print-second-last-element-of-an-array-in-Java
How to print second last element of an array in Java - Quora
Indexing is zero-based: last element = length - 1, second last = length - 2. Always check length >= 2 (or document precondition) to avoid exceptions. ... Consider if an array has a value like that I mentioned below….
🌐
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 - // Java code to find first and ... list); // find first element int first = list.get(0); // find last element int last = list.get(list.size() - 1); // print first and last element of ArrayList System.out.println("\nFirst : " ...
🌐
CodeSpeedy
codespeedy.com › home › how to get the first and last element of an array in java
Get the first and last element of an array in Java - CodeSpeedy
October 4, 2019 - So, In the above code the indexing ... System.out.println("First element of an array is"+a[0]); System.out.println("Last element of an array is "+a[size-1]); } }...
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Last Element Test in Java Array - Java Code Geeks
October 14, 2024 - Let’s take a look at the below ... "orange"); for (String item : items) { if (item.equals(items.get(items.size() - 1))) { System.out.println(item + " (last element)"); } else { System.out.println(item); } } } ...
🌐
Java67
java67.com › 2021 › 12 › how-to-get-first-and-last-item-in-array.html
How to get the first and last item in an array in Java? Example Tutorial | Java67
The first element of an array is 1 The last element of an array is 5 That's all about how to find the first and last element of an array in Java.
🌐
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 - Consider a scenario where you have an ArrayList named dataList containing strings, and you want to fetch the last element from it. Let’s walk through the Java code that accomplishes this using the get() method with a direct index:
🌐
BeginnersBook
beginnersbook.com › 2014 › 10 › how-to-get-the-last-element-of-arraylist
How to get the last element of Arraylist?
September 11, 2022 - 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)); } } } Output: ArrayList contains: [Ajay, Becky, Chaitanya, Dimple, Rock] Last element is: Rock ·
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Get the First and the Last Elements From an Array in Java - Java Code Geeks
July 9, 2024 - Java array get first last: Learn how to retrieve the first and last elements from a Java array with simple code snippet.
🌐
javathinking
javathinking.com › blog › how-to-get-first-and-last-element-in-an-array-in-java
How to Get First and Last Element of an Array in Java (Practical Example Included) — javathinking.com
Last element: Use array[array.length - 1] (same checks apply). Always validate array status (null/empty) to avoid runtime exceptions like ArrayIndexOutOfBoundsException or NullPointerException.
🌐
Intellipaat
intellipaat.com › home › blog › java: get last element after split
Java: Get last element after split - Intellipaat
February 3, 2026 - The split() method in Java will give you an array of substrings. You can get the last element by accessing the last index. It works for well-formed input but throws ArrayIndexOutOfBoundsException if the input is empty. public class LastElementSplit ...