首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

看几行bubble sort的代码,多谢

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

请教高手看几行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 {


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

热点排行
Bad Request.