开发者

How to remove elements from an array in java even if we have to iterate over array or can we do it directly? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicates:

How do I remove objects from an Array in java?

Removing an element from an Array (Java)

listOfNames = new String [] {"1","2","3","4"}; //
String [] l = new String [listOfNames.length-1];
for(int i=0; i<listOfNames.length-1; i++)    //removing the first element
   l[i] = listOfNames[i+1];

开发者_C百科// can this work , Is there a better way ? to remove certain elements from an array in this case the first one .


Without a for loop :

String[] array = new String[]{"12","23","34"};
java.util.List<String> list = new ArrayList<String>(Arrays.asList(array));
list.remove(0);
String[] new_array = list.toArray(new String[0]);

Tip
If you can, stick with List, you'll have more flexibility.


String[] listOfNames = new String [] {"1","2","3","4"};
List<String> list = new ArrayList<String>(Arrays.asList(listOfNames));
list.remove(0);
String[] array = list.toArray(array);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