简体   繁体   English

Java索引超出范围例外索引:2,大小:2

[英]Java index out of bounds exception index:2, Size:2

here is the error that I am getting. 这是我得到的错误。 If you need more information regarding the full code let me know. 如果您需要有关完整代码的更多信息,请告诉我。

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)                         

Here is my Code and I don't get what is wrong with it i+1 should not be out of bounds...(Not very good at programming so I don't quite see the problem through.) 这是我的代码,我不明白它有什么问题,我不应该超出范围(+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);

You see on the first line of errors: Index: 2, Size: 2 您会在错误的第一行看到:索引:2,大小:2

if(list.size() >= 2) {
    temp = temp + list.get(i+1);
}

So here at the condition is met, because the list size is 2. 因此这里满足条件,因为列表大小为2。

temp = temp + list.get(i+1); --> here is the problem. ->这是问题所在。

You're saying: get(i+1) which is equal to 2, but the index is actually 1. First iteration = i+1 => 0+1 = 1 Second iteration = i+1 => 1+1 = 2 ---> this is where you're getting out of bound. 您的意思是:get(i + 1)等于2,但索引实际上是1。第一次迭代= i+1 => 0+1 = 1第二次迭代= i+1 => 1+1 = 2 --->这就是您要越界的地方。

In java arrays and lists start at index 0 and end at index size - 1 . 在java数组和列表中,从索引0开始,以索引size - 1结束。 So if in your loop is i = 1 then list.get(i + 1) will cause an IndexOutOfBoundsException , because you are trying to access index 2 which does not exist. 因此,如果您的循环中i = 1list.get(i + 1)将导致IndexOutOfBoundsException ,因为您正尝试访问不存在的索引2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM