gpt4 book ai didi

Java LinkedList 数组引用

转载 作者:行者123 更新时间:2023-12-01 13:10:34 24 4
gpt4 key购买 nike

所以我有一个简单的程序,它创建一个给定大小 n 的 LinkedList 数组,列表中的每个值代表一个新的单独的 LinkedList 数据结构。

public class Graph {

public final LinkedList[] graph;

public Graph(int n){
graph = new LinkedList[n];

for (int i=0; i<n; i++){
graph[i] = new LinkedList();
}

}

public void addEdge(int x, int y){

graph[x].addFirst(y);
graph[y].addFirst(x);

}

但是,由于某种原因,当我使用两个 int 值调用 addEdge() 方法时,而不是将它们添加到 graph 中名为 LinkedList 的特定位置[],它将它们添加到 graph[] 中的每个 LinkedList 中。

这里有什么问题吗?

编辑:*

public void addEdge(int x, int y){

graph[x].addFirst(y);
graph[y].addFirst(x);

for (int i=0; i<graph.length; i++){
Node tmp = graph[i].first;

System.out.println(i + ":");

while (tmp != null){
System.out.print(tmp.name + " ");

tmp = tmp.Rnext;
}
System.out.println();
}
System.out.println();

}


public class Test {

public static void main(String[] args) {


Graph myGraph1 = new Graph(8);
myGraph1.addEdge(1, 2);
myGraph1.addEdge(1, 7);
myGraph1.addEdge(1, 4);
myGraph1.addEdge(2, 5);
myGraph1.addEdge(2, 6);
myGraph1.addEdge(6, 3);
myGraph1.addEdge(3, 8);
myGraph1.addEdge(5, 7);
}
}

Here is the output of graph:
0:
1 2
1:
1 2
2:
1 2
3:
1 2
4:
1 2
5:
1 2
6:
1 2
7:
1 2

0:
1 7 1 2
1:
1 7 1 2
2:
1 7 1 2
3:
1 7 1 2
4:
1 7 1 2
5:
1 7 1 2
6:
1 7 1 2
7:
1 7 1 2

0:
1 4 1 7 1 2
1:
1 4 1 7 1 2
2:
1 4 1 7 1 2
3:
1 4 1 7 1 2
4:
1 4 1 7 1 2
5:
1 4 1 7 1 2
6:
1 4 1 7 1 2
7:
1 4 1 7 1 2

0:
2 5 1 4 1 7 1 2
1:
2 5 1 4 1 7 1 2
2:
2 5 1 4 1 7 1 2
3:
2 5 1 4 1 7 1 2
4:
2 5 1 4 1 7 1 2
5:
2 5 1 4 1 7 1 2
6:
2 5 1 4 1 7 1 2
7:
2 5 1 4 1 7 1 2

0:
2 6 2 5 1 4 1 7 1 2
1:
2 6 2 5 1 4 1 7 1 2
2:
2 6 2 5 1 4 1 7 1 2
3:
2 6 2 5 1 4 1 7 1 2
4:
2 6 2 5 1 4 1 7 1 2
5:
2 6 2 5 1 4 1 7 1 2
6:
2 6 2 5 1 4 1 7 1 2
7:
2 6 2 5 1 4 1 7 1 2

0:
6 3 2 6 2 5 1 4 1 7 1 2
1:
6 3 2 6 2 5 1 4 1 7 1 2
2:
6 3 2 6 2 5 1 4 1 7 1 2
3:
6 3 2 6 2 5 1 4 1 7 1 2
4:
6 3 2 6 2 5 1 4 1 7 1 2
5:
6 3 2 6 2 5 1 4 1 7 1 2
6:
6 3 2 6 2 5 1 4 1 7 1 2
7:
6 3 2 6 2 5 1 4 1 7 1 2

这是我正在使用的 LinkedList 和 Node 类:

import java.util.NoSuchElementException;

public class LinkedList {

public static Node first;

public LinkedList(){
first = null;

}

// Returns true if the list is empty
public boolean isEmpty(){
return first == null;
}

// Inserts a new node at the beginning of this list.
public void addFirst(int name){

first = new Node(name, first);
}


public boolean findData(int d){
if(first == null) throw new NoSuchElementException();
Node tmp = first;

while (tmp != null) {

if (tmp.name == d) return true;
tmp = tmp.Rnext;
} return false;
}
}


public class Node {

public int name;
public Node Rnext;

public Node(){
name = 0;
Rnext = null;
}

public Node(int n, Node r){
this.name = n;
this.Rnext = r;
}

}

最佳答案

public static Node first;

这就是问题所在。您创建的每个 LinkedList 都共享相同的 Node,因此它们实际上都是相同的列表。

不要使用static作为实例变量。

关于Java LinkedList 数组引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22900917/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com