就是用bubble sort来排序ID,下面有报错的描述
两个class,Student和bubblesort
Student的classpublic class Student
{
 public int ID;
 public String name;
 
 public Student(int ID,String name)
 {
  ID=ID;
  name=name;
 }public int getID()
{
return ID;
}public void setID(int newID)
{
 ID = newID;
}public String getName()
{
return name;
}public void setName(String newName)
{
 name = newName;
}
}bubblesort的classpublic class BubbleSorter2
{
  private int[] a;
  public Student[]student = new Student[5];//////////////
  
  public BubbleSorter2(Student[] anArray)///////////////
  {
    student = anArray;////////////////////
  }
  public void sort()
  {
    boolean swapped;
    int i;
    int j;
    int max = a.length;
    int maxx = student.length;////////////
    
    for(i = max - 1; i>=0; i--)
    {
      swapped = false;
      for(j=0; j< i; j++)
      {
        if (a[j] > a[j+1])
        {
          swap(j,j+1);
          swapped = true;
        }
      }
      if(!swapped) return;
    }
    
    for(i=maxx-1;i>=0;i--)
    {
      swapped = false;
      for(j=0;j<i;j++)
      {
        if(student[j].getID()>student[j+1].getID())
        {
        swap(j,j+1);
        swapped = true;
        }
      }
      if(!swapped) return;
    }
  }  
  private void swap(student[i].getID(), student[j].getID())
上面这行有错报了4个,需要],需要),非法类型开始,需要:
请问应该怎么改啊?谢谢了!!!
  {
    int temp = student[i].getID();
    student[i].getID()= student[j].getID();
    student[j].getID() = temp;
  }
  public Student[] getArray()
  {
    return student;
  }  
}

