【发布时间】:2015-12-08 02:43:58
【问题描述】:
我正在尝试使用以下代码从包含 [9, 0, 0, 6, 2, 3, 0, 0, 0] 的 ArrayList 中删除一个 int
static void appendRow(ArrayList<ArrayList<Integer>> ans) {
int ind = 0;
while(yolo.size() < 81) {
for(int x = 0; x < ans.size(); x++) {
ArrayList<Integer> an = ans.get(x);
for(int i = ind; i < ind+3; i++) {
yolo.add(an.get(i));
}
for(int y = 0; y < 3; y++) {
an.remove(y);
}
}
ind+=3;
}
System.out.println(yolo);
}
这里发生的事情是,在我从 ans 添加前 3 个整数后,我将删除它们。有多个项目将传递给 appendRow 函数,但我只需要前三个,下一个函数只需要其余的(即 [6, 2, 3, 0, 0, 0])。
问题是当y = 2时,an.remove(y)并没有删除索引为2的0,而是删除了2,所以列表变成了[0, 6, 3, 0, 0, 0],这是完全错误的。
【问题讨论】:
-
尝试使用
an.remove(new Integer(y)) -
嗨 MadProgrammer,感谢您的回复,但不幸的是它不起作用。它返回 [9, 0, 6, 3, 0, 0, 0]
-
@RileyCarney: yolo 是另一个 ArrayList
-
哦,好的,谢谢,试试@Elliot Frisch 所说的