can someone please review this and tell me what is the problem ? cant i pass a reference to an array
to a constructor or what ? thanks
...
class Sort {
int[] input ;
int key=0;
Sort(int[] k){
//Sometimes a method will need to refer to the object that invoked it.
this.input= k ;
}
int[] returnArray() {
for(int i=2 ; i<=input.length ; i++){
key=input[i];
int j=i-1;
while (j>0 && input[j]>key){
input[j+1]=input[j];
j-=1;
}
input[j+1]=key;
}
return input ;
}
}
class InsertionSort{
public static void main (String[] args){
int[] A = {5,8,99,52,22,14,15,1,25,15585,36,244,8,99,25,8};
Sort sort = new Sort(A);
int[] B = sort.returnArray();
for (int i=0 ; i<B.length ; i++){
System.out.println("the array after sorting : ");
System.out.print( B[i] + " " );
}
}
}
...
and this is the exact contents of the whatsitssname :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 16 out of bounds for length 16<br/>
at Sort.returnArray(InsertionSort.java:10)<br/>
at InsertionSort.main(InsertionSort.java:25)<br/>