How do I remove objects from an array in Java?

前端 未结 20 1364
Happy的楠姐
Happy的楠姐 2020-11-22 01:20

Given an array of n Objects, let\'s say it is an array of strings, and it has the following values:

foo[0] = \"a\";
foo[1]          


        
20条回答
  •  一生所求
    2020-11-22 02:00

    You can always do:

    int i, j;
    for (i = j = 0; j < foo.length; ++j)
      if (!"a".equals(foo[j])) foo[i++] = foo[j];
    foo = Arrays.copyOf(foo, i);
    

提交回复
热议问题