简体   繁体   English

Java中的链接列表数组

[英]Array of Linked Lists in Java

I have to create an array of linked lists for a class in order to store a graph (adjacency list). 我必须为一个类创建一个链接列表数组,以便存储图形(邻接列表)。 We have to use Java. 我们必须使用Java。 I can create the array and instantiate each linked list, but when I go to add the first elements to each one, every linked list gets changed, not just the one at the index of the array. 我可以创建数组并实例化每个链表,但是当我向每个链表添加第一个元素时, 每个链表都会改变,而不仅仅是数组索引处的链表。

Node [] adjList;
for(i=0;i<adjList.length;i++)
    adjList[i] = new Node(0,0,null);

this instantiates each new linked list [ Node is my own class, with constructor Node(int head, int data, Node next ) and extends LinkedList ] 这将实例化每个新的链接列表[ Node是我自己的类,带有构造函数Node(int head, int data, Node next )并扩展LinkedList ]

then i go to add the first values to each node: 然后我去添加第一个值到每个节点:

for(i=0;i<adjList.length;i++)
    adjList[i].setHead(i+1); //  numbers 1 to end are the graph vertices

or 要么

for(i=0;i<adjList.length;i++)
    adjList[i].add(new Node(i+1,0,null);

I use print statements to debug the code at the end of these loop I print off each Linked List, but for each one, the values come out to be the final one 在这些循环的最后,我使用print语句调试代码。我打印出每个链表,但对于每个链表,值都是最后一个

ie. 即。 if adjList.length = 2 , it would print out 如果adjList.length = 2 ,它将打印出

[3,0,null] // adjList[0]
[3,0,null] // adjList[1]
[3,0,null] // adjList[2]

edit: here is the Node class 编辑:这是Node类

import java.util.LinkedList;
public class Node extends LinkedList{
    private static int head;
    private static int data;
    private static Node next;

    public Node(int h,int d,Node n) {
        head = h;
        data = d;
        next = n;
    }
    public int getHead(){ // getNext() and getData() are the same
        return head;
    }
    public void setHead(int h){ // setNext() and setData() are basically the same
        head = h;
    }
}

when I go to add the first elements to each one, every linked list gets changed, not just the one at the index of the array 当我将第一个元素添加到每个元素时,每个链接列表都会更改,而不仅仅是数组索引处的元素

Although your code snippet doesn't show it, almost definitely you have an aliasing problem. 尽管您的代码段未显示该代码段,但几乎可以肯定您有一个别名问题。 The aliasing problem, which tends to bite beginners in almost all object-oriented languages, is the problem of referring to the same object with two different names ie two different variables pointing at the same object. 别名问题几乎在所有面向对象的语言中都容易引起初学者的注意,它是用两个不同的名称(即两个指向相同对象的变量)来引用同一对象的问题。

Now you may be wondering: what about array indices? 现在您可能想知道:数组索引呢? The problem is with changing a variable at one array index and getting a change across all array indices , not a bunch of named variables. 问题在于更改一个数组索引处的变量并获得所有数组索引处的变化 ,而不是一堆命名变量。 But, as Eric Lippert explains (for C#, which is quite similar to Java), an array really is a bunch of variables that you can refer to with an indexer expression rather than having to define a bunch of individual names. 但是,正如Eric Lippert解释的那样 (对于C#,它与Java非常相似),数组实际上是一堆变量,您可以使用索引器表达式来引用它,而不必定义一堆单独的名称。 In a sense, int[] foo = new int[3] is like declaring foo0 , foo1 , and foo2 , and indexing into foo just tells the compiler to pick the appropriate variable out of foo0 , foo1 , and foo2 . 从某种意义上说, int[] foo = new int[3]就像声明foo0foo1foo2 ,索引到foo只是告诉编译器从foo0foo1foo2选择适当的变量。

You may also be wondering how data could be shared between multiple Node instances, if your array indeed has multiple nodes in it. 您可能还想知道,如果阵列中确实包含多个节点,那么如何在多个Node实例之间共享数据。 There are a few ways, and knowing which is pretty much impossible without the code for the Node class. 有几种方法,要知道没有Node类的代码几乎是不可能的。 As @DNA points out, there could be static data in the Node class, which is automatically shared across all instances. 正如@DNA指出的那样, Node类中可能有静态数据,这些数据会在所有实例之间自动共享。 A Node object may also have a reference to underlying data. Node对象也可以引用基础数据。 If you pass the same reference into all the Node constructors, they are all aliasing the same object in this way as well. 如果将相同的引用传递给所有Node构造函数,则它们也都以这种方式为同一对象别名。

You have probably declared something within Node as static, so every instance ends up with the same shared value, rather than having its own value. 您可能已经在Node中将某些内容声明为静态的,因此每个实例最终都具有相同的共享值,而不是拥有自己的值。 However, this is just a guess - please post the code of Node so we can see what the problem really is... 但是,这只是一个猜测-请发布Node的代码,以便我们可以真正了解问题所在...

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

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