English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories
0

What is the code to deal with an ArrayIndexOutOfBoundsException?

2007-05-16 06:01:54 · 1 answers · asked by DanCorb 3 in Computers & Internet Programming & Design

1 answers

Sounds like you are exceeding the allocated array size. Keep in mind that Java arrays are indexed from 0 not 1 too.

String[] abc = new String[2];

abc[0] = new String("test0");
abc[1] = new String("test1");

// throws ArrayIndexOutOfBoundsException
abc[2] = new String("error...array not this big");

You could put your code in a try block but I'd recommend checking your logic a bit. If you are reading an array of unknown size, consider the following:

public void printArray (String[] anArray) {

// Loop through the array and print the results.
for (int i = 0; i < anArray.length; i++ ) {
System.out.println("Array index " + i + ": " + anArray[i]);
}
}

2007-05-16 06:13:13 · answer #1 · answered by Jim Maryland 7 · 0 0

fedest.com, questions and answers