質問

I currently have the following Student class

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 + ".";

}

}

I have another class that reads data from a text file to create an array of 5 Student objects

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

I am trying to sort these students by GPA using a bubble sort outside of these classes

BubbleSort class:

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

I am unsure how to call the sort... BubbleSort.sort [4, ???]

役に立ちましたか?

解決

Why don't you pass a student array to the method instead and do the comparison as array[i].gpa > array[j + 1].gpa? (Note that you might have to provide a getter to access gpa).

A more flexible way would be the use of either the Comparable interface or a Comparator, much like Collections.sort(...) does.

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

With a comparator you wouldn't have to implement Comparable but provide a suitable instance of Comparator<Student>, e.g.

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
  }
});
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top