如何从 Java DisjointSet 创建父节点的实例
How to create an instance of a parent node from Java DisjointSet
这是我在 main 方法中尝试的。
(一个父节点指向它自己并且等级为0。)
public static void main(String[] args) {
DisjointSet x = new DisjointSet();
**Node<T> parent = new Node<T>(parent, 0);**
}
这是我的错误信息:
错误:无法从静态上下文中引用非静态类型变量 T
错误:无法从静态上下文中引用非静态类型变量 T
等号两边的 T 似乎各有一个错误。
错误是因为您正在从静态方法中访问非静态实例变量
接口的所有成员字段默认为public
、static
和final
。
由于内部接口默认为 static
,您不能从 static
字段或方法中引用 T
。
T 实际上与 class 的实例相关联,如果它与 static
字段或方法相关联,则该字段或方法与 class 那就没意义了
您可以创建一个 class,其中包含以下信息:
public class MyTreeNode<T>{
public T data = null;
public MyTreeNode parent = null;
public MyTreeNode(T data) {
this.data = data;
}
public void addChild(MyTreeNode child) {
child.setParent(this);
this.children.add(child);
}
public void addChild(T data) {
MyTreeNode<T> newChild = new MyTreeNode<>(data);
newChild.setParent(this);
children.add(newChild);
}
public void addChildren(List<MyTreeNode> children) {
for(MyTreeNode t : children) {
t.setParent(this);
}
this.children.addAll(children);
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
和主要示例:
MyTreeNode<String> root = new MyTreeNode<>("Root");
MyTreeNode<String> child1 = new MyTreeNode<>("Child1");
child1.addChild("Grandchild1");
child1.addChild("Grandchild2");
MyTreeNode<String> child2 = new MyTreeNode<>("Child2");
child2.addChild("Grandchild3");
root.addChild(child1);
root.addChild(child2);
root.addChild("Child3");
root.addChildren(Arrays.asList(
new MyTreeNode<>("Child4"),
new MyTreeNode<>("Child5"),
new MyTreeNode<>("Child6")
));
for(MyTreeNode node : root.getChildren()) {
System.out.println(node.getData());
}
参考:here and here
这是我在 main 方法中尝试的。 (一个父节点指向它自己并且等级为0。)
public static void main(String[] args) {
DisjointSet x = new DisjointSet();
**Node<T> parent = new Node<T>(parent, 0);**
}
这是我的错误信息:
错误:无法从静态上下文中引用非静态类型变量 T
错误:无法从静态上下文中引用非静态类型变量 T
等号两边的 T 似乎各有一个错误。
错误是因为您正在从静态方法中访问非静态实例变量
接口的所有成员字段默认为public
、static
和final
。
由于内部接口默认为 static
,您不能从 static
字段或方法中引用 T
。
T 实际上与 class 的实例相关联,如果它与 static
字段或方法相关联,则该字段或方法与 class 那就没意义了
您可以创建一个 class,其中包含以下信息:
public class MyTreeNode<T>{
public T data = null;
public MyTreeNode parent = null;
public MyTreeNode(T data) {
this.data = data;
}
public void addChild(MyTreeNode child) {
child.setParent(this);
this.children.add(child);
}
public void addChild(T data) {
MyTreeNode<T> newChild = new MyTreeNode<>(data);
newChild.setParent(this);
children.add(newChild);
}
public void addChildren(List<MyTreeNode> children) {
for(MyTreeNode t : children) {
t.setParent(this);
}
this.children.addAll(children);
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
和主要示例:
MyTreeNode<String> root = new MyTreeNode<>("Root");
MyTreeNode<String> child1 = new MyTreeNode<>("Child1");
child1.addChild("Grandchild1");
child1.addChild("Grandchild2");
MyTreeNode<String> child2 = new MyTreeNode<>("Child2");
child2.addChild("Grandchild3");
root.addChild(child1);
root.addChild(child2);
root.addChild("Child3");
root.addChildren(Arrays.asList(
new MyTreeNode<>("Child4"),
new MyTreeNode<>("Child5"),
new MyTreeNode<>("Child6")
));
for(MyTreeNode node : root.getChildren()) {
System.out.println(node.getData());
}
参考:here and here