3

how to handle this exception "ArrayIndexOutOfBoundsException" my code : I create an array of 64 length then I intialized every index then I print the indexes to make sure I am fulling all indexes but it prints up to 63 then gives the exception !! any idea

    public static void main(String [] arg) {
    int [] a=new int [64];
    for(int i=1;i<=a.length;i++){
        a[i]=i;
        System.out.println(i);
    }

}
Raedwald
  • 40,290
  • 35
  • 127
  • 207
Gain
  • 3,153
  • 4
  • 17
  • 10
  • but when i change the for loop to for(int i=0;i – Gain Dec 12 '10 at 08:18
  • 3
    You want to receive an exception? Elaborate what do you want to achieve and we will help you:) – Petar Minchev Dec 12 '10 at 08:19
  • In Java arrays start from index 0 and not 1, hence when your index will exceed 63, (becomes 64), the ArrayIndexOutOfBoundsException is thrown. – Logan Dec 12 '10 at 16:05
  • If you need complete elaboration on why we get this exception, you can go through my answer below. – gprathour Dec 09 '14 at 12:43
  • possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Raedwald Mar 12 '15 at 13:32

7 Answers7

15

The array indexes in Java start from 0 and go to array.length - 1. So change the loop to for(int i=0;i<a.length;i++)

Petar Minchev
  • 44,805
  • 11
  • 98
  • 117
3

Indexes start from 0 so last index is 63. Change your for loop like this:
for(int i=0;i<a.length;i++){

Caner
  • 49,709
  • 33
  • 153
  • 169
3

See the JLS-Arrays:

If an array has n components, we say n is the length of the array; the components of the array are referenced using integer indices from 0 to n - 1, inclusive.

So you have to iterate through [0,length()-1]

for(int i=0;i<a.length;i++) {
    a[i]=i+1;  //add +1, because you want the content to be 1..64
    System.out.println(a[i]);

}
Michael Konietzka
  • 5,183
  • 2
  • 24
  • 29
1

Need Complete Explanation? Read this

The index of an Array always starts from 0. Therefore as you are having 64 elements in your array then their indexes will be from 0 to 63. If you want to access the 64th element then you will have to do it by a[63].

Now if we look at your code, then you have written your condition to be for(int i=1;i<=a.length;i++) here a.length will return you the actual length of the array which is 64.

Two things are happening here:

  1. As you start the index from 1 i.e. i=1 therefore you are skipping the very first element of your array which will be at the 0th index.
  2. In the last it is trying to access the a[64] element which will come out to be the 65th element of the array. But your array contains only 64 elements. Thus you get ArrayIndexOutOfBoundsException.

The correct way to iterate an array with for loop would be:

for(int i=0;i < a.length;i++)

The index starting from 0 and going to < array.length.

gprathour
  • 13,193
  • 5
  • 56
  • 85
0

You've done your math wrong. Arrays begin counting at 0. E.g. int[] d = new int[2] is an array with counts 0 and 1.

You must set your integer 'i' to a value of 0 rather than 1 for this to work correctly. Because you start at 1, your for loop counts past the limits of the array, and gives you an ArrayIndexOutOfBoundsException.

0

In Java arrays always start at index 0. So if you want the last index of an array to be 64, the array has to be of size 64+1 = 65.

//                       start   length
int[] myArray = new int [1    +  64    ];
Robert
  • 2,435
  • 23
  • 24
0

You can correct your program this way :

int i = 0;     // Notice it starts from 0
while (i < a.length) {
    a[i]=i;
    System.out.println(i++);
}
fastcodejava
  • 35,219
  • 24
  • 124
  • 181
  • 4
    I'd rather recommend to use the for-loop, because the loop index is only visible in the loop. In your case, `i` is also visible after the `while` loop. – Mot Dec 12 '10 at 09:45
  • @mklhmnn - Good point, but the main thing is `i` starts from `0`. – fastcodejava Dec 12 '10 at 22:57