Java 尝试对链接列表中的节点和元素使用compareTo时发生类强制转换错误

Java 尝试对链接列表中的节点和元素使用compareTo时发生类强制转换错误,java,linked-list,nodes,compareto,Java,Linked List,Nodes,Compareto,我已经研究了这方面的一系列问题,但找不到一个能具体解决我问题的 基本上,这是一个作业分配,我有一个带有节点的链表,其中包含一个元素。节点类(LinearNode)和元素类(Golfer)都实现Comparable并重写compareTo方法。但是,运行时尝试将新节点添加到列表时失败(第一个节点添加成功),出现类强制转换异常:supersenior.LinearNode无法强制转换为supersenior.Golfer。我不知道为什么它试图获取节点并将其与要比较的节点的元素进行比较……我甚至尝试了

我已经研究了这方面的一系列问题,但找不到一个能具体解决我问题的

基本上,这是一个作业分配,我有一个带有节点的链表,其中包含一个元素。节点类(LinearNode)和元素类(Golfer)都实现Comparable并重写compareTo方法。但是,运行时尝试将新节点添加到列表时失败(第一个节点添加成功),出现类强制转换异常:supersenior.LinearNode无法强制转换为supersenior.Golfer。我不知道为什么它试图获取节点并将其与要比较的节点的元素进行比较……我甚至尝试了显式强制转换。观察到以下错误:

Exception in thread "main" java.lang.ClassCastException: supersenior.LinearNode cannot      be cast to supersenior.Golfer
at supersenior.Golfer.compareTo(Golfer.java:12)
at supersenior.LinearNode.compareTo(LinearNode.java:80)
at supersenior.LinearNode.compareTo(LinearNode.java:80)
at supersenior.LinkedList.add(LinkedList.java:254)
at supersenior.SuperSenior.main(SuperSenior.java:100)
任何帮助都将不胜感激。谢谢

LinkedList类:

package supersenior;
import supersenior.exceptions.*;
import java.util.*;

public class LinkedList<T> implements OrderedListADT<T>, Iterable<T>
{
   protected int count;
   protected LinearNode<T> head, tail;

