【问题标题】:Java ArrayList.remove(index) issue [closed]Java ArrayList.remove(index) 问题[关闭]
【发布时间】:2015-03-03 11:05:50
【问题描述】:

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

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,它包含了包含公式的表格的所有单元格,因此在检查依赖关系之前无法计算它们。

【问题讨论】:

  • 作为记录,cellNotComputed.clear() 将是执行上述操作的更快方法。
  • cellsNotComputed 包含哪些类型的元素?
  • 你可以试试cellsNotComputed.remove(cellsNotComputed.get(i));
  • cellsNotComputed.remove(cellsNotComputed.get(i)) 也不会删除任何东西@EpicPandaForce
  • 你确定这条线真的在运行吗?如果cellsNotComputed.remove(computedCell); 不起作用,那么你的问题就更大了。

标签: java list arraylist


【解决方案1】:

如果您想清空列表,只需致电

cellsNotComputed.clear ();

【讨论】:

  • 这只是我代码的一小部分,我不只是像这样删除所有元素,我的代码做了更多的事情。它在删除元素之前会检查一些东西,但问题是它永远不会这样做
  • @Luxy cellsNotComputed 是如何声明的?
  • ArrayList cellsNotComputed;
  • 它包含 Cell 元素,这是我拥有的另一个类。一个 Cell 有 5 个属性 int row, int column, int value, string content, string name
  • 我已经编辑了我的代码,请看一下
【解决方案2】:

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

考虑以下我尝试进行测试的代码。

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);
    }

只有当 arrayList 中只有一个单元格的内容与“某物”完全相同时,这才有效。请检查内容。

我不确定这是否正是您想要的,因为如果出现以下情况,它将进入无限循环: 该列表包含任何内容不是“某物”的单元格

以上代码产生

Before removing [Cell [id=1, content=Something]]
Cell [id=1, content=Something]
After removing []

并终止

【讨论】:

  • 即使 i 没有改变(这是我没有很好解释的)方法 remove 什么都不做
  • 我已经尝试过了...什么也没有发生
  • 你能在 sysout 语句中添加cellsNotComputed.remove(i); 并告诉结果
  • es.luxy.spreadsheet.Cell@7852e922
  • 它只是循环输出
【解决方案3】:

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

试试这样更易读的东西:

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();
}

【讨论】:

  • 即使 i 没有改变(这是我没有很好解释的)方法 remove 什么都不做
  • 不,我之前使用for(int i = 0; i &lt; list.size(); i++) { if(something) { list.remove(i--); } } 从列表中删除了元素,它工作得非常好。
  • 我已经编辑了我的代码,请看一下
  • @Luxy,就像一个想法......可能是你正在使用的 List 是一些“特殊”实现,可能有一个空的“删除”方法?尝试打印类名:cellsNotComputed.getClass().getCanonicalName()... 如果它不是标准的 Java 列表(如 ArrayList),则可能是 remove 方法没有正确实现...
  • @EpicPandaForce,我同意它应该可以工作,但我会鼓励以更易读的方式来做。这种模式只是不可读,一开始会引起混淆,并且与我列出的更具可读性的替代方案完全相同......
【解决方案4】:

您的 for 循环每一步都会递增 i,但在循环内您只需递减它...所以它就像 i + 1 - 1 = i

【讨论】:

  • 即使 i 没有改变(这是我没有很好解释的)方法 remove 什么都不做
  • 我已经编辑了我的代码,请看一下
【解决方案5】:

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

cellsNotComputed.clear ();

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

【讨论】:

    【解决方案6】:
    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);
       }
    
    }
    

    【讨论】:

    • 您能否发布更多代码以便我们也看到任何数据?尤其是类单元格可能很有趣。
    猜你喜欢
    • 2018-08-24
    • 1970-01-01
    • 2012-05-07
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    相关资源
    最近更新 更多