0

为什么这不起作用。我想要做的是在名为 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)
4

6 回答 6

4

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

于 2012-04-19T14:03:35.313 回答
4

您实际上对这里的数组有两个误解。

首先,数组的大小是固定的,在创建时就确定了。在这种情况下,您有一个包含 5 个元素的数组。可以替换元素,但是数组创建后不能加减。

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

这是因为数组是从零开始的,所以长度为 5 的数组的元素是零到 4,所以即使这样做items[items.length]也会导致 ArrayIndexOutOfBoundsException。

于 2012-04-19T14:09:04.373 回答
1

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

因此,您会得到一个ArrayIndexOutOfBoundException

如果您正在寻找一个动态数组(其大小可修改的数组) - 您可能想要使用 anArrayList<String>而不是String[].

于 2012-04-19T14:03:38.993 回答
1

我已经离开 Java 几年了……但无论如何,我认为您不能像那样动态地更改数组。

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

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

于 2012-04-19T14:06:28.837 回答
0

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

于 2012-04-19T14:04:20.240 回答
0

问题与final. 您正在写超出数组的边界。

于 2012-04-19T14:04:41.850 回答