  /**
  * Creates an empty list.
  */
public LinkedList()
{
  count = 0;
  head = tail = null;
}


public T removeFirst() throws EmptyCollectionException
{
  if (isEmpty())
     throw new EmptyCollectionException ("List");

  LinearNode<T> result = head; 
  head = head.getNext();
  if (head == null)
     tail = null;
  count--;

  return result.getElement();
}


public T removeLast() throws EmptyCollectionException
{
  if (isEmpty())
     throw new EmptyCollectionException ("List");

  LinearNode<T> previous = null;
  LinearNode<T> current = head;

  while (current.getNext() != null)
  {
     previous = current; 
     current = current.getNext();
  }

  LinearNode<T> result = tail; 
  tail = previous;
  if (tail == null)
     head = null;
  else
     tail.setNext(null);
  count--;

  return result.getElement();
}


public T remove (T targetElement) throws EmptyCollectionException, 
     ElementNotFoundException 
{
  if (isEmpty())
     throw new EmptyCollectionException ("List");

  boolean found = false;
  LinearNode<T> previous = null;
  LinearNode<T> current = head;

  while (current != null && !found)
     if (targetElement.equals (current.getElement()))
        found = true;
     else
     {
        previous = current;
        current = current.getNext();
     }

  if (!found)
     throw new ElementNotFoundException ("List");

  if (size() == 1)
     head = tail = null;
  else if (current.equals (head))
     head = current.getNext();
  else if (current.equals (tail))
  {
     tail = previous;
     tail.setNext(null);
  }
  else
     previous.setNext(current.getNext());

  count--;

  return current.getElement();
}


public boolean contains (T targetElement) throws 
     EmptyCollectionException 
{
  if (isEmpty())
     throw new EmptyCollectionException ("List");

  boolean found = false;
  Object result;

  LinearNode<T> current = head;

  while (current != null && !found) 
     if (targetElement.equals (current.getElement()))
        found = true;
     else
        current = current.getNext();

  return found;
}


public boolean isEmpty()
{
  return (count == 0);
}


public int size()
{
  return count;
}


public String toString()
{
  LinearNode<T> current = head;
  String result = "";

  while (current != null)
  {
     result = result + (current.getElement()).toString() + "\n";
     current = current.getNext();
  }

  return result;
}


public Iterator<T> iterator()
{
  return new LinkedIterator<T>(head, count);
}


public T first()
{
  return head.getElement();
}


public T last()
{
  return tail.getElement();
}

@Override
public void add (T element)
{
  LinearNode<T>node = new LinearNode<T>();
  node.setElement(element);

  if(isEmpty())
  {
      head = node;
      if(tail == null)
          tail = head;
      //node.setNext(head);
      //head.setPrevious(node);
      //head.setElement((T) node);
      count++;
  }
  else
  {
      for(LinearNode<T> current = head; current.getNext() != null; current = current.getNext())
          if(node.compareTo((T) current) >= 0)
          {
              current.setPrevious(current);
              current.setNext(current);
          }
          else
          {
              current.setPrevious(node);
          }
      tail.setNext(node);
  }
}
}
packagesupersenior;
导入supersenior.exceptions.*;
导入java.util.*;
公共类LinkedList实现OrderedListADT,Iterable
{
保护整数计数;
保护线状节点头、尾;
/**
*创建一个空列表。
*/
公共链接列表()
{
计数=0;
头=尾=空;
}
public T removeFirst()引发EmptyCollectionException异常
{
if(isEmpty())
抛出新的EmptyCollectionException(“列表”);
线性节点结果=水头;
head=head.getNext();
if(head==null)
tail=null;
计数--;
返回result.getElement();
}
public T removeLast()引发EmptyCollectionException异常
{
if(isEmpty())
抛出新的EmptyCollectionException(“列表”);
LinearNode previous=null;
线性节点电流=磁头;
while(current.getNext()!=null)
{
先前=当前;
current=current.getNext();
}
LinearNode结果=尾部;
尾=前一个;
if(tail==null)
head=null;
其他的
tail.setNext(null);
计数--;
返回result.getElement();
}
public T remove(T targetElement)抛出EmptyCollectionException,
ElementNotFoundException
{
if(isEmpty())
抛出新的EmptyCollectionException(“列表”);
布尔值=false;
LinearNode previous=null;
线性节点电流=磁头;
while(当前!=null&&!找到)
if(targetElement.equals(current.getElement()))
发现=真;
其他的
{
先前=当前;
current=current.getNext();
}
如果(!找到)
抛出新元素NotFoundException(“列表”);
如果(size()==1)
头=尾=空;
否则如果(当前值等于(头))
head=current.getNext();
else if(current.equals(tail))
{
尾=前一个;
tail.setNext(null);
}
其他的
previous.setNext(current.getNext());
计数--;
返回current.getElement();
}
公共布尔包含(T targetElement)抛出
EmptyCollectionException
{
if(isEmpty())
抛出新的EmptyCollectionException(“列表”);
布尔值=false;
客观结果;
线性节点电流=磁头;
while(当前!=null&&!找到)
if(targetElement.equals(current.getElement()))
发现=真;
其他的
current=current.getNext();
发现退货;
}
公共布尔值为空()
{
返回(计数=0);
}
公共整数大小()
{
返回计数;
}
公共字符串toString()
{
线性节点电流=磁头;
字符串结果=”;
while(当前!=null)
{
结果=结果+(current.getElement()).toString()+“\n”;
current=current.getNext();
}
返回结果;
}
公共迭代器迭代器()
{
返回新的链接器(头、计数);
}
公共交通优先
{
返回head.getElement();
}
公共交通最后一站
{
返回tail.getElement();
}
@凌驾
公共无效添加(T元素)
{
LinearNode=新的LinearNode();
node.setElement(元素);
if(isEmpty())
{
头部=节点;
if(tail==null)
尾=头;
//node.setNext(head);
//head.setPrevious(节点);
//head.setElement((T)节点);
计数++;
}
其他的
{
对于(LinearNode current=head;current.getNext()!=null;current=current.getNext())
如果(节点比较((T)电流)>=0)
{
当前。设置上一个(当前);
当前。设置下一个(当前);
}
其他的
{
当前.setPrevious(节点);
}
tail.setNext(节点);
}
}
}
LinearNode类:

