java - 使用单独的冒泡排序类根据 double 字段对对象进行排序

标签 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 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 个 Student 对象的数组

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

我正在尝试在这些类(class)之外使用冒泡排序按 GPA 对这些学生进行排序

冒泡排序类:

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, ???]

最佳答案

为什么不将学生数组传递给该方法,并以 array[i].gpa > array[j + 1].gpa 的形式进行比较? ? (请注意,您可能必须提供一个 getter 才能访问 gpa)。

更灵活的方法是使用 Comparable界面或 Comparator ,很像 Collections.sort(...)

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

有了比较器,你就不必实现 Comparable但提供 Comparator<Student> 的合适实例,例如

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

关于java - 使用单独的冒泡排序类根据 double 字段对对象进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22328181/

相关文章:

java - 在Android中部署SimpleNLG,NoClassDefFoundError

database - 如何在 Windows 中使用的 Eclipse 中设置 Prolog Interpreter 路径?

java - 跨 Eclipse 插件共享域对象的最佳方式?

javascript - 如何使用javascript jquery按字母顺序对表格内的默认td进行排序

java - 是否可以通过点表示法将一系列值流式传输/映射到一个结果?

c# - Java 和 Windows 之间使用的软件架构风格的建议

java - 字符索引到和来自字节索引

java - 替换所有非 latin-1 字符的 API 或方法

c# - 自定义列表<string[]> 排序

c++ - 对相互依赖的对象的 C++ vector 进行排序的简短解决方案?