1

我查看了该领域的一堆问题,但找不到专门解决我问题的问题。

基本上,这是一个家庭作业,我有一个带有节点的链表,其中包含一个元素。节点类 (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)

任何帮助将不胜感激。谢谢!

链表类:

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);
  }
}
}

线性节点类:

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;
}

}

元素类(高尔夫球手):

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;
}
}
4

1 回答 1

1

问题是您正在混合正在比较的内容。您正在尝试将LinearNode对象(包含E)与实际的E. LinearNode<E>不应该实施Comparable<E>;如果有的话,它可能会实现Comparable<LinearNode<E>>,并且类型参数应该是E extends Comparable<E>.

如果你想LinearNode根据底层元素的顺序对 s 进行排序,你应该使用这样的东西:

// in LinearNode
public int compareTo(LinearNode<E> otherNode) {
    return this.element.compareTo(otherNode.element);
}

(请注意,Java 排序集合不需要实现元素Comparable,因为您可以为Comparator它们中的任何一个提供自定义,但在此分配的情况下,要求它可能很好E extends Comparable<E>。)

(注 2:如果您使用泛型,任何类型转换,例如您的(Comparable<E>),都是危险信号;泛型系统的目的是消除对大多数显式类型转换的需要。)

于 2013-10-10T18:27:38.093 回答