java 关于java列表删除的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5543384/
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
question on java list remove
提问by Cratylus
The method public boolean remove(Object o)
of List
removes an object from list but does not shift the elements following.Just nulls the object value.
IMHO this is an uninintuitive design choice since the size of the list before and after removal remains the same.
Is there an elegant way to get a list with the elements shifted?
从列表public boolean remove(Object o)
中List
删除一个对象但不移动后面的元素的方法。只是将对象值清空。
恕我直言,这是一个不直观的设计选择,因为删除前后列表的大小保持不变。
有没有一种优雅的方法来获取元素移动的列表?
Thanks
谢谢
回答by Joachim Sauer
No, that's not what it does. The element is removed and all indices following it are reduced by one. What makes you think it acts differently?
不,这不是它的作用。该元素被删除,其后的所有索引都减一。是什么让你认为它的行为不同?
回答by ITroubs
According to the Java API hereit sais that the remove function of List DOES shift
根据这里的Java API ,它说List的remove函数确实发生了变化
Removes the element at the specified position in this list (optional operation). Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.
移除此列表中指定位置的元素(可选操作)。将任何后续元素向左移动(从它们的索引中减去一个)。返回从列表中删除的元素。
EDIT:
编辑:
Main class:
主类:
import java.util.ArrayList;
import java.util.Iterator;
public class Main {
public static void main(String[] args) {
ArrayList<A> x = new ArrayList<A>();
A one = new A("one");
A two = new A("two");
A three = new A("three");
A four = new A("four");
A five = new A("five");
A six = new A("six");
A seven = new A("seven");
A eight = new A("eight");
A nine = new A("nine");
A ten = new A("ten");
x.add(one);
x.add(two);
x.add(three);
x.add(four);
x.add(five);
x.add(six);
x.add(seven);
x.add(eight);
x.add(nine);
x.add(ten);
for(A item:x){
System.out.println(item.getStr());
}
x.remove(four);
Iterator<A> i = x.iterator();
while(i.hasNext()){
A item = i.next();
System.out.println(item.getStr());
}
}
}
The A Class:
A类:
public class A {
private String str;
public A(String x){
this.str = x;
}
public String getStr(){
return this.str;
}
}
works perfectly! no null pointer exception. This is how it should be done. the first For loop is the alternative syntax for what i did wit the Iterator object. Actually Java automatically translates the first for loop in something that looks like the while loop.
完美运行!没有空指针异常。这是应该如何做的。第一个 For 循环是我用 Iterator 对象所做的替代语法。实际上,Java 会自动将第一个 for 循环转换为类似于 while 循环的内容。
回答by Sankalp
If you look at ArrayList remove implementation, it uses a local method fastRemove(index) as follows:-
如果您查看ArrayList remove implementation,它使用本地方法 fastRemove(index) 如下:-
/* * Private remove method that skips bounds checking and does not * return the value removed. */
/* * 私有删除方法,跳过边界检查并且不 * 返回删除的值。*/
private void fastRemove(int index) {
modCount++;
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // Let gc do its work
}
It does use arraycopy which is a proof that you get whole new list of fresh objects and not the null filled in between. Is this a proof?
它确实使用了 arraycopy,这证明您获得了全新的新对象列表,而不是中间填充的空值。这是证明吗?
回答by VHS
Either your observation is wrong or you are using some other kind of List implementation (and not ArrayList) that doesn't shift the elements to the right of the element being removed. Can you post your code?
要么您的观察是错误的,要么您使用的是其他类型的 List 实现(而不是 ArrayList),该实现不会将元素移到被删除元素的右侧。你能贴出你的代码吗?
If you look at the java.util.ArrayList source code in JDK8, you will see that the remove(Object o) method effectively copies the elements to the right of the element being removed, to the same array starting from the index of the element being removed. Look at the ArrayList source codefor more info:
如果您查看 JDK8 中的 java.util.ArrayList 源代码,您将看到 remove(Object o) 方法有效地将被删除元素右侧的元素复制到从元素索引开始的同一数组中被移除。查看 ArrayList源代码以获取更多信息:
回答by Tony Casale
The contract for java.util.List
implies that calling remove
will cause the size()
to be decremented. If you're talking specifically about java.util.ArrayList
then you might be right about the internal array not shifting its elements, but this is an implementation detail that shouldn't matter to you in 99% of all cases. If it still does matter, then you're trying to optimize for a specific situation and you should probably implement your own List
or use something like java.util.LinkedList
.
合同java.util.List
意味着调用remove
将导致size()
递减。如果您是专门讨论,java.util.ArrayList
那么您可能对内部数组不移动其元素是正确的,但这是一个实现细节,在 99% 的所有情况下对您来说都无关紧要。如果它仍然很重要,那么您正在尝试针对特定情况进行优化,您可能应该实现自己的List
或使用类似java.util.LinkedList
.
回答by Tony Casale
If all you need is an array of the data, then just call toArray()
.
如果您只需要一个数据数组,那么只需调用toArray()
.