I have a String[]
that has at least 2 elements.
I want to create a new String[]
that has elements 1 through the rest of them. So.. basically, just skipping the first one.
Can this be done in one line? easily?
Use the System. arraycopy() Method to Get the Subset of an Array. System. arraycopy() method can copy a subset from an array with given beginning and ending indexes.
In Java, array slicing is a way to get a subarray of the given array.
slice() The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified. Basically, slice lets you select a subarray from an array.
Use copyOfRange
, available since Java 1.6:
Arrays.copyOfRange(array, 1, array.length);
Alternatives include:
ArrayUtils.subarray(array, 1, array.length)
from Apache commons-lang System.arraycopy(...)
- rather unfriendly with the long param list.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With