21

Here's my code:

int myArray[]={1,2,3,4,5,6,7,8};

for(int counter=myArray.length; counter > 0;counter--){
    System.out.println(myArray[counter]);
}

I'd like to print out the array in descending order, instead of ascending order (from the last element of the array to the first) but I just get thrown this error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
    at task1.main(task1.java:14)

Why is this happening? I was hoping that by using myArray.length to set the counter to 8, the code would just print out the 8th element of the array and then keep printing the one before that.

JimmyK
  • 4,801
  • 8
  • 35
  • 47
  • I don't know the array indexing style in Java, but if it is same as that in C, you are trying to print the array element out of the array bounds in the first iteration.. – Mallik Feb 21 '12 at 14:46
  • Just a tip : If you use Intellij IDEA, typing myArray.forr (r for reverse) and pressing enter at the suggestion will generate the loop For normal loop, you can type myArray.fori – Praveen E Jun 05 '19 at 17:33

7 Answers7

83

Arrays in Java are indexed from 0 to length - 1, not 1 to length, therefore you should be assign your variable accordingly and use the correct comparison operator.

Your loop should look like this:

for (int counter = myArray.length - 1; counter >= 0; counter--) {
  • I had this but instead of taking away 1 from the counter I just left it as is. Nice work. – James111 Nov 24 '15 at 01:55
  • If I don't care about the index, could we do it like this type `for (int i : nums)` this way but with backwards sequence. – Zhou Haibo Mar 22 '21 at 12:22
6
  • The first index is 0 and the last index is 7 not 8
  • The size of the array is 8
VirtualTroll
  • 3,077
  • 1
  • 30
  • 47
6

use myArray.length-1

  for(int counter=myArray.length-1; counter >= 0;counter--){
                System.out.println(myArray[counter]);
            }
Jama A.
  • 15,680
  • 10
  • 55
  • 88
3

The problem here is this piece of code: myArray.length. In Java, as in most other languages, Data structures are 0 based, so the last element has an index of structure.length - 1 (and the first being 0). So in your case, you should change your loop as follows:

for(int counter=myArray.length - 1; counter >= 0;counter--){
            System.out.println(myArray[counter]);
        }
npinti
  • 51,780
  • 5
  • 72
  • 96
2

You're starting at the wrong index. Do it like this:

for(int counter= myArray.length - 1; counter >= 0;counter--) {

The last index of an array is its length minus 1.

fivedigit
  • 18,464
  • 6
  • 54
  • 58
2

the counter is starting at the index of myArray.length which is actually counted from 1 instead of 0..

    for(int counter=myArray.length - 1; counter > 0; counter--){
smassey
  • 5,875
  • 24
  • 37
0

int myArray[]={1,2,3,4,5,6,7,8};

Here, given array length is 8 as the count starts from 1 but coming for the index myArray[0] = 1; and so on.... here index count starts from 0. So in your piece of code

for(int counter = myArray.length - 1; counter >= 0; counter--) {  

goes out of the array boundary so it shows you ArrayIndexOutOfBoundsException.

Andrew Li
  • 55,805
  • 14
  • 125
  • 143