Substituant la méthode compareTo afin de trier les objets par la Chaîne de valeurs

J'ai une classe qui implémente l'interface Comparable. Dans cette classe j'ai besoin de remplacer méthode compareTo afin de trier les objets par la Chaîne de valeurs.

Si vous faites défiler vers le bas, je suis d'essayer de faire ma méthode & j'ai besoin de dans la principale méthode de tri d'un tableau de Cours (à l'aide de la méthode compareTo je suis en train de faire) par le premier Département type puis par numéro de cours, etc. mais cela nécessite la comparaison de 2 chaînes de caractères.

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

La compareTo fonction:

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

Classe principale jusqu'à présent:

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());       
}
}
  • Pouvez-vous revenir sur votre question?
  • Dans la méthode main: Cours a = nouveau Cours("EEE", 230, "Génie"); le cadre du Cours b = nouveau Cours("MAT", 150, "Arts Libéraux"); Cours c = nouveau Cours("PHY", 150, "Arts Libéraux"); Cours[] cours = {a,b,c}; Maintenant trier les cours de première par Département type, alors...
  • Pouvez-vous ajouter votre principale méthode pour la question du corps?
  • Pourquoi êtes-vous de retour -1, 0, et 1, où vous pouviez juste retour getDepartment().compareTo(o.getDepartment())?
  • la principale méthode est ajoutée. Maintenant, je voudrais trier ce tableau à l'aide de la méthode compareTo je suis d'essayer de créer.
InformationsquelleAutor Adam Staples | 2014-07-07