java迭代器抛出awt-eventqueue异常

时间:2019-03-23 16:45:28

标签: java iterator

我正在编写一个程序,该程序将项目添加到列表,并使用迭代器将所有项目打印到JTextArea。我有些困惑,因为我以为是因为如果没有next,我试图打印一个空语句,但是while循环的条件不是检查迭代器中是否有下一个对象吗? >

我知道问题仅在println上,因为如果删除它,程序运行正常。但是,有没有一种方法可以以字符串形式访问迭代器中的下一个对象?

while (iter.hasNext())
  {
     System.out.println(iter.next().toString());
     i++;
     r = i + " x " + formatter.formatLineItem(iter.next());
  }

这是堆栈跟踪:

 Exception in thread "AWT-EventQueue-0" 
 java.lang.IndexOutOfBoundsException: Index 2 out-of-bounds for length 2
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.get(ArrayList.java:440)
at Invoice$1.next(Invoice.java:56)
at Invoice$1.next(Invoice.java:48)
at Invoice.format(Invoice.java:80)

它抛出“ AWT-Eventqueue-0”异常。我目前正在使用println来查看迭代器中的确切项目,因为我想按名称对每个项目进行分类。但是,我希望能够访问迭代器中项目的字符串。

0 个答案:

没有答案