简体   繁体   English

如何从Java的链表中的节点获取对象?

[英]How to get an object from a node in a linked list in Java?

Basically, I want to do something like this: 基本上,我想做这样的事情:

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

But my IDE claims the types are inconvertible. 但是我的IDE声称类型是不可转换的。 ( required: Patient; found: TreatmentRoomQueue<Patient> ). required: Patient; found: TreatmentRoomQueue<Patient> )。 You can see that I'm attempting to cast the Node as a Patient object and that I am attempting to call my peekFront method that should return the first node in the list. 您可以看到我正在尝试将Node强制转换为Patient对象,并且试图调用应该返回列表中第一个节点的peekFront方法。 It is however apparent that this is illegal. 但是很明显,这是非法的。 My syntax could be wrong, or maybe I'm just approaching this the wrong way. 我的语法可能是错误的,或者也许我只是以错误的方式来处理。

My motivation behind this is that I have a program for running a (fictional) Emergency Room. 我这样做的动机是,我有一个用于运行(虚拟)急诊室的程序。 I need to get someone from my TreatmentRoomQueue, discharge them, and then, if someone is in my WaitingRoomQueue, move them to the Treatment Room. 我需要从我的TreatmentRoomQueue中找一个人,把他们解雇,然后,如果有人在我的WaitingRoomQueue中,请将他们移到治疗室。 I am not using the Java built in LinkedList class, I am using my own linked list (which I have used previously and know it works). 我没有使用LinkedList类中内置的Java,而是使用了自己的链表(我以前使用过并且知道它可以工作)。

I could just say screw it and used an array instead of a linked list, but since linked lists are a bit harder for me to understand, I think there's more to be learned from a linked list implementation. 我只能说拧一下它,而是使用数组而不是链表,但是由于链表让我很难理解,因此我认为从链表实现中可以学到更多。

Any pointers, code snippets, advice, or whatever would be greatly appreciated! 任何指针,代码段,建议或任何其他内容,将不胜感激! Thanks for reading. 谢谢阅读。

I declare TreatmentRoomQueue in my Main class this way: 我以这种方式在Main类中声明TreatmentRoomQueue:

TreatmentRoomQueue<Patient> TreatmentRoomQueue = new TreatmentRoomQueue();

Here's the code for 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;
    }     
}

First, you are casting the list, not the element that you get from it. 首先,您要投射列表,而不是从列表中获取元素。 All you need to do is moving the parentheses to the right place: 您需要做的就是将括号移到正确的位置:

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

The way you placed the parentheses, Java tried casting TreatmentRoomQueue to Patient first, and after that attempted to call the peekFront() method on the Patient object. Java放置括号的方式是,Java首先尝试将TreatmentRoomQueue强制转换为Patient ,然后尝试在Patient对象上调用peekFront()方法。

Next, you need to make a public method for looking at the front element of your queue: replace your 接下来,您需要使用一个公共方法来查看队列的前部元素:

private Node peekFront()

method with 方法

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

Since classTypeObjectRef is private , you should make it public to make it available to peekFront . 由于classTypeObjectRefprivate ,因此应将其public ,以使其可用于peekFront

Any pointers, code snippets, advice, or whatever would be greatly appreciated! 任何指针,代码段,建议或任何其他内容,将不胜感激!

Since you asked, here is my advice / whatever: 既然你问了,这就是我的建议/无论如何:

A variable called TreatmentRoomQueue is a CODING STANDARD VIOLATION in all sensible Java coding standards. 在所有明智的Java编码标准中,一个名为TreatmentRoomQueue的变量是一个CODING STANDARD VIOLATION。 You have used the class identifier pattern for a variable. 您已将类标识符模式用于变量。 A variable identifier should have the form treatmentRoomQueue (unless it is a static final constant ... in which case it should be TREATMENT_ROOM_QUEUE ). 变量标识符的形式应为treatmentRoomQueue (除非它是static final常量...在这种情况下,应为TREATMENT_ROOM_QUEUE )。

Ordinarily this is a small thing, but in this case you have compounded the problem: 通常这是一件小事,但是在这种情况下,您使问题更加复杂了:

 TreatmentRoomQueue<Patient> TreatmentRoomQueue = new TreatmentRoomQueue();

Now you have a variable name and a type name that are identical ... but name different things!!! 现在您有了一个相同的变量名和一个类型名...但是命名不同!!! The JLS specifies how an identifier should be interpreted (ie whether it should be read by the compiler as a variable name or as a class name) ... but normal human Java programmers typically don't understand these rules, and are liable to read them the wrong way. JLS指定标识符的解释方式(即,编译器应将其作为变量名还是作为类名读取)...但是普通的Java程序员通常不理解这些规则,并且易于读取他们以错误的方式。

Indeed, this might have been the cause of some of the confusing compilation errors that you saw! 确实,这可能是您看到的一些令人困惑的编译错误的原因!

(Please don't think that I'm saying that ordinary programmers should understand the JLS's identifier disambiguation rules. What I'm saying is that you should follow accepted coding standards so that normal human programmers are able to read your code without falling into semantic mantraps!) (请不要以为我是说普通程序员应该了解JLS的标识符歧义消除规则。我的意思是,您应该遵循公认的编码标准,以便普通的人类程序员能够阅读您的代码而不会陷入语义上。 mantraps!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM