简体   繁体   English

从ArrayList中删除项目 <ArrayList<T> &gt;&gt;

[英]Remove item from ArrayList<ArrayList<T>>>

In following code, I want to remove items which are null or size of arraylist is zero from ArrayList in first "for loop". 在下面的代码中,我想从第一个“ for循环”中的ArrayList中删除null或arraylist的大小为零的项目。 After running first loop, again I have a second loop. 运行完第一个循环后,我又有了第二个循环。 In second loop, I have tested whether it contains any item which is null or size of item is zero. 在第二个循环中,我测试了它是否包含任何为null的项目或项目的大小为零。 Although I erase in first loop, ArrayList BS contains items which was deleted from list. 尽管我在第一个循环中进行了擦除,但ArrayList BS包含从列表中删除的项目。

ArrayList<ArrayList<Record>> BS = new ArrayList<>(); 
.
.// some codes
.

for (int j = 0; j < BS.size(); j++) {
    if(BS.get(j) == null || BS.get(j).size() == 0){
        BS.remove(j);
    }
}


for (int j = 0; j < BS.size(); j++) {
    if(BS.get(j) == null || BS.get(j).size() == 0){
            System.out.println("Again, fall into if condition");
    }
}

The problem you've got here is that you won't check the item immediately after the one you removed. 您在这里遇到的问题是,您将不会在删除该项目后立即检查该项目。

Either: 要么:

BS.removeIf(a -> a == null || a.isEmpty());

Or iterate the list in reverse: 或反向遍历该列表:

for (int j = BS.size() - 1; j >= 0; j--) { ... }

Or use an Iterator , and use the remove() method. 或使用Iterator ,然后使用remove()方法。

Iterator<? extends List<Record>> it = BS.iterator();
while (it.hasNext()) {
  List<Record> list = it.next();
  if (list == 0 || list.isEmpty()) it.remove();
}

removeIf is the best approach, as it is optimized to know how to remove many elements efficiently: removing anything but the last element of an ArrayList requires all of the elements with greater index to be shifted along in the internal array, so removing elements by simply calling remove is quadratic in time complexity. removeIf是最好的方法,因为它经过优化以了解如何有效地删除许多元素:删除ArrayList的最后一个元素以外的任何元素都需要在内部数组中转移具有较大索引的所有元素,因此只需简单地删除元素调用remove在时间复杂度上是二次方的。

However, it is available in Java 8+ only. 但是,它仅在Java 8+中可用。 (You can implement the efficient removal quite easily in earlier versions of Java; it's just a little more work). (您可以在Java的早期版本中轻松实现高效删除;这只是更多工作)。

When removing items from an Array, usually you want to go from the last item to the first one, otherwwise your index will jump one item each time you do a remove. 从数组中删除项目时,通常您希望从最后一个项目转到第一个项目,否则,每次执行删除操作时,索引都会跳一个项目。

This because removing an item shifts the remaining ones back of one position, namely from position "j + 1" to position j. 这是因为删除一项将剩余的项移回一个位置,即从位置“ j +1”移到位置j。 So the next loop you skip the item a position j (that's the item that was in position j+1 in the previous loop). 因此,在下一个循环中,您将跳过项目到位置j的位置(即上一个循环中位于位置j + 1的项目)。

Try this: 尝试这个:

for (int j = BS.size() - 1; j >= 0; j--) {
    if(BS.get(j) == null || BS.get(j).size() == 0){
        BS.remove(j);
    }
}

Problem is when a item is removed next item will not be removed if the condition is true. 问题是,如果条件为真,则当删除一个项目时,下一个项目将不会被删除。 For that you should decrement the index when you remove an item. 为此,应在删除项目时减少索引。

Change as follow. 进行如下更改。

ArrayList<ArrayList<Record>> BS = new ArrayList<>(); 
.
.// some codes
.

for (int j = 0; j < BS.size(); j++) {
    if(BS.get(j) == null || BS.get(j).size() == 0){
        BS.remove(j);
        j--;
    }
}

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

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