17

I have a string array with at least three elements. I'd like to copy the third and all subsequent elements into a new array. How can this be most efficiently achieved?

0

1 Answer 1

36
String[] arr = {"One", "Two", "Three", "Four", "Five"};

String[] arr2 = Arrays.copyOfRange(arr, 2, arr.length); 
//Note third param is exclusive
Sign up to request clarification or add additional context in comments.

1 Comment

If I'm reading the docs correctly, this also works when arr only has 2 elements, i.e. when arr2 would end up empty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.