日期:2014-05-20  浏览次数:20657 次

请教高手看几行bubble sort的代码,谢谢
就是用bubble sort来排序ID,下面有报错的描述。。。
两个class,Student和bubblesort
Student的class

public 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的class

public 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;
  }  
}



------解决方案--------------------
重新给你改了一下  把你多余无用的代码去掉了 已经测试过了

//测试类
public class Test2 {