Remove last element from an array in Java
This post will discuss how to remove the last element from an array in Java.
Since the size of an array cannot be changed in Java, you cannot remove any elements from it. However, you can create a new array, and then copy all the elements from the original array into the new array, except the last. There are several ways to do that:
1. Using Arrays.copyOf() method
The simplest solution is to remove the last element from an array is to use the Arrays.copyOf() method to copy the contents of the original array into a new array of one less size. The Arrays class provides several overloaded versions of the copyOf() method for all primitive types.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Arrays; public class Main { public static int[] removeLastElement(int[] arr) { return Arrays.copyOf(arr, arr.length - 1); } public static void main(String[] args) { int[] arr = {5, 3, 4, 7, 6, 8}; arr = removeLastElement(arr); System.out.println(Arrays.toString(arr)); } } |
Output:
[5, 3, 4, 7, 6]
You can also use the Arrays.copyOfRange() method to copy the specified range of an array into a new array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Arrays; public class Main { public static int[] removeLastElement(int[] arr) { return Arrays.copyOfRange(arr, 0, arr.length - 1); } public static void main(String[] args) { int[] arr = {5, 3, 4, 7, 6, 8}; arr = removeLastElement(arr); System.out.println(Arrays.toString(arr)); } } |
Output:
[5, 3, 4, 7, 6]
2. Using System.arraycopy() method
Another viable alternative is to leverage the System.arraycopy() method, which copies the specified range from the specified source array to the specified position in the destination array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Arrays; public class Main { public static int[] removeLastElement(int[] arr) { int[] result = new int[arr.length - 1]; System.arraycopy(arr, 0, result, 0, arr.length - 1); return result; } public static void main(String[] args) { int[] arr = {5, 3, 4, 7, 6, 8}; arr = removeLastElement(arr); System.out.println(Arrays.toString(arr)); } } |
Output:
[5, 3, 4, 7, 6]
3. Using IntStream.range() method
In Java 8, you can use IntStream.range() to generate a sequential ordered IntStream between two specified indexes. Here’s complete usage of this method to remove the last element:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Arrays; import java.util.stream.IntStream; public class Main { public static int[] removeLastElement(int[] arr) { return IntStream.range(0, arr.length-1) .map(i -> arr[i]) .toArray(); } public static void main(String[] args) { int[] arr = {5, 3, 4, 7, 6, 8}; arr = removeLastElement(arr); System.out.println(Arrays.toString(arr)); } } |
Output:
[5, 3, 4, 7, 6]
That’s all about removing the last element from an array in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)