-2

Here's my code:

import java.util.Arrays;

public class TwelveInts {

    public static void main(String[] args) {

        int bucky[]={1,2,3,4,5,6,7,8,9,10,11,12};
        System.out.println("First to last: ");
        for(int i = 0; i<bucky.length; i++){
            System.out.print(bucky[i] + " ");
        }
        System.out.println("");

        System.out.println("Opposite order :) ");
        **for(int i = bucky.length; i >=0; i--){**
            System.out.print(bucky[i] + " ");
        }


    }

}

I realize that the line of problematic code is this:

for(int i = bucky.length; i >=0; i--)

It should be written as follows to get rid of the exception:

for(int i = bucky.length - 1; i >=0; i--){

I just don't understand why though. I know that the reason is because it's due to an illegal index, but I just don't understand this exception error in application to this problem. If someone can help me understand why this exception occurs in this problem without the "-1", that would be great. I'm just having a really hard time visualizing it for this.

ihoaxed
  • 127
  • 2
  • 16
  • https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html – Tom Apr 04 '16 at 23:58
  • Because arrays are 0 based, and the length of the array is the number of elements. `int[] bucky = { 1, 2, 3};` The length of the array is 3, but has elements [0], [1], [2]. If you try to access `bucky[bucky.length]` that is [3] which doesn't exist in this example. – KevinO Apr 04 '16 at 23:59

6 Answers6

3

The index of arrays and lists are 0-based.

So the first element of the array is accessed with array[0], the last element is accessed with array[array.length - 1]

Zero-based indices are used in nearly all programming languages. There are good reasons for doing so, as eloquently explained by Edsger Dijkstra here: https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html.

wvdz
  • 16,251
  • 4
  • 53
  • 90
  • i really appreciate your answer. I think I honestly had a brain fart...Thanks for clearing this up :) – ihoaxed Apr 05 '16 at 00:08
1

Because the indexing is starting from 0, though the when you are addressing size it does not count zero so if you have 2 element inside, the first will be addressed at index 0, but the size will be 2 of the array, so by saying size 2 you address a non existing index.

for instance:

int []array={1,2,3}; // the size is 3, so array.length will return 3. but the last index will be 2 as indexing starts from 0. so if you address 3 it will be an exception out of bounds

vsarunov
  • 1,433
  • 14
  • 26
1

for(int i = bucky.length - 1; i >=0; i--) is the problem,

Arrays and lists beginn with the index 0

Imagine you have an array of String[] a = {A, B, C, D}

So the length of the array is 4, because it contains 4 elements. So a.length == 4. However, the first element of the array has the index 0 (and not 1). Therefore

a[0] = A
a[1] = B
a[2] = C
a[3] = D

But a[4] is out of bounds

sockeqwe
  • 15,574
  • 24
  • 88
  • 144
1

For this array:

int bucky[]={1,2,3,4,5,6,7,8,9,10,11,12};

bucky.length is 12 (array has 12 elements) therefore the indexes are from 0 to 11 (arrays are always zero based). Do not confuse the elements in the array with the indexes themselves (bucky[0], bucky[1], ... bucky[11]).

So, for reverse order you need to traverse the elements using indexes from 11 (which is bucky.length-1) down to 0, otherwise the for loop attempts to access bucky[bucky.length] same as bucky[12] which throws the ArrayIndexOutOfBoundsException.

Jose Cifuentes
  • 596
  • 6
  • 21
1

Your array bucky[]={1,2,3,4,5,6,7,8,9,10,11,12}; has 12 elements its length is 12 but you can not try to read the element bucky[12], you can go from bucky[0 ] to bucky[11] OR from bucky[11] to bucky[0] what ever your logic is..

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
1

The for loop starts with i = bucky.length. Let me explain why this is out of bounds. Mathematically the nth element is at index n-1. 1st element is at index 1-1 (0)

So visually if your array bucky is [1,2,3,4,5] , bucky[0] == 0, bucky[1] == 1, etc up to bucky[4] == 5. Your array has 5 elements so it's length is 5, but the last element is at index 4.

So to the index of the last element in the array is bucky.length-1 . From here you can see that if you try to access the index at bucky.length you will be accessing an index that is beyond the last index in the array. Your for loop is starting with i = bucky.length, hence why starting it with i=bucky.length-1 fixes the problem.

tt_Gantz
  • 2,786
  • 3
  • 23
  • 43