Java 如何从嵌套数组列表中删除项目
Java How to remove item from nested Array List
我有一个带有布尔值的嵌套 ArrayList,如下所示。我想从所有行中删除例如第 3 项。我尝试了一个循环,但它没有将 remove
解析为一种方法。我应该怎么做?非常感谢您的帮助。
for (int i = 0; i < list.size(); i++){
list.get(i).remove(3)// this remove method shows as an error in IDE
}
true false true false false false
false false true false true true
... It's a list of List<Instance> listInstances = new
ArrayList<Instance>();
and the class Instance
has vals = new
ArrayList<Boolean>();
....
在这种情况下,您的解决方案可能如下所示:
public static Instance deleleNthElement(Instance instance, int index) {
instance.getVals().remove(index - 1);
return instance;
}
然后使用流,您可以像这样调用方法:
int index = 3;
listInstances = listInstances.stream()
.map(instance -> deleleNthElement(instance, index))
.collect(Collectors.toList());
我认为您的逻辑没有错误,我相信您漏掉了一个“;”从 remove(3).
的末尾开始
顺便说一句,List 是一个接口,您需要将其实例化为 ArrayList(或类似的)。
我将以下内容串联在一起,似乎符合您的意图:
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String[] args) throws IOException {
List<Boolean> row1 = new ArrayList<Boolean>(Arrays.asList(new Boolean[] {true,false,true,true}));
List<Boolean> row2 = new ArrayList<Boolean>(Arrays.asList(new Boolean[] {true,true,false,true}));
List<List<Boolean>> list = Arrays.asList(new ArrayList[] {(ArrayList) row1, (ArrayList) row2});
for (int i=0;i<list.size();i++){
list.get(i).remove(3);// this remove method shows as an error in IDE
}
for (List<Boolean> ll : list) {
for (Boolean l : ll) {
System.out.print(l + ",");
}
System.out.println();
}
}
}
我有一个带有布尔值的嵌套 ArrayList,如下所示。我想从所有行中删除例如第 3 项。我尝试了一个循环,但它没有将 remove
解析为一种方法。我应该怎么做?非常感谢您的帮助。
for (int i = 0; i < list.size(); i++){
list.get(i).remove(3)// this remove method shows as an error in IDE
}
true false true false false false
false false true false true true
... It's a list of
List<Instance> listInstances = new ArrayList<Instance>();
and the classInstance
hasvals = new ArrayList<Boolean>();
....
在这种情况下,您的解决方案可能如下所示:
public static Instance deleleNthElement(Instance instance, int index) {
instance.getVals().remove(index - 1);
return instance;
}
然后使用流,您可以像这样调用方法:
int index = 3;
listInstances = listInstances.stream()
.map(instance -> deleleNthElement(instance, index))
.collect(Collectors.toList());
我认为您的逻辑没有错误,我相信您漏掉了一个“;”从 remove(3).
的末尾开始
顺便说一句,List 是一个接口,您需要将其实例化为 ArrayList(或类似的)。
我将以下内容串联在一起,似乎符合您的意图:
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String[] args) throws IOException {
List<Boolean> row1 = new ArrayList<Boolean>(Arrays.asList(new Boolean[] {true,false,true,true}));
List<Boolean> row2 = new ArrayList<Boolean>(Arrays.asList(new Boolean[] {true,true,false,true}));
List<List<Boolean>> list = Arrays.asList(new ArrayList[] {(ArrayList) row1, (ArrayList) row2});
for (int i=0;i<list.size();i++){
list.get(i).remove(3);// this remove method shows as an error in IDE
}
for (List<Boolean> ll : list) {
for (Boolean l : ll) {
System.out.print(l + ",");
}
System.out.println();
}
}
}