Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > ArrayIndexOutOfBoundsException

Reply
Thread Tools

ArrayIndexOutOfBoundsException

 
 
tiewknvc9
Guest
Posts: n/a
 
      02-15-2006
Hi.

Im getting an ArrayIndexOutOfBoundsException when using an array, and
while I know I can use a try catch statement to get around this
problem, I was wondering if there was a way to test to see if the
element exists instead.

for instance.

if (myArray[67].exists){
//then do something with myArray[67]
}

thanks.

 
Reply With Quote
 
 
 
 
tiewknvc9
Guest
Posts: n/a
 
      02-15-2006
I got it

if (67 > myArray.length){
//not exception!
}

 
Reply With Quote
 
 
 
 
Alan Krueger
Guest
Posts: n/a
 
      02-16-2006
tiewknvc9 wrote:
> I got it
>
> if (67 > myArray.length){
> //not exception!
> }


Actually, I think you mean "index < myArray.length". If the index is
greater than the length (or even equal to it), you will get an exception.
 
Reply With Quote
 
Finomosec
Guest
Posts: n/a
 
      02-16-2006
Alan Krueger schrieb:
> tiewknvc9 wrote:
>> I got it
>>
>> if (67 > myArray.length){
>> //not exception!
>> }

>
> Actually, I think you mean "index < myArray.length". If the index is
> greater than the length (or even equal to it), you will get an exception.


Be aware, that arrays in Java are zero-based.

A typical loop over an array looks like this:

String[] strings = new String[]{"string0", "string1", "string2"};

// zero-based:
// strings[0] ==> "string0"
// strings[1] ==> "string1"
// strings[2] ==> "string2"
// strings.length == 3

for (int i = 0; i < strings.length; i++) {
String current = strings[i];
// do something with it ...
System.out.println(i + ": " + current);
}

This way you will never get any ArrayIndexOutOfBoundsExceptions ...

Greetings Finomosec;
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      02-17-2006
On Thu, 16 Feb 2006 21:53:25 +0100, Finomosec <>
wrote, quoted or indirectly quoted someone who said :

>Be aware, that arrays in Java are zero-based.


in other words if you have an array of 10 elements ,they are numbered
0 to 9.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
tomcat - java.lang.ArrayIndexOutOfBoundsException: 4096 Simon Bieri Java 2 06-15-2004 08:24 AM
ArrayIndexOutOfBoundsException with DefaultTableModel Aloys Oberthuer Java 3 04-22-2004 04:32 PM
Tomcat 4.1.29 ArrayIndexOutOfBoundsException in Compiler.isOutDated siarsky Java 0 12-23-2003 12:10 PM
ArrayIndexOutOfBoundsException question Johan Java 5 10-15-2003 06:20 PM
JSP page gives ArrayIndexOutOfBoundsException in websphere for z/os Sudhir Java 1 08-27-2003 06:35 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57