从数组中删除元素(Java)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/642897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:18:30  来源:igfitidea点击:

Removing an element from an Array (Java)

javaarrayselement

提问by Tobias

Is there any fast (and nice looking) way to remove an element from an array in Java?

有没有什么快速(而且好看)的方法可以从 Java 中的数组中删除一个元素?

采纳答案by Peter Lawrey

You could use commons lang's ArrayUtils.

您可以使用 commons lang 的 ArrayUtils。

array = ArrayUtils.removeElement(array, element)

commons.apache.org library:Javadocs

commons.apache.org 库:Javadocs

回答by Vlad Gudim

You can't remove an element from the basic Java array. Take a look at various Collections and ArrayList instead.

您不能从基本 Java 数组中删除元素。来看看各种 Collections 和 ArrayList。

回答by Geo

Sure, create another array :)

当然,创建另一个数组:)

回答by jelovirt

Nice looking solution would be to use a List instead of array in the first place.

看起来不错的解决方案是首先使用列表而不是数组。

List.remove(index)

If you haveto use arrays, two calls to System.arraycopywill most likely be the fastest.

如果您必须使用数组,那么两次调用System.arraycopy很可能是最快的。

Foo[] result = new Foo[source.length - 1];
System.arraycopy(source, 0, result, 0, index);
if (source.length != index) {
    System.arraycopy(source, index + 1, result, index, source.length - index - 1);
}

(Arrays.asListis also a good candidate for working with arrays, but it doesn't seem to support remove.)

Arrays.asList也是处理数组的一个很好的候选者,但它似乎不支持remove。)

回答by Andreas Grech

Use an ArrayList:

使用ArrayList

alist.remove(1); //removes the element at position 1

回答by Martin K.

I hope you use the java collection / java commons collections!

希望你使用java集合/java commons集合!

With an java.util.ArrayList you can do things like the following:

使用 java.util.ArrayList 您可以执行以下操作:

yourArrayList.remove(someObject);

yourArrayList.add(someObject);

回答by Romain Linsolas

Copyyour original array into another array, without the element to be removed.

原始数组复制到另一个数组中,不删除要删除的元素。

A simplier way to do that is to use a List, Set... and use the remove() method.

一种更简单的方法是使用 List、Set... 并使用 remove() 方法。

回答by palindrom

Swap the item to be removed with the last item, if resizing the array down is not an interest.

如果对调整数组大小不感兴趣,则将要删除的项目与最后一个项目交换。

回答by Tobias

okay, thx a lot now i use sth like this:

好的,谢谢,现在我经常使用这样的东西:

public static String[] removeElements(String[] input, String deleteMe) {
    if (input != null) {
        List<String> list = new ArrayList<String>(Arrays.asList(input));
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).equals(deleteMe)) {
                list.remove(i);
            }
        }
        return list.toArray(new String[0]);
    } else {
        return new String[0];
    }
}

回答by Bill K

The best choice would be to use a collection, but if that is out for some reason, use arraycopy. You can use it to copy from and to the same array at a slightly different offset.

最好的选择是使用集合,但如果由于某种原因无法使用,请使用arraycopy. 您可以使用它以稍微不同的偏移量从同一个数组复制或复制到同一个数组。

For example:

例如:

public void removeElement(Object[] arr, int removedIdx) {
    System.arraycopy(arr, removedIdx + 1, arr, removedIdx, arr.length - 1 - removedIdx);
}


Edit in response to comment:

编辑以回应评论:

It's not another good way, it's really the only acceptable way--any tools that allow this functionality (like Java.ArrayList or the apache utils) will use this method under the covers. Also, you REALLY should be using ArrayList (or linked list if you delete from the middle a lot) so this shouldn't even be an issue unless you are doing it as homework.

这不是另一种好方法,它确实是唯一可接受的方法——任何允许使用此功能的工具(如 Java.ArrayList 或 apache utils)都将在幕后使用此方法。此外,您真的应该使用 ArrayList(如果您从中间删除很多,则使用链表)所以这甚至不应该是一个问题,除非您将其作为家庭作业。

To allocate a collection (creates a new array), then delete an element (which the collection will do using arraycopy) then call toArray on it (creates a SECOND new array) for every delete brings us to the point where it's not an optimizing issue, it's criminally bad programming.

分配一个集合(创建一个新数组),然后删除一个元素(该集合将使用 arraycopy 来完成),然后每次删除时调用它的 toArray(创建第二个新数组),这将我们带到了它不是优化问题的地步,这是非常糟糕的编程。

Suppose you had an array taking up, say, 100mb of ram. Now you want to iterate over it and delete 20 elements.

假设您有一个阵列占用了 100 mb 的内存。现在您要遍历它并删除 20 个元素。

Give it a try...

试一试...

I know you ASSUME that it's not going to be that big, or that if you were deleting that many at once you'd code it differently, but I've fixed an awful lot of code where someone made assumptions like that.

我知道你假设它不会那么大,或者如果你一次删除那么多你会用不同的方式编码,但我已经修复了很多有人做出这样假设的代码。