解决方案 »

  1.   

    大哥,你的swap()方法,参数中的 i从那里来哈,i在for 方法外都消失了,你的问题好多哈
    还有你定义的那个swap()方法,参数定义成 什么类型又不写好···
      

  2.   


    我改成这样好么??谢谢具体说说了。本人初级
      private void swap(int i,int j)
      {
        int temp = student[i].getID();
        student[i]= student[j];
        student[J].getID()= temp;
      }
      

  3.   


     student[J].getID()= temp;
      

  4.   


     student[J].getID()= temp;
    这行报错了
    Error: 意外的类型
      需要: 变量
      找到:    值
    看不明白。
      

  5.   


    class Student {
    private int ID;
    private String name; public Student(int ID, String name) {
    this.ID = ID;
    this.name = name;
    } public int getID() {
    return ID;
    } public void setID(int newID) {
    ID = newID;
    } public String getName() {
    return name;
    } public void setName(String newName) {
    name = newName;
    }
    }// bubblesort的classclass BubbleSorter2 {
    private int[] a; public Student[] student = new Student[5];// //////////// public BubbleSorter2(Student[] anArray)// /////////////
    {
    student = anArray;// //////////////////
    } public void sort() {
    boolean swapped;
    int i;
    int j;
    int max = a.length;// 你的没有定义a的长度不知道要做什么个的
    int maxx = student.length;// ////////// for (i = max - 1; i >= 0; i--) {
    swapped = false;
    for (j = 0; j < i; j++) {
    if (a[j] > a[j + 1]) {
    swap(j, j + 1);
    swapped = true;
    }
    }
    if (!swapped)
    return;
    } for (i = maxx - 1; i >= 0; i--) {
    swapped = false;
    for (j = 0; j < i; j++) {
    if (student[j].getID() > student[j + 1].getID()) {
    swap(j, j + 1);
    swapped = true;
    }
    }
    if (!swapped)
    return;
    }
    } private void swap(int num1, int num2) {
    Student temp = student[num1];
    student[num1] = student[num2];
    student[num2] = temp;
    } public Student[] getArray() {
    return student;
    }
    }
    大概给你改了下  我没有运行  你看看能用不
      

  6.   

    重新给你改了一下  把你多余无用的代码去掉了 已经测试过了//测试类
    public class Test2 {
    public static void main(String[] args) {
    //定义5个student对象
    Student s1 = new Student(1,"a");
    Student s2 = new Student(2,"b");
    Student s3 = new Student(3,"c");
    Student s4 = new Student(4,"d");
    Student s5 = new Student(5,"e");
    //定义一个student数组
    Student[] sArray = new Student[5];
    //将数组赋值
    sArray[0] = s5;
    sArray[1] = s2;
    sArray[2] = s1;
    sArray[3] = s4;
    sArray[4] = s3;
    System.out.println("当前的学生ID顺序是:");
    //打印排序前student的ID顺序
    for(Student array : sArray) {
    System.out.print(array.getID() + " ");
    }
    System.out.println();//上面的for循环结束后换行
    //定义BubbleSorter2对象
    BubbleSorter2 b = new BubbleSorter2(sArray);
    b.sort();
    System.out.println("排序后学生ID顺序是:");
    //打印排序后的5个student ID顺序
    for(Student array : sArray) {
    System.out.print(array.getID() + " ");
    }
    }
    }class Student {
    private int ID;
    private String name;
    //构造方法
    public Student(int ID, String name) {
    this.ID = ID;
    this.name = name;
    } public int getID() {
    return ID;
    } public void setID(int newID) {
    ID = newID;
    } public String getName() {
    return name;
    } public void setName(String newName) {
    name = newName;
    }
    }
    class BubbleSorter2 { private Student[] student = new Student[5];//定义长度为5的student数组
    //构造方法
    public BubbleSorter2(Student[] anArray) {
    student = anArray;
    }
    //排序算法
    public void sort() {
    int max = student.length;//student数组长度
    for (int i = max - 1; i >= 0; i--) {
    for (int j = 0; j < i; j++) {
    //如果student[j]的ID大于student[j+1]的ID,调用swap方法排序
    if (student[j].getID() > student[j + 1].getID()) {
    swap(j, j + 1);
    }
    }
    }
    }

    private void swap(int num1, int num2) {
    Student temp = student[num1];//定义Student类型的中间变量temp接收ID值大的student
    student[num1] = student[num2];
    student[num2] = temp;
    } public Student[] getArray() {
    return student;
    }
    }
      

  7.   

    首先声明本人也是刚学java的菜鸟一枚。如下有不当的地方请大神们指教。
    大概看了下代码,如果我没猜错的画楼主是想按学生的ID号进行排序,那么我想说的是,楼主
    你的private int[] a;这是干嘛的?下面的int max = a.length;这句更神奇了,a的长度根本不知道嘛,那么系统给的这个max就好笑了(在能编译通过的话),还有我觉得会出现错的地方是for (i = max - 1; i >= 0; i--) {
                swapped = false;
                for (j = 0; j < i; j++) {
                    if (a[j] > a[j + 1]) {
                        swap(j, j + 1);
                        swapped = true;
                    }
                }
                if (!swapped)
                    return;
            }这里的if (a[j] > a[j + 1])   如果我没记错的话a这个数组楼主一直没赋值,那么他们怎么进行比较呢???
      

  8.   


    那个a的部分是原先正确代码。那个method是排序数字,我这个基础上自己添了个排序学生ID的。之前那些a的array我就没有删除了好笑神奇是么?下面的代码写了main就能用,你是不是觉得a.length很神奇啊???????
    public class BubbleSorter
    {
      private int[] a;
      
      public BubbleSorter(int[] anArray)
      {
        a = anArray;
      }
      
      public void sort()
      {
        boolean swapped;
        int i;
        int j;
        int max = a.length;
        
        for(i = max - 1; i>=0; i--)
        {
          swapped = false;
          for(j=0; j< i; j++)
          {
            if (a[j] > a[j+1])
            {
              swap(j,j+1);
              swapped = true;
            }
          }
          if(!swapped) return;
        }
      }
      
      private void swap(int i, int j)
      {
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
      }
      
      public int[] getArray()
      {
        return a;
      }  
    }
      

  9.   

    你10楼刚写的这个能用是因为你在调用public BubbleSorter(int[] anArray)这个函数时参数的数组anArray把值传给了a数组(准确的说是把anArray的首地址给了a)这时a数组就有了长度和值,这样的话你在主函数里定义一个anArray数组并初始化,调用BubbleSorter函数当然能用,可是在你发布的程序里根本就没有给a这个数组进行一些必要的操作,所以导致错误。
      

  10.   

    我还没有写main里面,给a几个值
    new BubbleSorter(a).sort();