This post will discuss how to remove the first element from an array in Java.

Since the length of an array is fixed in Java, there is no standard way to remove the first element from it. However, you can create a new array containing all the original array elements except the first element. There are several ways to do that:

1. Using Arrays.copyOfRange() method

The idea is to use the Arrays.copyOfRange() method, to get a subarray of the original array. Java has overloaded versions of this method for all primitive types and objects. Its usage is demonstrated below for a primitive int array:

Download  Run Code

Output:

[3, 4, 8, 7, 1]

2. Using System.arraycopy() method

Another solution is to allocate a new array of size one less than the original array and then call the System.arraycopy() method, which copies the specified range from the original array to the new array.

Download  Run Code

Output:

[3, 4, 8, 7, 1]

3. Using IntStream.range() method

With the introduction of Stream with Java 8, you can get the sequential ordered stream between two indexes, and convert it back to an array using the toArray() method.

Download  Run Code

Output:

[3, 4, 8, 7, 1]

That’s all about removing the first element from an array in Java.