3
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?

4
  • Near as I can tell, that code ought to work... Maybe using a reference to array list directly instead of the List interface. Commented Jan 23, 2010 at 22:33
  • That's bizarre... where'd my other comment go? scratches head Commented Jan 23, 2010 at 22:57
  • @Alcon: lost in meta? since this question was first posted there. My answer is lost there :-/ Commented Jan 23, 2010 at 23:16
  • @Carlos lol yeah... sorry, I signed up in the wrong place at first Commented Jan 24, 2010 at 0:14

3 Answers 3

4

Change your last-but-one line to:

NewString = list.toArray(new String[list.size()]);

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! That worked well. Thank you for telling me aobut the naming conventions too :)
May I ask why you're using Arrays?
ask the question below the question, thus it will more likely be seen the the author. Though, the question isn't quite relevant :)
4

You will want to use the java.util.Arrays T[] copyOfRange(T[] original, int from, int to) method.

public static String[] RemoveString (String[] Original) { 
    return Arrays.copyOfRange(original, 1, original.length);
}

2 Comments

(+1) indeed, an elegant solution
Just a note for any one who wants to use copyOfRange: this method is available since Java 1.6 onwards.
0

The problem is that, because your new array is smaller than the old one, the new array gets copied in the old one. The remaining fields are then filled with null. This is ugly but will solve your problem:

public static String[] RemoveString (String[] Original) { 
  List<String> list = new ArrayList<String>(Arrays.asList(Original)); 

  list.remove(0); 
  return list.toArray(new String[0]);
 }

edit Or do what Bozho said, his code is better.

edit 2 Changes to accomodate comment below

2 Comments

How do you know Original is longer than one? Why can't it have length 1? You code would then return an array of size 1, which is clearly incorrect. Why not pass a new String[0] to toArray()?
I feel stupid, you're absolutely right. I stopped thinking at the first solution that worked.

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.