简体   繁体   English

如何从头开始创建对象的 LinkedList(不使用 java 的 API)

[英]How to create a LinkedList of objects from scratch(not using java's API)

I have to create a LinkedList of objects from scratch and I simply can't get my head around it, I'm not sure if i need a class for the linkedlist and the node or if i can do it in the object class itself.我必须从头开始创建对象的 LinkedList,我根本无法理解它,我不确定是否需要 class 用于链接列表和节点,或者我是否可以在 object ZA2F2ED4F8EBC2CBB4C21A2 本身中完成它。 I'm also doing this on JavaFx using scene builder so i need to add objects through user input from the controller class.我也在使用场景构建器在 JavaFx 上执行此操作,因此我需要通过来自 controller class 的用户输入添加对象。

I've tried separating the node and linkedlist into there own classes but that just confused me further.我尝试将节点和链表分离到自己的类中,但这让我更加困惑。

This is my current Show object fields and constructor这是我当前的 Show object 字段和构造函数

public class Show {

        private String title;
        private int runningTime;
        private String startDate, endDate;
        private int ticketPrice;
        public Node head, next;



        public Show (String title, int runningTime, String startDate, String 
                     endDate, int ticketPrice) {
            this.title = title;
            this.runningTime = runningTime;
            this.startDate = startDate;
            this.endDate = endDate;
            this.ticketPrice = ticketPrice;
            next=null;
        }

When i try to call the head which i've made public in my Show class it gives an error "Non-static field 'head' cannot be referenced from a static context"当我尝试调用我在 Show class 中公开的头部时,它会给出错误“无法从 static 上下文中引用非静态字段‘头部’”

I'm looking for the correct structure for object specific linkedlists.我正在寻找 object 特定链表的正确结构。

Adding the following method to your 'Show' class will work.将以下方法添加到您的“显示”class 将起作用。 You couldn't reference 'head' directly here because main is not executed in the context of a Show object and hence cannot access any class member variables.您不能在此处直接引用“head”,因为 main 不在 Show object 的上下文中执行,因此无法访问任何 class 成员变量。 However, it can access these member variables qualified by an instance of the Show class.但是,它可以访问这些由 Show class 实例限定的成员变量。 Please note, that I'm not advocating that you expose your implementation details like this, but that is another discussion.请注意,我并不主张您像这样公开您的实现细节,但这是另一个讨论。

public static void main(String args) {
  Show show = new Show(....);
  show.head = new Node(...);
}

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

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