简体   繁体   English

如何从Java ArrayList中删除顺序元素?

[英]How do I remove sequential elements from a Java ArrayList?

I'm a relatively new Java programmer and I'm having difficuly removing more than one element from an ArrayList. 我是一个相对较新的Java程序员,我从diffList中删除了多个元素。 Ideally I'd like to do something like this: 理想情况下,我想做这样的事情:

ArrayList ar1 = new ArrayList();
ar1.add(...)
ar1.add(...)
ar1.add(...)
ar1.add(...)

for (int i = 0; i < 2; i++){
     ar1.remove(i);
}

I think iterator might help, but I can't find an example that matches close enough to what I'm trying to do. 我认为迭代器可能会有所帮助,但我找不到一个与我正在尝试的内容足够接近的示例。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks. 谢谢。

Here's what you want to do: 这是你想要做的:

ar1.subList(0, 2).clear();

This creates a sublist view of the first 2 elements of the list and then clears that sublist, removing them from the original list. 这将创建列表的前2个元素的子列表视图,然后清除该子列表,将其从原始列表中删除。 The subList method exists primarily for this sort of thing... doing operations on a specific range of the list. subList方法主要用于这种事情......在列表的特定范围内执行操作。

You can certainly do that 你当然可以这样做

    ArrayList ar1 = new ArrayList();
    ar1.add("a");
    ar1.add("b");
    ar1.add("c");
    ar1.add("d");

    for (int i = 0; i < 2; i++) {
        ar1.remove(i);
    }
    System.out.println(ar1);

Only pay attention that after you remove first element, other elements shift. 只需注意在删除第一个元素后,其他元素就会移位。 Thus, calling 因此,打电话

ar1.remove(0);
ar1.remove(1);

will effectively remove first and third elements from the list. 将有效地从列表中删除第一个和第三个元素。 This will delete first two elements, though: 这将删除前两个元素,但:

ar1.remove(0);
ar1.remove(0);

For indexed removals from a list, you need to count backwards: 对于列表中的索引删除,您需要向后计数:

 for (int i = 1; i >= 0; i--)

otherwise, your first removal shifts the items "above" it in the collection and you don't wind up removing the items you think you are removing. 否则,您的第一次删除会将项目“高于”它移动到集合中,并且您不会删除您认为要删除的项目。

You can use Collection.removeAll(toRemove) if you have a separate list of objects to remove. 如果要删除要单独的对象列表,则可以使用Collection.removeAll(toRemove)。

http://download.oracle.com/javase/6/docs/api/java/util/Collection.html http://download.oracle.com/javase/6/docs/api/java/util/Collection.html

If your collection is indexed based, like ArrayList is, you can call 如果您的集合是基于索引的,就像ArrayList一样,您可以调用

remove(index)

to remove the element at the index. 删除索引处的元素。 You can do that in a loop, but beware that removing shifts all the indexes as another answer points out. 您可以在循环中执行此操作,但要注意删除所有索引的移位作为另一个答案指出。

If all you want to do is remove the first two elements from the list, then 如果你想要做的就是从列表中删除前两个元素,那么

   list.remove(0);
   list.remove(0);

should do it. 应该这样做。

If you know the indexes of the items you want to remove, you can remove them in reverse order, without worrying about shifting indexes: 如果您知道要删除的项目的索引,则可以按相反的顺序删除它们,而不必担心转移索引:

    ArrayList ar1 = new ArrayList();
    ar1.add("a");
    ar1.add("b");
    ar1.add("c");
    ar1.add("d");

    int[] indexesToRemove = {0,2,3};
    Arrays.sort(indexesToRemove);
    for (int i=indexesToRemove.length-1; i>=0; i--) {
        ar1.remove(indexesToRemove[i]);
    }

You could try this: 你可以试试这个:

List<Whatever> l = new ArrayList<Whatever>();
l.add(someStuff);
Iterator<Whatever> it = l.iterator();
int i = 0;
while (i < 2 && it.hasNext()) {
    it.next();
    it.remove();
    i++;
}

Or, more generally: 或者,更一般地说:

List<Whatever> l = new ArrayList<Whatever>();
l.add(someStuff);
Iterator<Whatever> it = l.iterator();
while (it.hasNext()) {
    Whatever next = it.next();
    if (shouldRemove(next)) {
        it.remove();
    }
}

EDIT: I guess it depends if you are trying to remove particular indices or particular objects. 编辑:我想这取决于你是否试图删除特定的索引或特定的对象。 It also depends on how much logic you need to decide if something should be removed. 它还取决于您需要多少逻辑来决定是否应删除某些内容。 If you know the indices then remove them in reverse order. 如果您知道索引,则按相反顺序删除它们。 If you have a set of Objects to be removed, then use removeAll. 如果要删除一组对象,请使用removeAll。 If you want to iterate over the list and remove objects that match a predicate then use the above code. 如果要迭代列表并删除与谓词匹配的对象,请使用上面的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM