Java 向数组中添加新项时是否使用ArrayIndexOutOfBounds?

Java 向数组中添加新项时是否使用ArrayIndexOutOfBounds?,java,arrays,exception,indexoutofboundsexception,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

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

在Java中,不能更改数组中的元素数。创建一个新的项目并复制元素,或使用一个集合类,例如ArrayList。

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

这样你就得到了一份工作


如果您要查找其大小可修改的数组,则可能需要使用一个而不是字符串[]。

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

这个问题与最终版本无关。您正在编写超出数组边界的内容。

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

您的语句int itemsl=items.length+1正在创建一个int,当在此处使用items[itemsl]=f时,该int超出了数组的界限


考虑使用一个Java集合来管理您的数组。

实际上,您对数组有两个误解

首先,数组的大小是固定的,在创建时确定。在本例中,您有一个由5个元素组成的数组。可以替换元素,但不能在创建数组后添加或删除元素

除此之外,即使可以,array.length已经超过数组末尾一个位置,向其中添加1会使您超过数组末尾两个位置


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

1为什么这不起作用?在这方面,编译器/JVM应该非常明确。如果您不理解输出,请复制/粘贴输出。2请学习如何使用代码格式。选择代码并激活消息发布表单上方的{}按钮。您用于声明items变量的语法实际上同时对其进行初始化。根据定义,Java中的数组是不可扩展的。我们要做的是在集合中添加一个新的用法,如Vector或ArrayList。他们有添加新项目的方法,因为Java不是PHP。感谢上帝,我把它送给了每一个人,感谢他们的帮助。