forum

Home / DeveloperSection / Forums / number.length-1 in an array, what does the “-1” stands for

number.length-1 in an array, what does the “-1” stands for

Anonymous User181105-Oct-2013

I am new to Java. I got this from a website and it does bubblesort. I doubt why is there "-1" after the "number.length". I just don't quite get it. Grateful if anyone could help.

import java.util.Arrays;
public class Bubblesort {
  public static void main(String[] args) {
    int[] number = {5,16,4,32,30};
    int j;
    boolean flag = true;   // set flag to true to begin first pass
    int temp;   //holding variable
    while ( flag )
    {
          flag= false;    //set flag to false awaiting a possible swap
          for( j=0;  j <number.length-1;  j++ )
          {
                 if ( number[ j ] > number[j+1] )   // change to > for ascending sort
                 {
                         temp = number[ j ];                //swap elements
                         number[ j ] = number[ j+1 ];
                         number[ j+1 ] = temp;
                        flag = true;              //shows a swap occurred
                }
          }
    }
    System.out.println(Arrays.toString( number ));
  }
}


Updated on 05-Oct-2013
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By