java - 覆盖 compareTo 方法以便按字符串值对对象进行排序

标签 java compareto

我有一个实现 Comparable 接口(interface)的类。在此类中,我需要重写 compareTo 方法以便按字符串值对对象进行排序。

如果向下滚动到底部,我正在尝试制作我的方法,我需要在主要方法中按第一个部门类型然后按类(class)编号对类(class)数组(使用我正在制作的 compareTo 方法)进行排序等,但这需要比较 2 个字符串。

   import java.io.Serializable;
   import java.io.*;
   import java.util.*;
public class Course implements Comparable<Course>, Serializable  {
private String prefix;
private int number;
private String Department;
private String grade;
/**
 * Constructs the course with the specified information.
 * 
 * @param prefix the prefix of the course designation
 * @param number the number of the course designation
 * @param Department the Department of the course
 * @param grade the grade received for the course
 */
public Course(String prefix, int number, String Department, String grade)
{
    this.prefix = prefix;
    this.number = number;
    this.Department = Department;
    if (grade == null)
        this.grade = "";
    else
        this.grade = grade;
}

/**
 * Constructs the course with the specified information, with no grade
 * established.
 * 
 * @param prefix the prefix of the course designation
 * @param number the number of the course designation
 * @param Department the Department of the course
 */
public Course(String prefix, int number, String Department)
{
    this(prefix, number, Department, "");
}

public String getPrefix()
{
    return prefix;
}

/**
 * Returns the number of the course designation.
 * 
 * @return the number of the course designation
 */
public int getNumber()
{
    return number;
}

/**
 * Returns the Department of this course.
 * 
 * @return the prefix of the course
 */
public String getDepartment()
{
    return Department;
}
/**
 * Returns the grade for this course.
 * 
 * @return the grade for this course
 */
public String getGrade()
{
    return grade;
}

/**
 * Sets the grade for this course to the one specified.
 * 
 * @param grade the new grade for the course
 */
public void setGrade(String grade)
{
    this.grade = grade;
}

/**
 * Returns true if this course has been taken (if a grade has been received).
 * 
 * @return true if this course has been taken and false otherwise
 */
public boolean taken()
{
    return !grade.equals("");
}

 * Determines if this course is equal to the one specified, based on the
 * course designation (prefix and number).
 * 
 * @return true if this course is equal to the parameter
 */
public boolean equals(Object other)
{
    boolean result = false;
    if (other instanceof Course)
    {
        Course otherCourse = (Course) other;
        if (prefix.equals(otherCourse.getPrefix()) &&
                number == otherCourse.getNumber())
            result = true;
    }
    return result;
}

compareTo 函数:

public int compareTo(Course o)
{
    if(getDepartment().equals(o.getDepartment()))
    {
        return 0;
    }
    else if()
    {
        return -1;
    }
    else
    {
        return 1;
    } 
}   

/**
 * Creates and returns a string representation of this course.
 * 
 * @return a string representation of the course
 */
public String toString()
{
    String result = prefix + " " + number + ": " + Department;
    if (!grade.equals(""))
        result += "  [" + grade + "]";
    return result;
    }
}

到目前为止的主要类(class):

import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;
import java.*;


public class StackCourse 
{

 public static void main(String[] args) 
    {
        Course a = new Course("EEE", 230, "Engineering");
        Course b = new Course("MAT", 150, "Liberal Arts");
        Course c = new Course("PHY", 150, "Liberal Arts");
        Course d = new Course("PHI", 304, "Liberal Arts");
        Course e = new Course("ECN", 214, "W.P. Carey");
        Course f = new Course("EEE", 120, "Engineering");


        Course[] courses = {a,b,c,d,e,f};
        for(int i=0; i<courses.length; i++)
        System.out.println(courses[i].getDepartment());       
    }
}

最佳答案

public int compareTo(Course o)
{
    if(getDepartment().compareTo(o.getDepartment()) ==0){
        if(getNumber() < o.getNumber()) return -1;
        else if(getNumber() > o.getNumber()) return 1;
        else return 0;
    }

    return getDepartment().compareTo(o.getDepartment());

}  

输出:

EEE 120:工程学 EEE 230:工程 垫 150:文科 PHY 150:文科 PHI 304:文科 ECN 214:W.P.凯里

关于java - 覆盖 compareTo 方法以便按字符串值对对象进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24601604/

相关文章:

java - 如何在java中的通用数组中使用compareTo?

java - 使用 compareTo 实现 equals 方法

java - Jersey 端点在查询参数中使用 '+'

Java 要求覆盖 jfilechooser

java - 一个字符串中有多少个点。 ( java )

java - 比较具有相同父抽象类的两个对象,输出为零

Java 比较方法。 Student.compareTo 方法应该返回

java - 带有空参数的准备语句

java - 我在 Netbeans 中的 java 项目变得很慢

Java:可以将日期作为字符串进行比较吗