Überschreiben von compareTo-Methode, um Objekte Sortieren von String-Werten
Habe ich eine Klasse implementiert, die Vergleichbare Schnittstelle. In dieser Klasse muss ich überschreiben von compareTo-Methode, um Objekte Sortieren von String-Werten.
Wenn Sie nach unten scrollen, ich werde versuchen, meine Methode & ich muss in der main-Methode ein array Sortieren von Kursen (mit der compareTo-Methode, die ich mache) von der ersten Abteilung geben und dann durch die Kurs-Nummer, etc. aber dies erfordert Vergleich von 2 strings.
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;
}
Den compareTo
Funktion:
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;
}
}
Main-Klasse so weit:
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());
}
}
Können Sie näher auf Ihre Frage?
In der main-Methode: Kurs a = new Kurs("EEE", 230, "Maschinenbau"); Kurs b = new Kurs("MAT", 150, "Liberal Arts"); Kurs c = new Kurs("PHY", 150, "Liberal Arts"); Kurs[] Kurse = {a,b,c}; Nun Sortieren Kurse, erste Abteilung geben, dann...
Können Sie fügen Sie Ihre 'main' - Methode, um die Frage Körper?
Warum sind Sie der Rückkehr
die 'main' - Methode Hinzugefügt. Nun möchte ich dieses array Sortieren, mit Hilfe der compareTo-Methode, die ich bin versucht zu erstellen.
In der main-Methode: Kurs a = new Kurs("EEE", 230, "Maschinenbau"); Kurs b = new Kurs("MAT", 150, "Liberal Arts"); Kurs c = new Kurs("PHY", 150, "Liberal Arts"); Kurs[] Kurse = {a,b,c}; Nun Sortieren Kurse, erste Abteilung geben, dann...
Können Sie fügen Sie Ihre 'main' - Methode, um die Frage Körper?
Warum sind Sie der Rückkehr
-1
, 0
, und 1
, wenn Sie nur könnten zurück getDepartment().compareTo(o.getDepartment())
?die 'main' - Methode Hinzugefügt. Nun möchte ich dieses array Sortieren, mit Hilfe der compareTo-Methode, die ich bin versucht zu erstellen.
InformationsquelleAutor Adam Staples | 2014-07-07
Du musst angemeldet sein, um einen Kommentar abzugeben.
Ausgabe:
EEE 120: Engineering
EEE 230: Engineering
MAT 150: Liberal Arts
PHY-150: Liberal Arts
PHI 304: Liberal Arts
ECN 214: W. P. Carey
Bubble-sort?...
Ich benutzte Sammlungen.sort(Liste); // ArrayList<Verlauf>
Hmm.. Kann ich die code?
Nur was anders ist, ist die compareTo-Methode, die ich oben gepostet... Hier ist die main-Methode : List<Verlauf> Liste =new ArrayList<Verlauf>(); Liste.add(a); Liste.add(b); Liste.add(c); Liste.add(d); Liste.add(e); Liste.add(f); Sammlungen.sort(Liste); for(int i=0;i< - Liste.size();i++){ System.aus.println(list.get(i)); }
InformationsquelleAutor Maheedhar Pamuru
Wenn ich Ihre Frage, ich glaube, Sie wollte so etwas
Hinweis: Sie können zurück, wenn die Felder, die Sie vergleichen nicht
0
, da die Felder nicht gleich sind.InformationsquelleAutor Elliott Frisch
Nur die Verwendung von String-built-in
compareTo
Methode:Sicher... warum nicht? Es ist ein anderes Objekt einer anderen Klasse.
InformationsquelleAutor shmosel