Home arrow static arrow Java Programming [Archive] - ArrayIndexOutOfBounds question
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - ArrayIndexOutOfBounds question
This topic has 6 replies on 1 page.

Posts:111
Registered: 1/14/04
ArrayIndexOutOfBounds question  
Aug 6, 2004 6:08 PM



 
What is the difference between

ArrayIndexOutOfBoundsException : -10

and

ArrayIndexOutOfBoundsException : 10>=10

Thanks in advance for helping out.

 

Posts:12,831
Registered: 2/22/00
Re: ArrayIndexOutOfBounds question  
Aug 6, 2004 6:13 PM (reply 1 of 6)



 

"-10" is different from "10>=10".

Well, that's what you asked.

Without seeing your code, I'm guessing that the first case happened because you tried to select from an array with a negative index, and the second happened because you tried to select from an array using a boolean expression or a string rather than an integer. Probably the latter since I can't imagine how the former would be detected by the JVM. But I could be wrong.
 

Posts:111
Registered: 1/14/04
Re: ArrayIndexOutOfBounds question  
Aug 6, 2004 6:34 PM (reply 2 of 6)



 
"-10" is different from "10>=10".

Well, that's what you asked.

Without seeing your code, I'm guessing that the first case happened because you tried to select from an array with a negative index, and the second happened because you tried to select from an array using a boolean expression or a string rather than an integer. Probably the latter since I can't imagine how the former would be detected by the JVM. But I could be wrong.

When does an array have a negative index??

In this case --> 10>=10, I happen to encounter this error when the index i selected is equal or greater than the size of the array.

Example:

array [0,1,2,3,4,5,6,7,8,9]
index selected: 10

On the other hand, with this case --> -10
I don't quite get what causes this error.

Can somebody explain to me?

 

Posts:1,495
Registered: 6/16/03
Re: ArrayIndexOutOfBounds question  
Aug 6, 2004 6:34 PM (reply 3 of 6)



 
The error messages are giving you the information you need to diagnose your error, nothing more. The lower bound of an array index is always 0, so it doesn't need to tell you that -10 < 0. The upper bound can vary depending on the size of you array, so the JVM generates a message that tells you not only what index you tried to access, but also the size of the array. This is why it tells you 10 >= 10, the first 10 is the index you tried to access, and the second 10 gives you the size of your array.
 

Posts:111
Registered: 1/14/04
Re: ArrayIndexOutOfBounds question  
Aug 6, 2004 6:48 PM (reply 4 of 6)



 
Yes I understand. But I have encountered this type of error:

ArrayIndexOutofBoundsException: -40

What does it mean?
I normally encounter null pointer or ArrayIndexOutofBoundsException: n>=n but not this one.
 

Posts:4,130
Registered: 3/31/99
Re: ArrayIndexOutOfBounds question  
Aug 6, 2004 7:16 PM (reply 5 of 6)



 
Yes I understand. But I have encountered this type of
error:

ArrayIndexOutofBoundsException: -40

What does it mean?
I normally encounter null pointer or
ArrayIndexOutofBoundsException: n>=n but not this one.

The code below generates that error and shows how it occurs
int[] stuff = new int[10];int index = -40;int v = stuff[index]; 
 

Posts:111
Registered: 1/14/04
Re: ArrayIndexOutOfBounds question  
Aug 6, 2004 7:22 PM (reply 6 of 6)



 
ahh i see. thanks to everybody!
 
This topic has 6 replies on 1 page.