私はクラスEightPuzzle
を持っています。親ノードに子ノード、子ノードなどをJavaで設定/設定するにはどうすればよいですか?
T
がオブジェクト
EightPuzzle
のあるクラス
Node<T>
を持って
public class EightPuzzle {
int[][] board = new int[3][3];
//code here
}
。私に1つのNode<EightPuzzle> parent
がある場合、どのように子どもを設定するのですか?その子供たちの子供たち?子どもたちがArrayList<Node<EightPuzzle>>
の場合は、同様に?
public class Node<T> {
private List<Node<T>> children = new ArrayList<Node<T>>();
private Node<T> parent = null;
private T data = null;
public Node(T data) {
this.data = data;
}
public Node(T data, Node<T> parent) {
this.data = data;
this.parent = parent;
}
public List<Node<T>> getChildren() {
return children;
}
public void setParent(Node<T> parent) {
parent.addChild(this);
setParentInternal(parent);
}
public void setParentInternal(Node<T> parent){
this.parent = parent;
}
public void addChild(T data) {
addChild(new Node<T>(data));
}
public void addChild(Node<T> child) {
child.setParentInternal(this);
this.children.add(child);
}
//may not use this and set data
public T getData() {
return this.data;
}
public void setData(T data) {
this.data = data;
}
public boolean isRoot() {
return (this.parent == null);
}
public boolean isLeaf() {
if(this.children.size() == 0)
return true;
else
return false;
}
public void removeParent() {
this.parent = null;
}
何を取得しようとしていますか? – 4castle
Object EightPuzzleでparent.childrenを設定しようとしていて、子などを設定しよう –
'Node'オブジェクトを作成して、それらを' addChild'または 'setParent'と一緒にリンクしようとしましたか?あなたがどこにいるのか分かりません。 – 4castle