Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中从arraylist中删除具有多个索引的多个元素_Java_Arrays_String_Arraylist - Fatal编程技术网

在java中从arraylist中删除具有多个索引的多个元素

在java中从arraylist中删除具有多个索引的多个元素,java,arrays,string,arraylist,Java,Arrays,String,Arraylist,我有两张清单。我将项目存储在一个列表中,在第二个列表中,我存储了我认为是索引的int数。p> 我想从包含所有索引的项目列表中删除项目 ArrayList<String> items = new ArrayList<String>(); ArrayList<Integer> indexes = new ArrayList<Integer>(); items.add("a"); items.add("b"); // should be remove

我有两张清单。我将项目存储在一个列表中,在第二个列表中,我存储了我认为是索引的int数。p> 我想从包含所有索引的项目列表中删除项目

ArrayList<String> items = new ArrayList<String>();

ArrayList<Integer> indexes = new ArrayList<Integer>();

items.add("a");
items.add("b"); // should be removed
items.add("c"); 
items.add("d"); // should be removed
items.add("e");
items.add("f"); // should be removed 
items.add("g");
items.add("h");


indexes.add(1);
indexes.add(3);
indexes.add(5);


Output : items : [a,c,e,g,h]
ArrayList items=new ArrayList();
ArrayList索引=新的ArrayList();
项目。添加(“a”);
项目。添加(“b”);//应该删除
项目。添加(“c”);
项目。添加(“d”);//应该删除
项目。添加(“e”);
项目。添加(“f”);//应该删除
项目。添加(“g”);
项目。添加(“h”);
增加(1);
增加索引(3);
增加(5);
输出:项目:[a、c、e、g、h]

您应该在末尾添加:

  Collections.reverse(indexes); 
    for(Integer index : indexes){
        items.remove((int)index);
    }
  • 使用索引反转列表,因为当您从1删除到n的下一个字母时,索引编号会发生变化,当您要删除索引“3”时,实际上是删除了索引“4”
  • 循环遍历要删除的索引
  • 将整数强制转换为int-

  • 完成。

    您应该在末尾添加:

      Collections.reverse(indexes); 
        for(Integer index : indexes){
            items.remove((int)index);
        }
    
  • 使用索引反转列表,因为当您从1删除到n的下一个字母时,索引编号会发生变化,当您要删除索引“3”时,实际上是删除了索引“4”
  • 循环遍历要删除的索引
  • 将整数强制转换为int-

  • 完成。

    使用java流API的解决方案

    IntStream.range(0, items.size()).filter(i -> !indexes.contains(i)).mapToObj(items::get)
                    .collect(Collectors.toList())
    

    使用java流API的解决方案

    IntStream.range(0, items.size()).filter(i -> !indexes.contains(i)).mapToObj(items::get)
                    .collect(Collectors.toList())
    
    公共静态无效过滤器(列表、列表索引StoreMove){
    集合。反向(indexesToRemove);
    for(整数indexToRemove:IndexStoreMove){
    list.remove((int)indexToRemove);
    }
    }
    公共静态void main(字符串[]args){
    ArrayList items=新建ArrayList();
    ArrayList索引=新的ArrayList();
    项目。添加(“a”);
    items.add(“b”);//应删除
    项目。添加(“c”);
    items.add(“d”);//应删除
    项目。添加(“e”);
    items.add(“f”);//应删除
    项目。添加(“g”);
    项目。添加(“h”);
    增加(1);
    增加索引(3);
    增加(5);
    过滤器(项目、索引);
    系统输出打印项次(项);
    }
    
    给你。从最大索引的数组开始删除:)

    公共静态无效筛选器(列表列表,列表索引StoreMove){
    集合。反向(indexesToRemove);
    for(整数indexToRemove:IndexStoreMove){
    list.remove((int)indexToRemove);
    }
    }
    公共静态void main(字符串[]args){
    ArrayList items=新建ArrayList();
    ArrayList索引=新的ArrayList();
    项目。添加(“a”);
    items.add(“b”);//应删除
    项目。添加(“c”);
    items.add(“d”);//应删除
    项目。添加(“e”);
    items.add(“f”);//应删除
    项目。添加(“g”);
    项目。添加(“h”);
    增加(1);
    增加索引(3);
    增加(5);
    过滤器(项目、索引);
    系统输出打印项次(项);
    }
    

    给你。从数组中删除最大索引的开头:)

    对于在该页面上被卡住并登陆的任何人,但您的索引不是索引列表,而是索引数组。这个页面很有帮助,但我对答案做了一点调整,效果很好。下面的代码

     Arrays.sort(itemIndices, Collections.reverseOrder());
            for (int i : itemIndices) {
                uriCache.add(mediaUris.remove(i));
            }
    

    注意:itemIndexs表示列表中要删除的位置的索引URICHACHE不是必需的,我在代码中使用它来存储已删除项的列表(顺便说一句,它们是URI)MediaURI是一个列表,您希望删除那些具有数组ItemIndexs

    指定的索引的项目,该数组用于被卡住并登录到此页面的任何人,但您的索引不是索引的列表,而是索引的数组。这个页面很有帮助,但我对答案做了一点调整,效果很好。下面的代码

     Arrays.sort(itemIndices, Collections.reverseOrder());
            for (int i : itemIndices) {
                uriCache.add(mediaUris.remove(i));
            }
    

    注意:itemIndexs表示列表中要删除的位置的索引URICHACHE不是必需的,我在代码中使用它来存储已删除项的列表(顺便说一句,它们是URI)MediaURI是您要删除那些具有数组指定索引的项目的列表ItemIndexs

    请阅读请阅读Shaunak Patel…非常感谢您花时间制作。Shaunak Patel…非常感谢您花时间制作。