为什么会提示数组下标越界呢
public class matrix {
/**
* @param args
*/
public static int max(int table[])
{
int m=table[0];
for(int i=1;i<table.length;i++)
{
if(table[i]>m)
m=table[i];
}
return m;
}
public static int min(int table[])
{
int n=table[0];
for(int i=0;i<table.length;i++)
{
if(table[i]<n)
n=table[i];
}
return n;
}
public static int indexOf(int table[],int key)
{
int index=-1;
for(int i=0;i<table.length;i++)
{
if(key==table[i])
{
index=i+1;
break;
}
}
return index;
}
public static void sort(int table[])
{
for(int i=table.length-1,number=0;i>=1;i--)
for(int j=0;j<i;j++)
{
if(table[j]>table[j+1])
{
number=table[j+1];
table[j+1]=table[j];
table[j]=number;
}
}
}
public static boolean isSorted(int table[])
{
for(int i=table.length-1;i>=0;i--)
{
if(table[i]<table[i-1])
return false;
}
return true;
}
public static void print(int table[])
{
for(int i=0;i<table.length;i++)
System.out.print(table[i]+"\t");
System.out.println();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int matrix[]={9,5,13,18,19,5,3,2,7,9,6,24,21};
System.out.println("the max number is: "+max(matrix));
System.out.println("the min number is: "+min(matrix));
if(indexOf(matrix,5)==-1)
System.out.println("the number is not in the matrix!");
else
{
System.out.println("the index of the number is: "+indexOf(matrix,5));
}
System.out.print("the number in matrix are:"); print(matrix);
sort(matrix);
System.out.println("the matrix is sorted?"+"\t"+(isSorted(matrix)?"YES":"NO"));
System.out.print("the number in matrix are:"); print(matrix);
}
}
已解决 不要去运行程序 看你眼力如何了 能找出问题不