- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面的类用于按行索引单词的位置。抛出错误的方法旨在将单独文档的索引附加到当前索引。也就是说,如果第一个文档中有 6 行,则附加文档的第一行应该被索引为第 7 行。
public class DocumentIndex {
// a NavigableMap implementation to store indexed words and their locations
private TreeMap<String, ArrayList<Integer>> map = new TreeMap<String, ArrayList<Integer>>();
/**
* A method to append another index onto the main index
* @param indexAppendix the additional index to be appended onto the main index
*/
public void append(DocumentIndex indexAppendix){
if(indexAppendix == null){
throw new NullPointerException();
}
Integer docEnd = 0; // the last line recorded in the main index
Set<String> set = map.keySet(); // a Set of the key values from map
//select each key
for(Iterator<String> iter = set.iterator(); iter.hasNext();){
String key = iter.next(); // the current key value
// for each key select contents and determine the highest value
for(Iterator<Integer> iter2 = this.find(key).iterator(); iter2.hasNext();){
Integer compare = iter2.next(); // the key index current value
if(compare>docEnd){
docEnd=compare;
}
}
}
// for each key find an index value
for(Iterator<String> iter = set.iterator(); iter.hasNext();){
String key = iter.next(); // the current key value
// for each index value map that value adjusting for the length of the original document
ArrayList<Integer> toAdd = new ArrayList<Integer>();
for(Iterator<Integer> iter2 = this.find(key).iterator(); iter2.hasNext();){
Integer addIter = iter2.next();
toAdd.add(addIter); // the current index value
}
/**
*Below is the loop in which the error is thrown
*/
for(Iterator<Integer> iter3 = toAdd.iterator(); iter.hasNext();){
Integer addIter = iter3.next(); // The error is thrown on this line
map.get(key).add(addIter+docEnd);
}
}
}
我做错了什么?
最佳答案
Louis Wasserman 做到了。
我只想指出,如果您使用“新的”Java for
循环语法,您的代码会简单很多,而且不会(不能!!)一开始就犯了那个错误。例如,使用"new"for
语法,您的代码大致如下所示:
...
Integer docEnd = 0;
Set<String> set = map.keySet();
for (String key : set) {
for (Integer compare : this.find(key)) {
if (compare < docEnd){
docEnd = compare;
}
}
}
for (String key : set) {
ArrayList<Integer> toAdd = new ArrayList<Integer>();
for (String add : this.find(key)) {
toAdd.add(add);
}
for (Integer add : toAdd) {
map.get(key).add(add * docEnd);
}
}
这不是更具可读性吗?
我将"new"用引号引起来,因为这种语法是在 2004 年发布的 Java 5 中引入的。到现在为止,它应该成为所有实践 Java 程序员……和讲师……的标准报告的一部分。
请不要复制粘贴以上代码。这只是为了说明我的观点。
关于java - 对于 ArrayList<Integer> 的每个循环都出现 NoSuchElementException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10255152/
此查询由 2 个相关问题组成。在进行下一步之前,我需要等待元素不可见,因此我尝试定义一个自定义方法,如下所示: public void waitToDisappear(long timeOutInSe
所以我有一个一年前编写的相当大的 java 应用程序,我试图再次理解它。我正在查看代码中的一个方法,其中明显存在出现 NoSuchElementException 的风险:我正在使用任意字符串构造的扫
我开始通过简单地读取数据文件来做练习。当我运行该程序时,数据文件被读取但没有读取,但出于某种原因我仍然收到“NoSuchElementException”并且我的输出没有按照预期的方式格式化。这是正在
我正在尝试逐行读取一个大型 csv 文件,以查找其中字符串出现的次数。 这是执行此操作的代码: public int getOffset(File file, String searched) thr
我的代码在使用扫描仪的每一行上都给出了 NoSuchElementException 异常。我的其余代码工作正常。 import java.io.*; import java.util.*; impo
当我运行我的程序时,我在 Eclipse 控制台中得到了这个: Type a number then press enter. 1. Add a customer 2. Add an order 3.
我想知道是否有人可以帮助解决我的程序中的 NoSuchElements 异常,该程序扫描非常大的文本,然后将其添加到 ArrayList 中。 我尝试重新排列代码的顺序,看看是否可以修复它,但现在我不
for (final ArrayList smartPhones : smartPhonesCluster) { new Thread(new Runnable() { @Ov
我想在客户端读取时设置超时。该例程应该抛出 InterruptedIOException,但它在 System.out.println("echo: " + _in.nextLine()); 上抛出
我在功能检查冲突方面遇到问题。无论我做什么,它都会抛出 NoSuchElementException。这是: public boolean isColliding(Pixel a) { if(
如果在读取文件时缺少字段,我应该捕获 NoSuchElementException。如果缺少一个字段,我只需要跳到文件的下一行。我的问题是,我在哪里实现我的 try/catch 代码来做到这一点?这是
我正在开发一种解析器(业余爱好项目),它采用 Cpp 文件,读取文件中的注释,然后尝试基于该文件创建头文件。 我面临的问题是当java.util.Scanner即将读取第一行时。程序停止并给出 NoS
我的任务是: 创建一个包含 2 列数字的文件:距离和速度。 编写一个 TravelInfo 类,其中包含三部分信息:速度、时间、距离。 该类还应该有一个方法 calcTime(),用于根据距离和速度计
在过去的几个小时里,我一直对迭代器感到震惊。 当我执行Iterator时,得到NoSuchElementException 代码 new Thread() { @Overrid
我尝试了许多不同的方法来修复该错误。我知道这与 hasNext 有关,我只是不确定错误在哪里。请帮忙。另外,如果有人能告诉我如何从 getLargestQuantityItem 方法将变量larges
代码在 Gitbash 命令行 (GitHub) 中正确运行。但是,当我在 NetBeans 中编译并运行它时,它给出了 NoSuchElement Exception。如果有人可以帮助我使用 Net
从队列中删除时,我有时会收到 NoSuchElementException 。我需要使用锁/等待/其他一些延迟机制吗? 我提供了代码的粗略翻译。 带队列的线程 public void run(){ w
如何删除代码中的 NoSuchElementException ?我也在应用 hasNextLine 但它也没有接受它。 public static void main (String[] a
我已经做了一些研究,但一无所获。 这是我的代码: public void play(){ System.out.print("Make your choice ( R - P - S) : "
自从我醒来以来,我一直在尝试让它工作,但我根本无法弄清楚是什么导致了这个异常。 try { scan = new Scanner(new File("dictionary.txt"))
我是一名优秀的程序员,十分优秀!