[英]comparing two strings in java - compareTo
我在線程“主” java.lang.ClassCastException中不斷獲取異常:不能將studentscoresapp.Student強制轉換為java.lang.Comparable,無法弄清原因。 也許有人可以看看並告訴我我在搞砸。 我一直在弄弄它好幾個小時了,看來我正在使程序變得更糟,而且我在網上發現的任何內容似乎都無法正常工作。 謝謝
public class Student implements IComparable<Student>
{
private String lastName;
private String firstName;
private int score;
public Student(String lastName, String firstName, int score)
{
this.lastName = lastName;
this.firstName = firstName;
this.score = score;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the score
*/
public int getScore() {
return score;
}
/**
* @param score the score to set
*/
public void setScore(int score) {
this.score = score;
}
@Override
public int compareTo(Object obj)
{
Student i = (Student) obj;
if (i.lastName.equals(lastName)) {
return firstName.compareToIgnoreCase(i.firstName);
} else {
return lastName.compareToIgnoreCase(i.lastName);
}
}
}
我的compareTo覆蓋界面
public interface IComparable<E> {
int compareTo(Object object);
}
和學生分數應用程序
public class StudentScoresApp {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Welcome message
System.out.println("Welcome to the Student Scores Application.");
System.out.println();
int count = ConsoleValidator.getInt("Enter number of students to enter: ", 0);
Student[] student = new Student[count];
for (int j = 0; count > j; j++)
{
student[j] = getItem();
System.out.println();
}
Arrays.sort(student);
for (Student i : student)
{
System.out.println(i.getLastName() + " " + i.getFirstName() + ": " + i.getScore() + "\n");
}
}
public static Student getItem()
{
String name = ConsoleValidator.getString("Student first name: ");
String last = ConsoleValidator.getString("Student last name: ");
int score = ConsoleValidator.getInt("Student score: ", 0);
Student j = new Student(name,last,score);
return j;
}
}
異常可能來自此行。
Arrays.sort(student);
在內部Arrays.sort()將嘗試將傳遞的數組的元素強制轉換為Comparable,因此可以調用其compareTo()方法。 這就是為什么您會收到類強制轉換異常的原因。
學生必須實施可比性。 不要使用自己的IComparable接口,而要使用java.lang.Comparable 。
雖然,您應該一直使用標准的Comparable
接口,但是編寫自己的方式也是有缺陷的。 它嘗試使用泛型,但是在聲明方法時放棄了泛型 。 正確的實現應該是
public interface IComparable<T> { // Use of T for types recommended
int compareTo(T object); // Use the generic type for method as well
}
使用標准java.lang.Comparable
接口也一樣,你應該使用像泛型
public class Student implements Comparable<Student> {
// ...
public int compareTo(Student obj) { // No need for casting now
if (obj.lastName.equals(lastName)) {
return firstName.compareToIgnoreCase(obj.firstName);
} else {
return lastName.compareToIgnoreCase(obj.lastName);
}
}
}
對於Array.sort()方法,描述如下:
' 數組中的所有元素都必須實現Comparable接口 '。
您創建接口IComparable而不是Comparable。
我認為這是不正確的。
我將接口更改為Comparable,並且可以正常工作。
任何意見,謝謝分享。
問候,
漢克斯
您想如何對學生進行排序,這是我們如何根據學生的得分對學生進行排序的方法,請在此處輸入代碼
First,Convert Student class into List<Student<String, String, Integer>> studentList
然后使用這段代碼
Collections.sort(studentList, new Comparator<Student>()
{
public int compare(Student c1, Student c2)
{
if (c1.getScore() < c2.getScore())
return -1;
if (c1.getScore() > c2.getScore())
return 1;
return 0;
}
});
這將根據分數以升序對sortedList列表進行排序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.