这是我遇到的错误。如果您需要有关完整代码的更多信息,请告诉我。
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at main.Field.upCountFirst(Field.java:158)
at main.Field$1.actionPerformed(Field.java:55)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
这是我的代码,我不明白它有什么问题,我不应该超出范围……((不太擅长编程,所以我不太清楚这个问题。)>
list.add(x);
String temp = x;
for(int i = 0; i<list.size(); i++) {
System.out.println(list.get(i) + " X" + list.size());
if(list.size() >= 2) {
temp = temp + list.get(i+1);
}
}
System.out.println(temp);
答案 0 :(得分:1)
您会在错误的第一行看到:索引:2,大小:2
if(list.size() >= 2) {
temp = temp + list.get(i+1);
}
所以这里满足条件,因为列表大小为2。
temp = temp + list.get(i+1);
->这是问题所在。
您是说:get(i + 1)等于2,但索引实际上是1。
第一次迭代= i+1 => 0+1 = 1
第二次迭代= i+1 => 1+1 = 2
--->这是您超越界限的地方。
答案 1 :(得分:0)
在Java数组和列表中,索引起始于索引0
,结束于索引size - 1
。因此,如果您的循环中是i = 1
,那么list.get(i + 1)
将导致IndexOutOfBoundsException
,因为您正在尝试访问不存在的索引2
。