Java 使用Comparable接口时,compareTo()方法不会覆盖默认方法
我试图通过编写自己的并使用implements comparable来覆盖java中的默认compareTo()方法,但是java似乎仍然在使用默认方法 我试图按照从.dat文件中获得的长度对字符串数组进行排序,但是它一直按照字母顺序进行排序。如果有人能告诉我我做错了什么,我将不胜感激,因为我不知道为什么这不起作用 谢谢Java 使用Comparable接口时,compareTo()方法不会覆盖默认方法,java,interface,comparable,compareto,Java,Interface,Comparable,Compareto,我试图通过编写自己的并使用implements comparable来覆盖java中的默认compareTo()方法,但是java似乎仍然在使用默认方法 我试图按照从.dat文件中获得的长度对字符串数组进行排序,但是它一直按照字母顺序进行排序。如果有人能告诉我我做错了什么,我将不胜感激,因为我不知道为什么这不起作用 谢谢 import static java.lang.System.*; import java.util.Arrays; public class Word implements
import static java.lang.System.*;
import java.util.Arrays;
public class Word implements Comparable
{
private String word;
private String[] array;
public Word()
{
word = "";
}
public Word(String s)
{
word = s;
}
public void setWord(String s)
{
word = s;
}
public int compareTo(String rhs)
{
String temp = (String)rhs;
if(word.length() > temp.length())
return 1;
else if(word.length() < temp.length())
return -1;
return 0;
}
public void setSize(int size)
{
array = new String[size];
}
public void add(int spot, String other)
{
array[spot] = other;
}
public String[] sortByLength()
{
Arrays.sort(array);
return array;
}
public String toString()
{
return Arrays.toString(array);
}
}
导入静态java.lang.System.*;
导入java.util.array;
公共类Word实现了可比较的
{
私有字符串字;
私有字符串[]数组;
公共词()
{
单词=”;
}
公共字(字符串s)
{
word=s;
}
公共无效设置字(字符串s)
{
word=s;
}
公共整数比较(字符串rhs)
{
字符串温度=(字符串)rhs;
if(word.length()>temp.length())
返回1;
else if(word.length()
下面是包含main方法的类
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.Arrays;
import static java.lang.System.*;
public class Lab18d
{
public static void main( String args[] ) throws IOException
{
Scanner file = new Scanner(new File("lab18d.dat"));
int size = file.nextInt();
file.nextLine();
Word test = new Word();
test.setSize(size);
String word = "";
for(int i = 0; i < size; i++)
{
word = file.next();
test.setWord(word);
test.add(i, word);
}
test.sortByLength();
System.out.println(test);
}
}
导入java.io.File;
导入java.io.IOException;
导入java.util.Scanner;
导入java.util.array;
导入静态java.lang.System.*;
公共类Lab18d
{
公共静态void main(字符串args[])引发IOException
{
扫描仪文件=新扫描仪(新文件(“lab18d.dat”);
int size=file.nextInt();
file.nextLine();
单词测试=新单词();
测试。设置尺寸(尺寸);
字串=”;
对于(int i=0;i
简短版本:您需要使用
长版本:行
Arrays.sort(array);
在sortByLength
方法中,该方法对正在排序的对象不断调用compareTo
方法,而这些对象就是字符串!相反,你需要排队
Arrays.sort(array, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
if (s1.length() > s2.length())
return 1;
if (s1.length() < s2.length())
return -1;
return 0;
}
});
Arrays.sort(数组,新比较器(){
@凌驾
公共整数比较(字符串s1、字符串s2){
如果(s1.length()>s2.length())
返回1;
如果(s1.length()
或者,您可以创建一个单独的类来实现
比较器
,并使用该类的实例作为数组的第二个参数。sort
检查compareTo方法的签名
它应该是int compareTo(对象o)
您正在将public int compareTo(字符串rhs)
您还可以向方法添加
@Override
注释。如果您没有遵循正确的签名,它会让您知道。帮自己一个忙:每次重写方法时,都要在其中添加@override
注释。如果您在重写方法时出错,这将导致编译错误,这就是这里发生的情况。您实现它是错误的,因为Comparable
(Comparable
的“原始”形式没有声明方法compareTo(String)
,而是声明方法compareTo(Object)
要使其按原样编译,您需要接受对象而不是字符串
,或者实现可比
而不是可比
但这在大多数情况下都是不正确的,因为这样的比较是不对称的:可以将单词与字符串进行比较,但不能将字符串与单词进行比较
您很可能希望实现Comparable
而不是Comparable
,并接受Word
到compareTo()
请注意,Comparable
只有在类型本身是有序的(文档称之为“自然顺序”)时才是非常合适的,比如日期或数字。因为您实际上并不是按字母顺序比较这两个单词(最接近字符串的自然顺序)这是使用外部比较器的更好的候选者
//由于Word.Word是私有成员,因此需要将其嵌套在Word中
//或者Word.Word需要提供一个访问器方法
公共静态类LengthComparator实现Comparator{
@凌驾
公共整数比较(Word word1、Word word2){
返回Integer.valueOf(word1.word.length()).compareTo(word2.word.length());
}
}
已键入,但您使用的是原始类型。请尝试以下操作:
public class Word implements Comparable<Word> { // Note: typing of Comparable
...
public int compareTo(Word rhs) { // Note: parameter is typed
String temp = rhs.word;
return word.length() - temp.length(); // Note: Simplification of code
}
}
public类Word实现了Comparable{//注意:键入Comparable
...
public int compareTo(Word rhs){//注意:参数已键入
字符串temp=rhs.word;
返回word.length()-temp.length();//注意:代码的简化
}
}
您的链接指向Java 1.4文档,这些文档是在将泛型添加到语言之前编写的。如果您在查看Java 6文档,您将看到实现Comparable
的类中compareTo方法的正确签名确实是public int compareTo(字符串rhs)
。也就是说,发布的代码实现了原始的Comparable
而不是Comparable
,这是不受欢迎的。这仍然不能解决sortByLength
方法是排序字符串列表,而不是排序Word
对象列表的问题。我做了这些更改,它编译得很好(就像以前一样),但它仍然是按字母顺序对数组进行排序。哇,我不知道这与类中其他代码的上下文是否完全一致。我很确定,如果我将所有内容放在一行中,而不是放在一行中,即:array.sort(array);为什么不呢?这是一个匿名类的例子,你可以在
//since Word.word is a private member, this either needs to be nested inside of Word
//or Word.word would need to be given an accessor method
public static class LengthComparator implements Comparator<Word> {
@Override
public int compare(Word word1, Word word2) {
return Integer.valueOf(word1.word.length()).compareTo(word2.word.length());
}
}
public class Word implements Comparable<Word> { // Note: typing of Comparable
...
public int compareTo(Word rhs) { // Note: parameter is typed
String temp = rhs.word;
return word.length() - temp.length(); // Note: Simplification of code
}
}