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:58

See code below

ArrayList<String> a = new ArrayList<>(Arrays.asList(strings));
a.remove(i);
strings = new String[a.size()];
a.toArray(strings);
查看更多
登录 后发表回答