Java ArrayList.remove(索引)问题

Java ArrayList.remove(索引)问题,java,list,arraylist,Java,List,Arraylist,我在尝试这样做时遇到了一些问题,但我仍然不明白为什么这段代码没有做任何事情。。。它不会从我的列表中删除任何内容,只是循环。一些提示 public void compute(){ String formula =""; boolean hasDependencies = false; Integer value = 0; while(!cellsNotComputed.isEmpty()){ for(int i=0; i<cellsNotCo

我在尝试这样做时遇到了一些问题,但我仍然不明白为什么这段代码没有做任何事情。。。它不会从我的列表中删除任何内容,只是循环。一些提示

public void compute(){
    String formula ="";
    boolean hasDependencies = false;
    Integer value = 0;

    while(!cellsNotComputed.isEmpty()){
        for(int i=0; i<cellsNotComputed.size(); i++){
            formula = getCell(cellsNotComputed.get(i).getRow(), 
                                cellsNotComputed.get(i).getColumn())
                             .getContent();
            // formulas always begin with = and only contains cell names
            // and + symbols (ex. =A1+A2+A3)
            formula = formula.substring(1);
            String[] dependantCells = formula.split("\\+");

            for (String cellName : dependantCells) {
                for (Cell cell : cellsNotComputed) {
                    if(cell.getName().equals(cellName)){
                        hasDependencies = true;
                    }
                }
                if(!hasDependencies){
                    value = value + getCell(cellName).getValue();
                }
            }

            if(!hasDependencies){
                Cell computedCell = cellsNotComputed.get(i);
                cellsNotComputed.remove(i); // it works but... ** 
                i--;
                computedCell.setValue(value);
                setCell(computedCell.getRow(),
                        computedCell.getColumn(),
                        computedCell);                  
            }

            hasDependencies = false; //** here the element removed is again
                                     // in the list.
            value = 0;
        }
    }
}

**要澄清的是,cellsNotComputed是一个属性,是一个ArrayList,它包含表中包含公式的所有单元格,因此在检查依赖项之前无法计算这些单元格。

for循环每一步都会递增,但在循环中,只会递减它。。。所以它就像i+1-1=i

在您的代码中,我无法理解的是,如果您没有找到任何匹配项,或者如果您找到任何内容不重要的单元格,那么这个循环将是一个无止境的循环

考虑下面我尝试测试的代码

public class Test {

    static class Cell{
        private int id;
        private String content;
        public String getContent() {
            return content;
        }
        public void setContent(String content) {
            this.content = content;
        }
        public Cell(int id, String content) {
            this.id = id;
            this.content = content;
        }
        @Override
        public String toString() {
            return "Cell [id=" + id + ", content=" + content + "]";
        }
    }

    static private List<Cell> cellsNotComputed;

    public static void main(String[] args){
        cellsNotComputed = new ArrayList<Test.Cell>();
        cellsNotComputed.add(new Cell(1, "Something"));
        //cellsNotComputed.add(new Cell(2, "Hi"));
        System.out.println("Before removing " + cellsNotComputed);
        while(!cellsNotComputed.isEmpty()){
            for(int i=0; i<cellsNotComputed.size(); i++){
                if(cellsNotComputed.get(i).getContent().equals("Something")){
                  System.out.println(cellsNotComputed.remove(i));
                  i--;
                }                   
            }
        }
        System.out.println("After removing " + cellsNotComputed);
    }

并终止

如果您想清空列表,只需拨打

cellsNotComputed.clear ();

如果要从列表中删除所有元素,可以使用clear方法

cellsNotComputed.clear ();

清除从ist中删除所有元素。此调用返回后,列表将为空。

如前所述,您的示例只是递增和递减计数器,这是一种奇怪的模式

尝试更具可读性的方法,例如:

public void remove(){
    while(!this.cellsNotComputed.isEmpty()) {
        //if you need to do something with the element, extract 0 and do it.
        cellsNotComputed.remove(0);
    }
}
或者更好,以防您只想清除:

public void remove(){
        cellsNotComputed.clear();
}


作为记录,cellsNotComputed.clear是执行上述操作的更快方法。cellsNotComputed包含什么类型的元素?您可以尝试cellsNotComputed.removecellsNotComputed.geti;cellsNotComputed.removecellsNotComputed.geti也不会删除任何内容@EpicPandaForce您确定该行实际运行吗?如果单元格未计算。则删除已计算单元格;这只是我代码的一小部分,我不会像这样删除所有元素,我的代码会做更多的事情。在删除元素之前,它会检查一些东西,但问题是它从来不会这样做it@Luxy如何声明cellsNotComputed?ArrayList cellsNotComputed;它包含Cell元素,这是我拥有的另一个类。一个单元格有5个属性:int行,int列,int值,字符串内容,字符串名称我已经编辑了我的代码,请看一看,即使我没有改变,这是我没有解释好的方法删除没有做什么我已经尝试过了。。。没有发生任何事情您可以添加单元格notcomputed.removei;在sysout语句中,并告诉resultses.luxy.spreadsheet。Cell@7852e922it只是循环输出,即使i没有改变,这是我没有解释清楚的方法remove没有做任何事情,我已经编辑了我的代码,请看一看,即使i没有改变,这是我没有解释清楚的方法remove没有做任何事情。不,我以前用forint i=0从列表中删除了元素;ipublic void remove(){ cellsNotComputed.clear(); }
public void remove(){
List<Integer> founds= new ArrayList<Integer>;
    while(!this.cellsNotComputed.isEmpty()){
        for(int i=0; i<this.cellsNotComputed.size(); i++){
            if(cellsNotComputed.get(i).getContent().equals("something")){
              // cellsNotComputed.remove(i);
               founds.add(i);
               //i--;
            }                   
        }
    }
   for(int f: founds){
      cellsNotComputed.remove(f);
   }

}