Java 如何从嵌套的数组列表中删除项目

标签 java list nested nested-lists

我有一个带有 boolean 值的嵌套 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());

关于Java 如何从嵌套的数组列表中删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55321983/

相关文章:

java - 使用 SHA1 进行哈希处理

java - 如何在 spring application.properties 中注入(inject)包含 ${...} 的字符串

CSS - 不使用绝对定位和 JS 的水平样式列表

xml - 直接与间接嵌套结构

elasticsearch - 如何创建在结果中包含嵌套数据的Elasticsearch查询

java - Java中静态类的初始化

eclipse - 在 Eclipse 中安装新的 JRE

python - 以列表为值的字典 - 查找最长列表

python - 使用python删除字典内列表中的元素

asp.net-mvc-3 - 嵌套的 BeginCollectionItem