for (j = i - 1; j > 0; j--) {                if (temp < ary[j]) {                    ary[j + 1] = ary[j];                } else {                    break;                }

解决方案 »

  1.   

    刚才手贱了,你把 for (j = i - 1; j > 0; j--) {中的 j>0 改为 j>=0 就ok了;
      

  2.   


    非常感谢,这个问题解决了.然后我把它包装了一下,包装之后输出却是有问题的,麻烦你看一看.package test;public class Test { public static void main(String[] args) {
    int[] ary = { 5, 8, 4, 1, 6, 4, 7 };
    InsertSortf is=new InsertSortf();
    is.sort(ary);
    for (int k = 0; k < ary.length; k++) {
    System.out.print(ary[k] + " ");
    }
    }}
    class  InsertSortf{
    public void sort(int[] ary){
    int j;
    for (int i = 1; i < ary.length; i++) {
    int temp = ary[i];
    for (j = i - 1; j >= 0; j--) {
    if (temp < ary[j]) {
    ary[j + 1] = ary[j];
    } else {
    }
    }
    ary[j + 1] = temp;
    }
    }
    }
      

  3.   


    非常感谢,这个问题解决了.然后我把它包装了一下,包装之后输出却是有问题的,麻烦你看一看.package test;public class Test { public static void main(String[] args) {
    int[] ary = { 5, 8, 4, 1, 6, 4, 7 };
    InsertSortf is=new InsertSortf();
    is.sort(ary);
    for (int k = 0; k < ary.length; k++) {
    System.out.print(ary[k] + " ");
    }
    }}
    class  InsertSortf{
    public void sort(int[] ary){
    int j;
    for (int i = 1; i < ary.length; i++) {
    int temp = ary[i];
    for (j = i - 1; j >= 0; j--) {
    if (temp < ary[j]) {
    ary[j + 1] = ary[j];
    } else {
    }
    }
    ary[j + 1] = temp;
    }
    }
    }
    晕死,问题已经解决.忘记写 break;了
      

  4.   


    非常感谢,这个问题解决了.然后我把它包装了一下,包装之后输出却是有问题的,麻烦你看一看.package test;public class Test { public static void main(String[] args) {
    int[] ary = { 5, 8, 4, 1, 6, 4, 7 };
    InsertSortf is=new InsertSortf();
    is.sort(ary);
    for (int k = 0; k < ary.length; k++) {
    System.out.print(ary[k] + " ");
    }
    }}
    class  InsertSortf{
    public void sort(int[] ary){
    int j;
    for (int i = 1; i < ary.length; i++) {
    int temp = ary[i];
    for (j = i - 1; j >= 0; j--) {
    if (temp < ary[j]) {
    ary[j + 1] = ary[j];
    } else {
    }
    }
    ary[j + 1] = temp;
    }
    }
    }
    晕死,问题已经解决.忘记写 break;了
    哥们,仔细一点嘛