MTreeNode에 AnyType 요소가있는 m-ary 트리를 구현 중이며 최대 자식 수를 결정하는 int m 및 해당 자식에 대한 링크로 작동하는 중첩 ArrayList . MTreeNode의 ArrayList를 새로운 MTreeNode의 생성자를 통해 전달할 때, 상기 ArrayList 내부의 MTreeNodes 또한 ArrayList의 일부가되는 ArrayList로 변경됩니다. 나는 새로운 MTreeNode myRoot로를 TestB을 포함 배열을 통과 할 때,를 TestB의 아이들이 ArrayList와, 보는 바와 같이생성자를 통해 전달할 때 개체가 변경됨
[email protected]: testB Node
null: testB's 1st child
[email protected]: array's first element
null: testB's 1st child after being added to array
[email protected]: testB's 1st child after being set as myRoot's child
[email protected]: testB after testB.children = new ArrayList();
null: testB's first child after children = new ArrayList()
[email protected]: myRoot Node
null: myRoot's 1st child
null: myRoot's 2nd child
[email protected]: testB's child after adding a new MTreeNode to testB's children
[email protected]: myRoots's child after adding a new MTreeNode to testB's children
:이 결과
public static void main(String[] args) {
MTreeNode<String> testB = new MTreeNode<String>("B", 2);
System.out.println(testB + ": testB Node");
System.out.println(testB.children.get(0)+ ": testB's 1st child\n");
ArrayList<MTreeNode> array = new ArrayList();
array.add(testB);
System.out.println(array.get(0) +": array's first element");
System.out.println(testB.getChildren().get(0)+ ": testB's 1st child after being added to array\n");
MTreeNode<String> myRoot = new MTreeNode<String>("A", 2, array);
System.out.println(testB.getChildren().get(0)+ ": testB's 1st child after being set as myRoot's child\n");
ArrayList<MTreeNode> array2 = new ArrayList();
testB.children = array2;
System.out.println(testB + ": testB after testB.children = new ArrayList();");
System.out.println(testB.children.get(0) +": testB's first child after children = new ArrayList()\n");
System.out.println(myRoot +": myRoot Node");
System.out.println(myRoot.children.get(0)+": myRoot's 1st child");
System.out.println(myRoot.children.get(1)+": myRoot's 2nd child\n");
MTreeNode<String> testC = new MTreeNode<String>("C", 2);
array2.add(testC);
testB.children = array2;
System.out.println(testB.children.get(0) +": testB's child after adding a new MTreeNode to testB's children");
System.out.println(myRoot.children.get(0) +": myRoots's child after adding a new MTreeNode to testB's children");
}
:
public class MTreeNode<AnyType>{
private static class ArrayList<AnyType>{
private AnyType[] array;
private static final int size = 5;
private int index;
private int actSize;
public ArrayList(){
AnyType[] newArray = (AnyType[]) new Object[size];
this.array = newArray;
this.actSize = size;
}
public AnyType get(int temp){
if(temp > this.index-1){
return null;
}
if(this.array[temp] == null)
return null;
if(temp < 0) throw new ArrayIndexOutOfBoundsException("Index is negative!");
return this.array[temp];
}
public void add(AnyType obj){
if(this.index == this.actSize-1) doubleSize();
array[this.index] = obj;
this.index++;
}
}
// *** MTreeNode begins
private AnyType element;
private int m;
private static ArrayList<MTreeNode> children;
public MTreeNode(AnyType element, int m, ArrayList<MTreeNode> c){
this.element = element;
this.m = m;
this.children = c;
}
public MTreeNode(AnyType el, int m){
this.element = el;
this.m = m;
this.children = new ArrayList<MTreeNode>();
}
public ArrayList<MTreeNode> getChildren(){
return this.children;
}
}
여기 내 주요 테스트입니다 배열이됩니다. 여기에 무슨 일이 일어나고 있는지에 대한 도움이 필요 하신가요?
내 남자가 당신을 감사 젠장 –