简体   繁体   English

Java中的Arraylist删除元素

[英]Arraylist in Java removing elements

I have 2 ArrayList, one containing String s, the other Integer s. 我有2个ArrayList,一个包含String ,另一个包含Integer The list2 contains indices of elements of list1 . list2包含list1元素的索引。

Now I need to remove all the elements from list1 whose index is in list2 . 现在,我需要从list1删除其索引在list2中的所有元素。 Any ideas? 有任何想法吗?

ArrayList<String> list1 = new ArrayList<String>(); 
list1.add("a");
list1.add("b");
list1.add("c");
list1.add("d");
list1.add("e");
list1.add("f");

ArrayList<Integer> list2 = new ArrayList<Integer>();
list2.add(1);
list2.add(4);
list2.add(2);

The problem here is, you cannot remove from original list1 as the index will keep changing. 这里的问题是,您不能从原始list1删除,因为索引会不断变化。 I tried creating a temp HashMap to store the array index and String relation. 我尝试创建一个临时HashMap来存储数组索引和String关系。

I iterate over list2 and map. 我遍历list2并映射。 When I found a matching key=index, I skipped that. 当找到匹配的key = index时,我将其跳过。 Else I put String element in new list. 另外,我将String元素放入新列表中。

Any better suggestions? 还有更好的建议吗?

Sort list2 and delete items from list1 starting at the highest index given in list2 . list2排序,并从list2给定的最高索引开始,从list1删除项目。 That way your other relevant indexes in list1 wont change. 这样,您在list1其他相关索引将不会更改。

You could do it like this: 您可以这样做:

    Collections.sort(list2, Collections.reverseOrder());
    for (Integer i : list2) {
        list1.remove((int) i);
    }

EDIT: 编辑:

As pointed out by David Wallace in his comment, above approach only works if there are no duplicates in list2 . 正如David Wallace在其评论中指出的那样,上述方法仅在list2中没有重复项的情况下才有效。 You can get rid of duplicates by adding the following line before the code posted above: 通过在上面的代码之前添加以下行,可以消除重复项:

list2 = new ArrayList<>(new HashSet<>(list2));

This solution assumes that null is not a valid value in list1 . 此解决方案假定null在list1不是有效值。

You could iterate through list2 , and for each index you get to, set the corresponding value in list1 to null. 您可以遍历list2 ,对于获得的每个索引,将list1的相应值设置为null。 Then remove all the nulls from list1 at the end. 然后从列表list1的最后删除所有空值。 This will work even if there are duplicate elements in list2 , which jlordo's solution would have difficulty with. 即使list2存在重复的元素,这也会起作用,而jlordo的解决方案将很难解决这个问题。

for(Integer index : list2){
    list1.set(index,null);
}
list1.removeAll(Collections.singleton(null));

If you just care about insertion order, something like a LinkedHashMap work better for this. 如果您只关心插入顺序,那么类似LinkedHashMap更好。 But, in order to determine an index, you would have to iterate over the list until you found the element you wanted: 但是,为了确定索引,您必须遍历列表,直到找到所需的元素:

int n = 0;
for (String value : linkedMap) {
  if (value.equals(valueToSearch)) {
    break;
   }
   ++n;
}
// n == index now

If you frequently need the indexes, then you probably just want 2 Maps. 如果您经常需要索引,那么您可能只需要2张地图。 One that holds indexe -> String and the other holds String -> index. 一个持有indexe-> String,另一个持有String-> index。 Just make sure you insert and remove from both maps at the same time. 只要确保您同时插入和删除两个地图即可。

Used for BaseAdapter and getView() method inside code write down and particular onClickListner imlement and try this out this code, 用于代码内的BaseAdapter和getView()方法,并写下特定的onClickListner元素,并尝试以下代码,

mArrayList.remove(position);
notifyDataSetChanged();

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

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