【发布时间】:2014-02-04 17:56:29
【问题描述】:
在我想要它之前/在它执行整个 for 循环块之前,我无法弄清楚为什么 for 循环计数器会增加。我在代码中添加了用于调试的打印语句,代码如下:
public void findSubSeq() {
temp = new ArrayList<Integer>();
temp.add(vals.get(0));
for (int i = 0; i < (vals.size()-2); i++) {
System.out.println(i + " index in array, for");
if (vals.get(i) < vals.get(i++)) {
System.out.println(i + " index in array, if 1");
temp.add(vals.get(i++));
System.out.println(i + " index in array, if 2");
System.out.println(this.getlargest());
}
else {
System.out.println(i + " index in array, else 1");
compareALs();
System.out.println(i + " index in array, else 2");
temp.add(vals.get(i++));
System.out.println(i + " index in array, else 3");
System.out.println(this.getlargest());
}
}
}
在代码运行中,我们可以看到计数器 i 在 for 循环结束之前增加。我真的很困惑。我还尝试了一个 for-each 循环,结果相似。 这是运行: 在创建 subarrayB 之前
在填充之前
输入文件:inp
[1, 2, 3, 0, 4, 5]
在 findSubSeq 之前
数组中的0索引,用于
数组中有1个索引,否则为1
数组中有1个索引,否则为2个
数组中有2个索引,否则为3
[1]
数组中的3个索引,用于
数组中有4个索引,否则为1
数组中有4个索引,否则为2个
数组中有5个索引,否则为3
[1]
【问题讨论】:
-
为什么要投反对票?他显然试图理解错误来自哪里......