【发布时间】: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);不起作用,那么你的问题就更大了。