2

基本上,我想做这样的事情:

patientReference = ((Patient) TreatmentRoomQueue).peekFront();

但我的 IDE 声称这些类型是不可转换的。( required: Patient; found: TreatmentRoomQueue<Patient>)。您可以看到我正在尝试将 Node 转换为 Patient 对象,并且我正在尝试调用我的 peekFront 方法,该方法应该返回列表中的第一个节点。然而很明显这是非法的。我的语法可能是错误的,或者我只是以错误的方式处理这个问题。

我这样做的动机是我有一个运行(虚构的)急诊室的程序。我需要从我的 TreatmentRoomQueue 中找人,让他们出院,然后,如果有人在我的 WaitingRoomQueue 中,将他们转移到 Treatment Room。我没有使用内置在 LinkedList 类中的 Java,我使用的是我自己的链表(我以前使用过并且知道它可以工作)。

我只能说搞砸了,使用数组而不是链表,但由于链表对我来说有点难以理解,我认为从链表实现中可以学到更多东西。

任何指针、代码片段、建议或任何将不胜感激的东西!谢谢阅读。

我以这种方式在我的 Main 类中声明 TreatmentRoomQueue:

TreatmentRoomQueue<Patient> TreatmentRoomQueue = new TreatmentRoomQueue();

这是 TreatmentRoomQueue 的代码:

public class TreatmentRoomQueue <ClassType> 
{
    private Node frontQueueNodeRef = null;
    private Node backQueueNodeRef = null;
    private int counter = 0;

    private class Node 
    {
      private ClassType classTypeObjectRef;
      private Node nextNodeRef;
      Node(ClassType newClassTypeObjectRef)
      {
          classTypeObjectRef = newClassTypeObjectRef;
      }
    }

    private Node peekFront()
    {
        return frontQueueNodeRef;
    }

    public void enqueue(ClassType enqueueObjectRef) 
    {            
     Node queueNodeRef = new Node(enqueueObjectRef);
     if (frontQueueNodeRef == null)
     {
         frontQueueNodeRef = backQueueNodeRef = queueNodeRef;
     }
     else {
         backQueueNodeRef.nextNodeRef = queueNodeRef;
         backQueueNodeRef = queueNodeRef;
         counter++;
     }

    }

    public ClassType dequeue() 
    {
    if ( frontQueueNodeRef == null )
    {
        return null;
    } else {
    ClassType firstClassTypeObjectRef = frontQueueNodeRef.classTypeObjectRef;
    frontQueueNodeRef = frontQueueNodeRef.nextNodeRef;
    counter--;
    return firstClassTypeObjectRef;
    }
    }

    public boolean isFull()
    {
        return false;
    }

    public boolean isEmpty()
    {
        return frontQueueNodeRef == null;
    }     
}
4

2 回答 2

4

首先,您正在投射列表,而不是从中获得的元素。您需要做的就是将括号移到正确的位置:

patientReference = (Patient) (TreatmentRoomQueue.peekFront());

您放置括号的方式是,Java 首先尝试强制TreatmentRoomQueue转换Patient,然后尝试调用对象peekFront()上的方法Patient

接下来,您需要创建一个公共方法来查看队列的前面元素:替换您的

private Node peekFront()

方法与

public ClassType peekFront()
{
    return frontQueueNodeRef.classTypeObjectRef;
}

既然classTypeObjectRefprivate,你就应该让它publicpeekFront.

于 2012-10-14T01:07:20.017 回答
1

任何指针、代码片段、建议或任何将不胜感激的东西!

既然你问了,这是我的建议/无论如何:

在所有合理的 Java 编码标准中,一个名为的变量TreatmentRoomQueue是 CODING STANDARD VIOLATION。您已经为变量使用了类标识符模式。变量标识符应该有这种形式treatmentRoomQueue(除非它是一个static final常数......在这种情况下它应该是TREATMENT_ROOM_QUEUE)。

通常这是一件小事,但在这种情况下,您使问题更加复杂:

 TreatmentRoomQueue<Patient> TreatmentRoomQueue = new TreatmentRoomQueue();

现在你有一个相同的变量名和一个类型名......但是命名不同的东西!!!JLS 指定应如何解释标识符(即编译器应将其读取为变量名还是类名)......但普通的人类 Java 程序员通常不理解这些规则,并且容易阅读他们走错路了。

实际上,这可能是您看到的一些令人困惑的编译错误的原因!

(请不要以为我是说普通程序员应该理解 JLS 的标识符消歧规则。我的意思是您应该遵循公认的编码标准,以便普通人类程序员能够阅读您的代码而不会陷入语义咒语!)

于 2012-10-14T01:44:05.133 回答