Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

sorting - How to fix the error: " AlphaSorter must implement the inherited abstract method java.lang.Comparable<AlphaSorter>.compareTo(AlphaSorter)?

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:

  1. AlphaSorter must implement the inherited abstract method java.lang.Comparable.compareTo(AlphaSorter)

  2. 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() + "
");
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!

question from:https://stackoverflow.com/questions/65861758/the-type-account-must-implement-the-inherited-abstract-method-comparableaccount

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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) {

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...