这个问题这次不能解决,因为代码有重大错误,需要在提交问题之前先更正。
问问题
52 次
4 回答
1
代替:
if (result = true)
做
if (result)
此外,而不是:
for (int index = 0; index < lineList.size() - 1; index =+2)
做
for (int index = 0; index < lineList.size(); index +=2)
已编辑:您的 for 声明中有两个问题:
index < lineList.size() - 1
不会打到最后一项。删除- 1
或更改<
为<=
- 索引不会增加其值。更改
index =+2
为index +=2
.
于 2012-04-23T12:35:51.073 回答
0
我能找到的一个明显的错误是:
if (result = true)
应该是:
if (result == true)
于 2012-04-23T12:34:08.687 回答
0
这里有一个问题:
if (result = true)
你可能打算写:
if (result == true)
编写此检查的更好方法是:
if (result)
中也可能存在一个错误index < lineList.size() - 1
。如果文件包含奇数行,您当前的代码将忽略最后一行。我说可能,因为在这种情况下你期望发生什么并不完全清楚。
最后,整个事情可以用一个循环而不用列表(即只用集合)来实现。
于 2012-04-23T12:34:15.597 回答
0
您有一个包含两个错误的部分。与我的样本比较:
for (int index = 0; index < lineList.size() - 1; index += 2)
if (usr.add(list.get(index))) System.out.println(list.get(index));
或者,在另一个成语中:
boolean take = true;
for (String line : lineList) {
if (take) usr.add(line);
take = !take;
}
于 2012-04-23T12:38:09.500 回答