I have a String[]
that has at least 2 elements.
I want to create a new String[]
that has elements 1 through 开发者_C百科the rest of them. So.. basically, just skipping the first one.
Can this be done in one line? easily?
Use copyOfRange
, available since Java 1.6:
Arrays.copyOfRange(array, 1, array.length);
Alternatives include:
ArrayUtils.subarray(array, 1, array.length)
from Apache commons-langSystem.arraycopy(...)
- rather unfriendly with the long param list.
String[] subset = Arrays.copyOfRange(originalArray, 1, originalArray.length);
See Also:
- java.util.Arrays
Stream API could be used too:
String[] array = {"A", "B"};
Arrays.stream(array).skip(1).toArray(String[]::new);
However, the answer from Bozho should be preferred.
精彩评论