Java ArrayList.remove(索引)问题
Java ArrayList.remove(index) issue
我在尝试执行此操作时遇到了一些问题,而且我仍然不明白为什么这段代码没有执行任何操作...它没有从我的列表中删除任何内容,只是循环。一些提示?
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,它包含 table 的所有包含公式的单元格,因此在检查依赖关系之前无法计算它们。
你的 for 循环每一步递增 i
但在你的循环中你只是递减它......所以它就像 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);
}
仅当 arrayList 中只有一个单元格的内容与 "Something" 完全相同时,此方法才有效。请检查内容。
我不确定这是否正是您想要的,因为如果出现以下情况,它将进入无限循环:
该列表包含任何内容不是 "Something"
的单元格
以上代码产生
Before removing [Cell [id=1, content=Something]]
Cell [id=1, content=Something]
After removing []
并终止
如果您想清空列表,只需致电
cellsNotComputed.clear ();
如果要从列表中删除所有元素,可以使用清除方法。
cellsNotComputed.clear ();
Clear() 从 ist 中删除所有元素。此调用后列表将为空 returns.
如前所述,您的示例只是递增和递减计数器,这是一个奇怪的模式。
尝试像这样更具可读性的内容:
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();
}
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);
}
}
我在尝试执行此操作时遇到了一些问题,而且我仍然不明白为什么这段代码没有执行任何操作...它没有从我的列表中删除任何内容,只是循环。一些提示?
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,它包含 table 的所有包含公式的单元格,因此在检查依赖关系之前无法计算它们。
你的 for 循环每一步递增 i
但在你的循环中你只是递减它......所以它就像 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);
}
仅当 arrayList 中只有一个单元格的内容与 "Something" 完全相同时,此方法才有效。请检查内容。
我不确定这是否正是您想要的,因为如果出现以下情况,它将进入无限循环: 该列表包含任何内容不是 "Something"
的单元格以上代码产生
Before removing [Cell [id=1, content=Something]]
Cell [id=1, content=Something]
After removing []
并终止
如果您想清空列表,只需致电
cellsNotComputed.clear ();
如果要从列表中删除所有元素,可以使用清除方法。
cellsNotComputed.clear ();
Clear() 从 ist 中删除所有元素。此调用后列表将为空 returns.
如前所述,您的示例只是递增和递减计数器,这是一个奇怪的模式。
尝试像这样更具可读性的内容:
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();
}
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);
}
}