How to fix the error: " AlphaSorter must implement the inherited abstract method java.lang.Comparable<AlphaSorter>.compareTo(AlphaSorter)?
How to fix the error: " AlphaSorter must implement the inherited abstract method java.lang.Comparable<AlphaSorter>.compareTo(AlphaSorter)?
我一直在询问有关创建排序方法的问题,以对从文本文件中读取的联系人姓名链接列表进行排序,并从我之前的问题中取得进展:(What is a better method to sort strings alphabetically in a linked list that is reading in lines from a text file?),但现在 运行 在使用方法 compareTo() 创建一个名为 class 的单独 class 后变成两个错误,该方法旨在覆盖我的链表 program/class 中名为 [=41] 的 sort() 方法=].
我收到两个错误:
AlphaSorter 必须实现继承的抽象方法java.lang.Comparable.compareTo(AlphaSorter)
AlphaSorter 类型的方法 compareTo(ContactNode, ContactNode) 必须覆盖或实现超类型方法
这是链表程序:ContactList.java
public class ContactList{
private ContactNode head;
private ContactNode last;
public ContactNode current;
public ContactList(){
head = null;
last = null;
current = null;}
public void addNode(ContactNode input){
if(this.head == null)
{this.head = input;
this.last = input;}
else
last.setNext(input);
input.setPrev(last);
this.last = input;}
public void traverse(){
System.out.println();
current = this.head;
while (current != null){
System.out.print(current.getName() + " ");
System.out.println("");
current = current.getNext();}
System.out.println();}
@Override
public String toString(){
ContactNode current = head;
while(current!=null){
System.out.print(current.getName() + "\n");
current = current.getNext();}
return null;}
public void insertNewFirstNode(String current){
ContactNode newNode = new ContactNode(current);
head = newNode;
if(last == null){
last = head;}}
public void sort(){
ContactList sorted = new ContactList();
ContactNode current = head
while (current != null){
if((current.getName() != null)){
current.getName().compareTo(current.getName());
sorted.insertNewFirstNode(current.getName());}
else if((current != null)){current = current.getNext();}
System.out.println(toString() + sorted);
System.out.println("");
System.out.println("");
break;}}}
这是节点 Class:ContactNode.java
public class ContactNode{
public String name;
public int index;
private ContactNode prev;
public ContactNode next;
ContactNode(String a){
this.name = a;
index = 0;
next = null;
prev = null;}
ContactNode(){}
public ContactNode getNext()
{return next;}
public ContactNode getPrev()
{return prev;}
public String getName()
{return name;}
public int getIndex(){
return index;}
public void setNext(ContactNode newnext)
{next = newnext;}
public void setPrev(ContactNode newprevious)
{prev = newprevious;}
public void setName(String a)
{name=a;}
public void setIndex(int b)
{index=b;}}
这是主要方法:ContactMain.java
import java.util.Scanner;
import java.io.FileReader;
import java.io.FileNotFoundException;
public class ContactMain{
public static void main(String[]args){
try{
FileReader filepath = new FileReader("data1.txt");
Scanner k = new Scanner(filepath);
ContactList myList = new ContactList();
while (k.hasNextLine()){
String i = k.nextLine();
myList.addNode(new ContactNode(i));}
myList.traverse();
System.out.println("");
myList.sort();
}catch (FileNotFoundException e){
System.out.println("File Not Found. ");}}}
最后是排序 class:AlphaSorter.java
import java.util.Comparator;
import java.io.Serializable;
class AlphaSorter implements Comparable<AlphaSorter>{
@Override
public int compareTo(ContactNode e1, ContactNode e2) {
return e1.getName().compareTo(e2.getName());}
public boolean equals(Object obj){
return this==obj;}}
我不确定 CompareTo() 方法如何与导入包一起工作。有人可以解释吗?谢谢!
您需要正确实施compareTo
方法:
@Override
public int compareTo(ContactNode other) {
return getName().compareTo(other.getName());
}
不要混淆 Comparable
(它有一个单参数 compareTo
方法,它将当前对象与另一个对象进行比较)和 Comparator
(它有一个双参数 compare
方法,并比较另外两个对象)。
Comparable
界面适用于那些可以将自己的 class 实例与 "natural order".
进行比较的 classes
但是,您正试图为此创建一个单独的 class。看起来您正在尝试实现 Comparator
,因为您有一个 compareTo
方法来比较不同 class、ContactNode
的对象。
改为实施 Comparator<ContactNode>
。
class AlphaSorter implements Comparator<ContactNode>{
此外,在 Comparator
, the method is compare
中,而不是 compareTo
。
@Override
public int compare(ContactNode e1, ContactNode e2) {
我一直在询问有关创建排序方法的问题,以对从文本文件中读取的联系人姓名链接列表进行排序,并从我之前的问题中取得进展:(What is a better method to sort strings alphabetically in a linked list that is reading in lines from a text file?),但现在 运行 在使用方法 compareTo() 创建一个名为 class 的单独 class 后变成两个错误,该方法旨在覆盖我的链表 program/class 中名为 [=41] 的 sort() 方法=].
我收到两个错误:
AlphaSorter 必须实现继承的抽象方法java.lang.Comparable.compareTo(AlphaSorter)
AlphaSorter 类型的方法 compareTo(ContactNode, ContactNode) 必须覆盖或实现超类型方法
这是链表程序:ContactList.java
public class ContactList{
private ContactNode head;
private ContactNode last;
public ContactNode current;
public ContactList(){
head = null;
last = null;
current = null;}
public void addNode(ContactNode input){
if(this.head == null)
{this.head = input;
this.last = input;}
else
last.setNext(input);
input.setPrev(last);
this.last = input;}
public void traverse(){
System.out.println();
current = this.head;
while (current != null){
System.out.print(current.getName() + " ");
System.out.println("");
current = current.getNext();}
System.out.println();}
@Override
public String toString(){
ContactNode current = head;
while(current!=null){
System.out.print(current.getName() + "\n");
current = current.getNext();}
return null;}
public void insertNewFirstNode(String current){
ContactNode newNode = new ContactNode(current);
head = newNode;
if(last == null){
last = head;}}
public void sort(){
ContactList sorted = new ContactList();
ContactNode current = head
while (current != null){
if((current.getName() != null)){
current.getName().compareTo(current.getName());
sorted.insertNewFirstNode(current.getName());}
else if((current != null)){current = current.getNext();}
System.out.println(toString() + sorted);
System.out.println("");
System.out.println("");
break;}}}
这是节点 Class:ContactNode.java
public class ContactNode{
public String name;
public int index;
private ContactNode prev;
public ContactNode next;
ContactNode(String a){
this.name = a;
index = 0;
next = null;
prev = null;}
ContactNode(){}
public ContactNode getNext()
{return next;}
public ContactNode getPrev()
{return prev;}
public String getName()
{return name;}
public int getIndex(){
return index;}
public void setNext(ContactNode newnext)
{next = newnext;}
public void setPrev(ContactNode newprevious)
{prev = newprevious;}
public void setName(String a)
{name=a;}
public void setIndex(int b)
{index=b;}}
这是主要方法:ContactMain.java
import java.util.Scanner;
import java.io.FileReader;
import java.io.FileNotFoundException;
public class ContactMain{
public static void main(String[]args){
try{
FileReader filepath = new FileReader("data1.txt");
Scanner k = new Scanner(filepath);
ContactList myList = new ContactList();
while (k.hasNextLine()){
String i = k.nextLine();
myList.addNode(new ContactNode(i));}
myList.traverse();
System.out.println("");
myList.sort();
}catch (FileNotFoundException e){
System.out.println("File Not Found. ");}}}
最后是排序 class:AlphaSorter.java
import java.util.Comparator;
import java.io.Serializable;
class AlphaSorter implements Comparable<AlphaSorter>{
@Override
public int compareTo(ContactNode e1, ContactNode e2) {
return e1.getName().compareTo(e2.getName());}
public boolean equals(Object obj){
return this==obj;}}
我不确定 CompareTo() 方法如何与导入包一起工作。有人可以解释吗?谢谢!
您需要正确实施compareTo
方法:
@Override
public int compareTo(ContactNode other) {
return getName().compareTo(other.getName());
}
不要混淆 Comparable
(它有一个单参数 compareTo
方法,它将当前对象与另一个对象进行比较)和 Comparator
(它有一个双参数 compare
方法,并比较另外两个对象)。
Comparable
界面适用于那些可以将自己的 class 实例与 "natural order".
但是,您正试图为此创建一个单独的 class。看起来您正在尝试实现 Comparator
,因为您有一个 compareTo
方法来比较不同 class、ContactNode
的对象。
改为实施 Comparator<ContactNode>
。
class AlphaSorter implements Comparator<ContactNode>{
此外,在 Comparator
, the method is compare
中,而不是 compareTo
。
@Override
public int compare(ContactNode e1, ContactNode e2) {