问题
I have been asking questions about creating a sorting method to sort a linked list of contact names read in from a text file and have advanced from my previous question:(What is a better method to sort strings alphabetically in a linked list that is reading in lines from a text file?), but am now running into two errors after creating a separate class called AlphaSorter.java with the method compareTo() which is intended to override my sort() method in my Linked List program/class called ContactList.java.
I receive the two errors:
AlphaSorter must implement the inherited abstract method java.lang.Comparable.compareTo(AlphaSorter)
The method compareTo(ContactNode, ContactNode) of type AlphaSorter must override or implement a supertype method
This is the Linked List program: 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;}}}
This is the Node 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;}}
This is the main method: 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. ");}}}
Finally this is the Sorting 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;}}
I am unsure of how exactly the CompareTo() method works with the import packages. Could someone explain? Thank you!
回答1:
The Comparable
interface is for those classes that can compare instances of their own class for a "natural order".
However, you are attempting to create a separate class for this purpose. It looks like you are trying to implement Comparator
, because you have a compareTo
method that compares objects of a different class, ContactNode
.
Implement Comparator<ContactNode>
instead.
class AlphaSorter implements Comparator<ContactNode>{
Also, in Comparator, the method is compare, not compareTo
.
@Override
public int compare(ContactNode e1, ContactNode e2) {
回答2:
You need to implement the compareTo
method properly:
@Override
public int compareTo(ContactNode other) {
return getName().compareTo(other.getName());
}
Do not confuse Comparable
(which has a one-argument compareTo
method, which compares the current object with another object) with Comparator
(which has a two-argument compare
method, and compares two other objects).
来源:https://stackoverflow.com/questions/27912749/how-to-fix-the-error-alphasorter-must-implement-the-inherited-abstract-method