在 Java 中创建节点类 [英] Creating a node class in Java

查看:31
本文介绍了在 Java 中创建节点类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我对 Java 和编程还很陌生,我想知道如何创建节点类?

So I'm fairly new to Java and programming and I was wondering how to create a node class?

到目前为止我有:

public class ItemInfoNode{ 
    private ItemInfoNode next;
    private ItemInfoNode prev;
    private ItemInfo info;
    public ItemInfoNode(ItemInfo info, ItemInfoNode next, ItemInfoNode prev){
        info = info;
        next = next;
        prev = prev;
    }
    public void setInfo(ItemInfo info){
        info = info;

    }
    public void setNext(ItemInfoNode node){
        next = node;
    }
    public void setPrev(ItemInfoNode node){
        prev = node;
    }
    public ItemInfo getInfo(){
        return info;
    }
    public ItemInfoNode getNext(){
        return next;
    }
    public ItemInfoNode getPrev(){
        return prev;
    }

}

几乎是问这些方法的问题,所以我把它们记下来,但是,下一个问题要求我参考 ItemInfoNode 节点的头部和尾部.只是在这里有点困惑.谢谢

Pretty much the question asked for those methods so I put those down but, the next question asks me to refer to the head and tail of ItemInfoNode nodes. Just a bit confused here. Thanks

感谢大家的帮助!我正在尝试创建一个InsertInfo"方法,将名称、价格、标签编号等信息放入一个节点.我如何着手创建这种方法?

Thanks for the help guys! I'm trying to create an "InsertInfo" method that puts information like the name, price, tag number, etc. Into one node. How do I go about creating this method?

到目前为止我得到了这个..我在一个不同的类中有一个 Iteminfo 构造函数,它包含所有这些但是,我不知道如何使用它/如果我什至应该这样做..

So far I got this.. I have an Iteminfo constructor in a different class that has all of these but, I'm not sure how to use that/if I am even supposed to do..

public void InsertInfo(String name, String rfdnumber, double price, String original_position){

        head = new ItemInfoNode (Iteminfo, head);
    }

推荐答案

欢迎使用 Java!这个节点就像一个块,它们必须组装起来才能做出惊人的事情!在这种特殊情况下,您的节点可以表示一个列表、一个链表,您可以在此处查看示例:

Welcome to Java! This Nodes are like a blocks, they must be assembled to do amazing things! In this particular case, your nodes can represent a list, a linked list, You can see an example here:

public class ItemLinkedList {
    private ItemInfoNode head;
    private ItemInfoNode tail;
    private int size = 0;

    public int getSize() {
        return size;
    }

    public void addBack(ItemInfo info) {
        size++;
        if (head == null) {
            head = new ItemInfoNode(info, null, null);
            tail = head;
        } else {
            ItemInfoNode node = new ItemInfoNode(info, null, tail);
            this.tail.next =node;
            this.tail = node;
        }
    }

    public void addFront(ItemInfo info) {
        size++;
        if (head == null) {
            head = new ItemInfoNode(info, null, null);
            tail = head;
        } else {
            ItemInfoNode node = new ItemInfoNode(info, head, null);
            this.head.prev = node;
            this.head = node;
        }
    }

    public ItemInfo removeBack() {
        ItemInfo result = null;
        if (head != null) {
            size--;
            result = tail.info;
            if (tail.prev != null) {
                tail.prev.next = null;
                tail = tail.prev;
            } else {
                head = null;
                tail = null;
            }
        }
        return result;
    }

    public ItemInfo removeFront() {
        ItemInfo result = null;
        if (head != null) {
            size--;
            result = head.info;
            if (head.next != null) {
                head.next.prev = null;
                head = head.next;
            } else {
                head = null;
                tail = null;
            }
        }
        return result;
    }

    public class ItemInfoNode {

        private ItemInfoNode next;
        private ItemInfoNode prev;
        private ItemInfo info;

        public ItemInfoNode(ItemInfo info, ItemInfoNode next, ItemInfoNode prev) {
            this.info = info;
            this.next = next;
            this.prev = prev;
        }

        public void setInfo(ItemInfo info) {
            this.info = info;
        }

        public void setNext(ItemInfoNode node) {
            next = node;
        }

        public void setPrev(ItemInfoNode node) {
            prev = node;
        }

        public ItemInfo getInfo() {
            return info;
        }

        public ItemInfoNode getNext() {
            return next;
        }

        public ItemInfoNode getPrev() {
            return prev;
        }
    }
}

将 ItemInfo 声明为:

Declare ItemInfo as this:

public class ItemInfo {
    private String name;
    private String rfdNumber;
    private double price;
    private String originalPosition;

    public ItemInfo(){
    }

    public ItemInfo(String name, String rfdNumber, double price, String originalPosition) {
        this.name = name;
        this.rfdNumber = rfdNumber;
        this.price = price;
        this.originalPosition = originalPosition;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRfdNumber() {
        return rfdNumber;
    }

    public void setRfdNumber(String rfdNumber) {
        this.rfdNumber = rfdNumber;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getOriginalPosition() {
        return originalPosition;
    }

    public void setOriginalPosition(String originalPosition) {
        this.originalPosition = originalPosition;
    }
}

然后,您可以像这样在链表中使用您的节点:

Then, You can use your nodes inside the linked list like this:

public static void main(String[] args) {
    ItemLinkedList list = new ItemLinkedList();
    for (int i = 1; i <= 10; i++) {
        list.addBack(new ItemInfo("name-"+i, "rfd"+i, i, String.valueOf(i)));

    }
    while (list.size() > 0){
        System.out.println(list.removeFront().getName());
    }
}

这篇关于在 Java 中创建节点类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