0

我正在尝试使用 System.out.print() 打印数组的内容,但遇到此错误:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.get(ArrayList.java:382)
at Polymorphism.actionPerformed(Polymorphism.java:196)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

错误所指的行是第 3 行:

 else if (e.getSource() == printArray) {
        for (int q=0; q<j; q++){
            System.out.print("Type: " + itemList.get(j).selectedType + "    ");
            System.out.print("Width: " + itemList.get(j).selectedWidth + "    ");
            System.out.print("Length: " + itemList.get(j).selectedLength + "    ");
    }
}

我没有任何调试器经验,所以我很感激任何帮助,谢谢。

4

7 回答 7

4

正如 Renjith 所说,更改为.get(j),您还可以确保永远不会遇到您所处的情况

代替

for (int q=0; q<j; q++) {

for (int q=0; q<itemList.size(); q++) {

这样你就不能从任何大于列表/数组大小的东西中获取

于 2013-01-28T05:51:02.367 回答
2

我猜你指的是错误的索引。

将 itemList.get(j) 更改为 itemList.get(q) ;

于 2013-01-28T05:47:24.277 回答
2

代替

        System.out.print("Type: " + itemList.get(j).selectedType + "    ");
        System.out.print("Width: " + itemList.get(j).selectedWidth + "    ");
        System.out.print("Length: " + itemList.get(j).selectedLength + "    ");

   System.out.print("Type: " + itemList.get(q).selectedType + "    ");
   System.out.print("Width: " + itemList.get(q).selectedWidth + "    ");
   System.out.print("Length: " + itemList.get(q).selectedLength + "    ");

原因是您正在使用列表大小的查找值遍历列表。

当循环索引(始终为 < listsize)大于 listsize(在本例中为 2)时,条件被打破

于 2013-01-28T05:52:59.347 回答
1

那是大错特错!采用:

else if (e.getSource() == printArray) {
        for (int q=0; q<j; q++){
            System.out.print("Type: " + itemList.get(q).selectedType + "    ");
            System.out.print("Width: " + itemList.get(q).selectedWidth + "    ");
            System.out.print("Length: " + itemList.get(q).selectedLength + "    ");
    }
}
于 2013-01-28T05:49:35.330 回答
1

您正在使用此代码打印元素

 itemList.get(j).selectedType

你需要使用这个

 itemList.get(q).selectedType
于 2013-01-28T05:56:18.073 回答
0

如果j是您的项目列表的大小,itemList.get(j)它将给您一个超出范围的索引,因为从零开始的索引只会转到 j - 1。itemList.get(q)从您的代码看起来像您想要的那样。

于 2013-01-28T05:49:10.510 回答
0

看来这是一个pretty common mistake. 如果您正在使用 any IDE like Eclipse,您应该考虑了解更多关于Debug Perspective.

于 2013-01-28T05:52:51.017 回答