8.10: Netbeans

Asked by peter

Hello

I asked this question already in the Netbeans forum, but unfortunately I haven't been getting an answer. I've the following Java code:

int[] anArray = {37, 23, 11, 87, 63, 55, 17, 1, 46, 88};
int temp1;
int temp2;

for (int x1 = 0; x1 < 11; x1++) {
      for (int x2 = 1; x2 < 11; x2++) {
            temp1 = anArray[x1]:
            temp2 = anArray[x2]:
}

If I run it, I always get the following error message:

run:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
        at x.Main.main(Main.java:29)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

If I click "Main.java:29", the cursor will be placed in the line "temp2 = anArray[x2]:".

Can you tell me why I get this error? And what can I do to prevent it? Thanks.
Peter

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Solved by:
peter
Solved:
Last query:
Last reply:
Revision history for this message
Roberto Lizana (trey.es) (robertolizana) said :
#1

The problem not is NetBeans, your code have a bug. A array start at 0.

int[] anArray = {37, 23, 11, 87, 63, 55, 17, 1, 46, 88};
int temp1;
int temp2;

for (int x1 = 0; x1 < anArray.length ; x1++) {
      for (int x2 = 1; x2 < anArray.length; x2++) {
            temp1 = anArray[x1]:
            temp2 = anArray[x2]:
}

Revision history for this message
peter (peter-neuweiler) said :
#2

Thanks a lot, man. It works. I knew that the array starts at 0. And I knew that I wrote a bug.

I'm completely new to Java.Thank you!