class sort
{
public static void main(String args[])
{
//int a[]=new int[2];
int a[]={6,11,5};
for(int i=0;i<=a.length;i++)
{
for(int j=i+1;j<=i;j++)
{
if(a[j]<a[j+1])
{
int temp;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
System.out.println(a[i]);
}
}
}
这段代码到底错在哪?怎么没实现排序??
{
public static void main(String args[])
{
//int a[]=new int[2];
int a[]={6,11,5};
for(int i=0;i<=a.length;i++)
{
for(int j=i+1;j<=i;j++)
{
if(a[j]<a[j+1])
{
int temp;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
System.out.println(a[i]);
}
}
}
这段代码到底错在哪?怎么没实现排序??
class sort
{
public static void main(String args[])
{
//int a[]=new int[2];
int a[]={6,11,5};
for(int i=0;i <a.length;i++)
{
for(int j=i;j <a.length-1-i;j++)
{
if(a[j] <a[j+1])
{
int temp;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp; }
}
System.out.println(a[i]);
} }
}
出现
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at sort.main(sort.java:11)这样的提示??
class sort {
public static void main(String args[]) {
int a[] = { 6, 11, 5 };
for (int i = 0; i < a.length; i++) { //数组下标的最大值,为length-1
for (int j = i + 1; j < a.length; j++) {
if (a[i] < a[j]) {
int temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
System.out.println(a[i]); //每次比较最大的输出
}
}
}
改为i<a.length