就是用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;
}
}
两个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;
}
}
还有你定义的那个swap()方法,参数定义成 什么类型又不写好···
我改成这样好么??谢谢具体说说了。本人初级
private void swap(int i,int j)
{
int temp = student[i].getID();
student[i]= student[j];
student[J].getID()= temp;
}
student[J].getID()= temp;
student[J].getID()= temp;
这行报错了
Error: 意外的类型
需要: 变量
找到: 值
看不明白。
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;
}
}
大概给你改了下 我没有运行 你看看能用不
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;
}
}
大概看了下代码,如果我没猜错的画楼主是想按学生的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这个数组楼主一直没赋值,那么他们怎么进行比较呢???
那个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;
}
}
new BubbleSorter(a).sort();