How do I remove objects from an array in Java?

2018-12-31 05:23发布

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] = "cc";
foo[2] = "a";
foo[3] = "dd";

What do I have to do to delete/remove all the strings/objects equal to "a" in the array?

19条回答
何处买醉
2楼-- · 2018-12-31 05:50

It depends on what you mean by "remove"? An array is a fixed size construct - you can't change the number of elements in it. So you can either a) create a new, shorter, array without the elements you don't want or b) assign the entries you don't want to something that indicates their 'empty' status; usually null if you are not working with primitives.

In the first case create a List from the array, remove the elements, and create a new array from the list. If performance is important iterate over the array assigning any elements that shouldn't be removed to a list, and then create a new array from the list. In the second case simply go through and assign null to the array entries.

查看更多
孤独寂梦人
3楼-- · 2018-12-31 05:52

I realise this is a very old post, but some of the answers here helped me out, so here's my tuppence' ha'penny's worth!

I struggled getting this to work for quite a while before before twigging that the array that I'm writing back into needed to be resized, unless the changes made to the ArrayList leave the list size unchanged.

If the ArrayList that you're modifying ends up with greater or fewer elements than it started with, the line List.toArray() will cause an exception, so you need something like List.toArray(new String[] {}) or List.toArray(new String[0]) in order to create an array with the new (correct) size.

Sounds obvious now that I know it. Not so obvious to an Android/Java newbie who's getting to grips with new and unfamiliar code constructs and not obvious from some of the earlier posts here, so just wanted to make this point really clear for anybody else scratching their heads for hours like I was!

查看更多
刘海飞了
4楼-- · 2018-12-31 05:52

Arrgh, I can't get the code to show up correctly. Sorry, I got it working. Sorry again, I don't think I read the question properly.

String  foo[] = {"a","cc","a","dd"},
remove = "a";
boolean gaps[] = new boolean[foo.length];
int newlength = 0;

for (int c = 0; c<foo.length; c++)
{
    if (foo[c].equals(remove))
    {
        gaps[c] = true;
        newlength++;
    }
    else 
        gaps[c] = false;

    System.out.println(foo[c]);
}

String newString[] = new String[newlength];

System.out.println("");

for (int c1=0, c2=0; c1<foo.length; c1++)
{
    if (!gaps[c1])
    {
        newString[c2] = foo[c1];
        System.out.println(newString[c2]);
        c2++;
    }
}
查看更多
梦该遗忘
5楼-- · 2018-12-31 05:53

Initial array

   int[] array = {5,6,51,4,3,2};

if you want remove 51 that is index 2, use following

 for(int i = 2; i < array.length -1; i++){
    array[i] = array[i + 1];
  }
查看更多
残风、尘缘若梦
6楼-- · 2018-12-31 05:56

Use:

list.removeAll(...);
//post what char you need in the ... section
查看更多
裙下三千臣
7楼-- · 2018-12-31 05:56

Assign null to the array locations.

查看更多
登录 后发表回答