Here is the link to the data structure: http://git.eclipse.org/c/platform/eclipse.platform.ui.git/plain/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/misc/DisjointSet.java
Here is what I tried in the main method. (A parent node points to itself and has a rank of 0.)
public static void main(String[] args) {
DisjointSet x = new DisjointSet();
**Node<T> parent = new Node<T>(parent, 0);**
}
Here are my error messages:
Error: non-static type variable T cannot be referenced from a static context
Error: non-static type variable T cannot be referenced from a static context
Looks like one error for T on each side of the equal sign.
The error is because you are accessing a non-static instance variable from within a static method
All member fields of an interface are by default public
, static
and final
.
Since inner interface is static
by default, you can't refer to T
from static
fields or methods.
T is actually associated with an instance of a class, if it were associated with a static
field or method which is associated with class then it wouldn't make any sense
You can create one class with informations:
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;
}
and main with examples:
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());
}