如何根据列表的位置从列表副本中删除值?



我正在尝试根据这些特定项目在值列表中的位置删除它们。下面的代码,当复制列表缩小太多时引发错误

private ArrayList<String> removeGen(int addLines, int labLines, int venLines, ArrayList<String> values){
        ArrayList<String> copy = new ArrayList<>();
        copy = (ArrayList<String>) values.clone();
        for(int i = 0; i < values.size(); i++) {
            if (i > 100 && i < (100 + (addLines * 5) + 1)) {
                copy.remove(i);
            }
            if (i > (100 + (addLines * 5)) + 9 && i < 100 + (addLines * 5) + (labLines * 4) + 10) {
                copy.remove(i);
            }
            if (i > 100 + (addLines * 5) + (labLines * 4) + 21 && i < 100 + (addLines * 5) + (labLines * 4) + (venLines * 4) + 22) {
                copy.remove(i);
            }
        }
        return copy;

问题是有些值彼此无法区分,所以我也不能使用 remover(obj)。

如何返回在上述位置删除值的列表?

您正在按索引而不是按值删除,但是您忘记了values的索引不是删除某些内容后copy中的索引。删除后,每个对象都会获得另一个索引,因此列表不再相同。

例:删除索引 4 后,索引 5 将获取索引 4 上的新值。这种切换效果将持续到列表末尾。

因此,有两种方法可以使此方法正确:

  1. 扭转条件,将所有有效数据添加到copy并返回此列表。
  2. 在索引 i 处为空值,并在之后过滤列表,然后再返回它。 return copy.filter(x -> x != null).collect(Collectors.toList()); 如果您使用的是 java 7 及更低版本,则可以使用 copy.removeAll(Collections.singleton(null));

这是第一个结果:

ArrayList<String> copy = new ArrayList<>();
for(int i = 0; i < values.size(); i++) {
    if (i < 100 && i > (100 + (addLines * 5) + 1)) {
        copy.add(values.get(i));
    }
    if (i < (100 + (addLines * 5)) + 9 && i > 100 + (addLines * 5) + (labLines * 4) + 10) {
        copy.add(values.get(i));
    }
    if (i < 100 + (addLines * 5) + (labLines * 4) + 21 && i > 100 + (addLines * 5) + (labLines * 4) + (venLines * 4) + 22) {
        copy.add(values.get(i));
    }
}
return copy;

这是第二个结果:

ArrayList<String> copy = new ArrayList<>();
copy = (ArrayList<String>) values.clone();
for(int i = 0; i < values.size(); i++) {
    if (i > 100 && i < (100 + (addLines * 5) + 1)) {
        copy.get(i) = null;
    }
    if (i > (100 + (addLines * 5)) + 9 && i < 100 + (addLines * 5) + (labLines * 4) + 10) {
        copy.get(i) = null;
    }
    if (i > 100 + (addLines * 5) + (labLines * 4) + 21 && i < 100 + (addLines * 5) + (labLines * 4) + (venLines * 4) + 22) {
        copy.get(i) = null;
    }
}
return copy.filter(x -> x != null).collect(Collectors.toList());
 private ArrayList<Integer> removeGen(int addLines, int labLines, int venLines, ArrayList<String> values){
        ArrayList<String> copy = new ArrayList<>();
        ArrayList<Integer> numbers = new ArrayList<>();
        copy = (ArrayList<String>) values.clone();
        for(int i = 0; i < values.size(); i++) {
            if (i > 100 && i < (100 + (addLines * 5) + 1)) {
                //copy.remove(i);
            }
            else if(i > (100 + (addLines * 5)) + 9 && i < 100 + (addLines * 5) + (labLines * 4) + 10) {
                //copy.remove(i);
            }
            else if(i > 100 + (addLines * 5) + (labLines * 4) + 21 && i < 100 + (addLines * 5) + (labLines * 4) + (venLines * 4) + 22) {
                //copy.remove(i);
            }
            else {
                numbers.add(i);
            }

        }
        return numbers;
    }

只是做了相反的事情,提取了我需要的位置,应该对其他人有帮助

 ArrayList<Integer> tempList = removeGen(additional,labour,vendor,values);
        for (int j = 0; j < tempList.size(); j++) {
            template = template.replaceFirst(section + j, values.get(tempList.get(j)));
        }

最新更新