Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 来自不同类节点的链表_Java_Data Structures_Linked List - Fatal编程技术网

Java 来自不同类节点的链表

Java 来自不同类节点的链表,java,data-structures,linked-list,Java,Data Structures,Linked List,我试图为一个任务创建一个链表,类似于一个杂货市场的篮子,篮子是链表,其中的节点(链接)是每个产品。下面的代码表示篮子(LinkedList)和一些产品Gouda和Bacon。我的问题是如何将这两个节点制作一个链接列表,以便获得像Gouda->Bacon->Bacon->Bacon->Gouda等这样的链接列表 p、 It’先谢谢你 public class Test { public static void main(String[] args) { LinkedList the

我试图为一个任务创建一个链表,类似于一个杂货市场的篮子,篮子是链表,其中的节点(链接)是每个产品。下面的代码表示篮子(LinkedList)和一些产品Gouda和Bacon。我的问题是如何将这两个节点制作一个链接列表,以便获得像Gouda->Bacon->Bacon->Bacon->Gouda等这样的链接列表

p、 It’先谢谢你

public class Test {
    public static void main(String[] args) {
    LinkedList theLinkedList = new LinkedList();
    theLinkedList.insertFirstLink();
    theLinkedList.display();
   }
}

class LinkedList{

// Reference to first Link in list
// The last Link added to the LinkedList

public Gouda firstLink; 

LinkedList(){

    // Here to show the first Link always starts as null

    firstLink = null;

}

// Returns true if LinkList is empty

public boolean isEmpty(){

    return(firstLink == null);

}

public void insertFirstLink(){

    Gouda newLink = new Gouda();

    // Connects the firstLink field to the new Link 

    newLink.next = firstLink;

    firstLink = newLink;

}


public void display(){

    Gouda theLink = firstLink;

    // Start at the reference stored in firstLink and
    // keep getting the references stored in next for
    // every Link until next returns null

    while(theLink != null){

        theLink.display();

        System.out.println("Next Link: " + theLink.next);

        theLink = theLink.next;

        System.out.println();

    }

    }
}

public class Gouda{

// Set to public so getters & setters aren't needed

    public String category= "Dairy";
    public String productName = "Gouda Cheese";
    public double price = 57.50;

    // Reference to next link made in the LinkList
    // Holds the reference to the Link that was created before it
    // Set to null until it is connected to other links

    public Gouda next; 

    public void display(){

        System.out.println(category+" :"+ productName +""+ price);

    }   
}

public class Bacon{

// Set to public so getters & setters aren't needed

    public String category= "Meat";
    public String productName = "Thick cut Bacon";
    public double price = 5.50;

    // Reference to next link made in the LinkList
    // Holds the reference to the Link that was created before it
    // Set to null until it is connected to other links

    public Bacon next; 

    public void display(){

        System.out.println(category+" :"+ productName +""+ price);

    }   
}

您应该定义一个新类,例如
Product
,其中
Gouda
Bacon
被定义为它的子类

然后在链接列表中,将firstLink定义为:

public Product firstLink; 
并始终在类
LinkedList
中使用Product。通过这种方式,您可以在列表中插入
Goude
Bacon
的实例,因为它们是
Product


这是子类和继承的基本思想。

在java中,通过使列表的每个链接都是一个类似容器的类、节点来解决,该类、节点有一个值字段,例如Object(Gouda和Bacon是对象),等等。您自己的LinkedList实现了吗?为什么您不喜欢默认Java的LinkedList?关于您的问题——考虑将Gouda和BACON分成基本产品类的子类(BACON类扩展产品)。之后,只需使用List products=new LinkedList();但是,如果由于不同的原因需要自己的实现,您仍然可以创建一个名为Product的接口。你们两个类都应该实现这个接口,所以最终你们将能够编写
Product gouda=new gouda()确定,因此我创建了一个名为Product的新类,其中包含一个空的display()和一个public Product next,用于分配下一个链接,但当我声明一个新链接Gouda newLink=new Gouda()时,我在newLink.next=firstLink中得到一个错误,表示它无法将其转换为Product to Gouda。既然Gouda是Product的一个子类,为什么会发生这种情况?您应该执行Product newLink=new Gouda。感谢您的快速回复,并通过更改解决了此问题。。。我知道问题已经解决了,但你能解释为什么Gouda newLink=new Gouda()不起作用,而Product newLink=new Gouda()起作用吗?这是因为你用Product的类型子类型重新定义了newLink,而Java的类型系统不允许这种操作。谷歌关于“协方差与反方差”的讨论,看看原因。