public static String[] removeString (String[] original) {
String[] newString;
List<String> list = new ArrayList<String>(Arrays.asList(original));
list.remove(0);
newString = list.toArray(original);
return newString;
}
I’m trying to use the above code to remove the first string from an array of strings; however, it seems that although I do succeed in removing the first string from the array, I also made the last string in the array null. How can I make the array itself shorter?
Change your last-but-one line to:
The
toArray(..)method takes as an argument a list which it tries to fill with the list data. You are passing a list of length 3, so the last element stays empty. With my suggestion you create a new array with the new size of the list.As a sidenote: I’d advice that you revisit your naming conventions. According to the recommended naming conventions your method and variable names should start with a lower-case letter
Update: but you’d better use
Arrays.copyOfRange(..)as suggested by others.