将新项添加到Array时的ArrayIndexOutOfBounds?

时间:2012-04-19 14:00:25

标签: java arrays exception indexoutofboundsexception

为什么这不起作用。我想要做的是向名为items

的数组添加一个新的文本字符串
final String items[] = {"Java", "JSP", "PHP", "C", "C++"};
int itemsl = items.length + 1;
items[itemsl] = "f";

这是错误输出

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 6
    at com.modinstaller.guii$4.actionPerformed(guii.java:127)
    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$000(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$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.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$1.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)

6 个答案:

答案 0 :(得分:4)

您无法在Java中更改数组中的元素数。创建一个新元素并复制元素,或使用其中一个Collection类,例如ArrayList

答案 1 :(得分:4)

这里对阵列实际上有两个误解。

首先,数组具有固定大小,在创建时确定。在这种情况下,您有一个包含5个元素的数组。您可以替换元素,但在创建数组后无法添加或减去。

除此之外,即使你可以,array.length已经超过数组末尾的一个位置,向它添加1会使你超过数组的末尾。

这是因为数组是基于零的,所以长度为5的数组的元素为0到4,所以即使执行items[items.length]也会导致ArrayIndexOutOfBoundsException。

答案 2 :(得分:1)

items[itemsl]超出范围,因为数组大小小于itemsl

因此你得到ArrayIndexOutOfBoundException

如果您正在寻找dynamic array(其大小可修改的数组) - 您可能希望使用ArrayList<String>代替String[]

答案 3 :(得分:1)

我已经离开了Java几年......但无论如何,我认为你不能像这样动态地改变数组。

您的语句int itemsl = items.length + 1正在创建一个在此处使用时超出数组范围的int items[itemsl] = "f";

考虑使用其中一个Java集合来管理您的阵列。

答案 4 :(得分:0)

您必须创建一个新数组。一旦你创建了一个,你就无法改变它的大小。

答案 5 :(得分:0)

问题与final无关。你正在超越数组的边界写作。