gpt4 book ai didi

Java索引越界异常索引:2,大小:2

转载 作者:行者123 更新时间:2023-12-02 10:07:49 25 4
gpt4 key购买 nike

这是我收到的错误。如果您需要有关完整代码的更多信息,请告诉我。

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)

这是我的代码,我不明白它有什么问题 i+1 不应该越界...(不太擅长编程,所以我不太明白问题所在。)

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);

最佳答案

您在第一行错误中看到:索引: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 ---> 这就是你出界的地方。

关于Java索引越界异常索引:2,大小:2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55211581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com