package supersenior;   

public class LinearNode<E> implements Comparable<E>
{
private LinearNode<E> next, previous;
public E element;


public LinearNode()
{
    next = null;
    element = null;
}


public LinearNode (E elem)
{
    next = null;
    element = elem;
}


public LinearNode<E> getNext()
{
    return next;
}


public void setNext (LinearNode<E> node)
{
    next = node;
}


public E getElement()
{
    return element;
}


public void setElement (E elem)
{
    element = elem;
}



@Override
 public int compareTo(E otherElement) {
    return ((Comparable<E>) this.element).compareTo(otherElement);
}

  public LinearNode<E> getPrevious()
{
    return previous;
}


public void setPrevious (LinearNode<E> node)
{
    previous = node;
}

}
packagesupersenior;
公共类LinearNode实现了可比较的
{
私有LinearNode下一个,上一个;
公共电子元素;
公共线性节点()
{
next=null;
元素=空;
}
公共线性节点(E元素)
{
next=null;
元素=元素;
}
public LinearNode getNext()
{
下一步返回;
}
public void setNext(LinearNode节点)
{
下一个=节点;
}
公共E getElement()
{
返回元素;
}
公共无效集合元素(E元素)
{
元素=元素;
}
@凌驾
公共整数比较(E其他元素){
返回((可比较的)this.element).compareTo(其他元素);
}
public LinearNode getPrevious()
{
返回上一个;
}
public void setPrevious(LinearNode节点)
{
上一个=节点;
}
}
元素类(高尔夫球手):

packagesupersenior;
公营高尔夫球手{
高尔夫球手;
字符串名;
国际巡回赛;
国际奖金;
双平均值;
公共高尔夫球手(字符串属性[]){
this.name=attr[0];
this.tourneys=Integer.parseInt(attr[1]);
this.winnings=Integer.parseInt(attr[2]);
this.avg=findAvg(赢款、锦标赛);
}
私人双人findAvg(国际奖金、国际巡回赛){
双a=奖金/巡回赛;
返回a;
}
@凌驾
公共字符串toString(){
返回“名称:”+Name+“锦标赛:”+Tourneys+“奖金:”+Winnings+“平均值:”+avg;
}
@凌驾
公众国际比较(高尔夫球手){

如果(this.avg,问题是您混合了所比较的内容。您试图比较
LinearNode
对象(其中包含
E
)和实际的
E
LinearNode
不应实现
可比较的
;如果有,它可能实现
compariable
,以及类型参数s
package supersenior;


public class Golfer implements Comparable<Golfer>{
Golfer imaGolfer;
String name;
int tourneys;
int winnings;
double avg;

public Golfer(String attr[]){
    this.name = attr[0];
    this.tourneys = Integer.parseInt(attr[1]);
    this.winnings = Integer.parseInt(attr[2]);
    this.avg = findAvg(winnings, tourneys);

 }

 private double findAvg(int winnings, int tourneys){
   double a = winnings/tourneys;
   return a;
 }

@Override
 public String toString(){
   return "Name: " + name + " Tourneys: " + tourneys + " Winnings: " + winnings + " Average: " + avg;
}

@Override
public int compareTo(Golfer golfer) {
if(this.avg <= golfer.avg)
    return 1;
if(this.avg == golfer.avg)
    return 0;
else
    return -1;
}
}
// in LinearNode
public int compareTo(LinearNode<E> otherNode) {
    return this.element.compareTo(otherNode.element);
}