0

Why does this not work. What I want to do is add a new string of text to the array called items

final String items[] = {"Java", "JSP", "PHP", "C", "C++"};
int itemsl = items.length + 1;
items[itemsl] = "f";

this is the error out put

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
  • 1
    1) "Why does this not work?" Your compiler/JVM should be pretty explicit on that. Copy/paste the output if you do not understand it. 2) Please learn how to use the code formatting. Select the code & activate the {} button above the message posting form. Commented Apr 19, 2012 at 14:04
  • The syntax you used to declare the "items" variable actually initializes it at the same time. And per definition, arrays in Java are not extensible. Commented Apr 19, 2012 at 14:06
  • "want to do is add a new" Use a collections such as a Vector or ArrayList. Thay have methods to add new items. Commented Apr 19, 2012 at 14:06
  • 2
    Because Java is not PHP. And thank God. Commented Apr 19, 2012 at 14:12
  • I got it thx to every for the help. Commented Apr 19, 2012 at 14:54

6 Answers 6

4

You can't change the number of elements in the array in Java. Create a new one and copy the elements, or use one of the Collection classes e.g. ArrayList.

Sign up to request clarification or add additional context in comments.

Comments

4

You actually have two misunderstandings around arrays here.

First, an array is of fixed size, determined at creation. In this case, you have an array of 5 elements. You can replace elements, but you can't add or substract after the array is created.

Besides this, even if you could, array.length is one position past the end of the array already, adding 1 to it puts you two past the end of the array.

This is because arrays are zero based, so an array with a length of 5 has elements zero through 4, so even doing items[items.length] will result in ArrayIndexOutOfBoundsException.

Comments

1

items[itemsl] is out of bound, since the array size is smaller then itemsl!

Thus you get an ArrayIndexOutOfBoundException.

If you are looking for a dynamic array (array which its size is modifiable) - you will probably want to use an ArrayList<String> instead of a String[].

Comments

1

I've been out of Java for a couple of years...but anyway, I don't think you can dynamically change the array like that.

Your statement int itemsl = items.length + 1 is creating an int that is out of bounds of the array when used here items[itemsl] = "f";

Consider using one of the Java Collections to manage your array.

Comments

0

You have to create a new array. Once you've created one you cannot change it's size.

Comments

0

The problem has nothing to do with final. You are writing beyond the boundary of the array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.