Alternative for #subList() for String array?

So I modified an API (for command management) to use String[] instead of a list of Strings.

My problem is here:strings.subList(1, strings.length) So I need to change this to a different alternative that would do the same job.

What exactly could I do here?

Jon Skeet
people
quotationmark

You can't get one array which is a sliced view of another - but you can make a list view over an array, and then use sublist on that:

List<String> stringList = Arrays.asList(strings);
List<String> subList = stringList.subList(1, strings.length);

Now you won't be able to pass that to anything taking an array, of course...

So your options are:

  • Go back to using a List<String> everywhere - this would be my suggestion, as collection classes are generally more flexible than arrays
  • Use arrays for part of the code, but collection classes elsewhere
  • Copy part of the array instead (e.g. using System.arraycopy or Arrays.copyOfRange)

people

See more on this question at Stackoverflow