Java 使用单独的气泡排序类根据double字段对对象进行排序
我目前有以下学生班Java 使用单独的气泡排序类根据double字段对对象进行排序,java,eclipse,sorting,bubble-sort,Java,Eclipse,Sorting,Bubble Sort,我目前有以下学生班 public class Student { String name; String address; String major; double gpa; int ClassLevel; int college; String idNumber; public Student(String name, String address, String major, double gpa, int ClassLevel, int college, String idNum
public class Student
{
String name;
String address;
String major;
double gpa;
int ClassLevel;
int college;
String idNumber;
public Student(String name, String address, String major, double gpa, int ClassLevel, int college, String idNumber)
{
this.name = name;
this.address = address;
this.major = major;
this.gpa = gpa;
this.ClassLevel = ClassLevel;
this.college = college;
this.idNumber = idNumber;
}
public String toString()
{
return name + " is a student enrolled in college number " + college + " majoring in " + major + " with a GPA of " + gpa + ". He is studying his year " + ClassLevel + " and his M# is " + idNumber + ". His address is " + address + ".";
}
}
我还有一个类,它从文本文件中读取数据,创建一个包含5个学生对象的数组
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadStudent
{
public static void main(String[] args)
{
Scanner fileIn = null;
String name = null;
String address = null;
String major = null;
double gpa = 0;
int ClassLevel = 0;
int college = 0;
String idNumber = null;
try
{
fileIn = new Scanner (new FileInputStream("student.dat"));
}
catch (FileNotFoundException e)
{
System.out.println("file not found");
System.exit(0);
}
Student[] object = new Student[5];
int i = 0;
while (fileIn.hasNextLine() && i <= 4)
{
name = fileIn.nextLine();
address = fileIn.nextLine();
major = fileIn.nextLine();
gpa = fileIn.nextDouble();
ClassLevel = fileIn.nextInt();
college = fileIn.nextInt();
idNumber = fileIn.nextLine();
fileIn.nextLine();
fileIn.nextLine();
object[i] = new Student(name, address, major, gpa, ClassLevel, college, idNumber);
i++;
}
for (int j =0; j<i; j++)
System.out.println(object[j]);
}
}
我试图在这些课程之外,用泡泡排序法,通过GPA对这些学生进行排序
BubbleSort类:
public class BubbleSort
{
public static void sort (int n, double [] array)
{
double tempVar;
for (int i = 0; i<n-1; i++)
{
for (int j = 0; j<n-1; j++)
{
if (array[i] > array[j + 1])
{
tempVar = array [j+1];
array [j+1] = array[i];
array [i] = tempVar;
}
}
}
}
}
}
我不知道该如何称呼排序。。。BubbleSort.sort[4,??]为什么不将一个student数组传递给该方法,并作为数组[i].gpa>数组[j+1].gpa进行比较?注意,您可能需要提供一个getter来访问gpa 一种更灵活的方法是使用可比较的接口或比较器,就像Collections.sort。。。是的
为什么不将一个student数组传递给该方法,并作为array[i].gpa>array[j+1].gpa进行比较呢?注意,您可能需要提供一个getter来访问gpa 一种更灵活的方法是使用可比较的接口或比较器,就像Collections.sort。。。是的
执行数组[i].compareTo数组[j+1]>0并让compareTo返回-1,0,1@kai你到底是什么意思?我的示例中有错误吗?没有,但是compareTo的标准实现应该返回-1、0或1。如果你使用comparator.compare数组[i],数组[j+1]>1,它将始终为false。还是我错了?太好了!谢谢你的帮助!执行数组[i].compareTo数组[j+1]>0并让compareTo返回-1,0,1@kai你到底是什么意思?我的示例中有错误吗?没有,但是compareTo的标准实现应该返回-1、0或1。如果你使用comparator.compare数组[i],数组[j+1]>1,它将始终为false。还是我错了?太好了!谢谢你的帮助!
public static <T extends Comparable<T>> void sort( int n, T[] array ) {
T tempVar;
for( int i = 0; i < n - 1; i++ ) {
for( int j = 0; j < n - 1; j++ ) {
if( array[i].compareTo( array[j + 1]) > 0 ) {
tempVar = array[j + 1];
array[j + 1] = array[i];
array[i] = tempVar;
}
}
}
}
class Student implements Comparable<Student> {
public int compareTo( Student s ) {
//compare gpa here
}
}
public static <T> void sort( int n, T[] array, Comparator<T> comparator ) {
T tempVar;
for( int i = 0; i < n - 1; i++ ) {
for( int j = 0; j < n - 1; j++ ) {
if( comparator.compare( array[i], array[j + 1]) > 0 ) {
tempVar = array[j + 1];
array[j + 1] = array[i];
array[i] = tempVar;
}
}
}
}
BubbleSort.sort( students.length, students, new Comparator<Student>() {
public int compare(Stundent lhs, Student rhs ) {
//compare gpa
}
});