Ich implementiere eine m -ary-Struktur, wo ein MTreeNode hat ein AnyType-Element, eine int m, die seine maximale Anzahl von Kindern bestimmt, und eine verschachtelte ArrayList als Link zu seinen Kindern . Wenn ich eine ArrayList von MTreeNodes durch einen neuen Konstruktor von MTreeNode übergebe, haben die MTreeNodes innerhalb der ArrayList auch ihre ArrayLists in die ArrayList geändert, zu der sie gehören.Object geändert, wenn durch einen Konstruktor
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;
}
}
Hier ist meine Hauptprüfung:
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");
}
Dies führt dazu:
[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
Wie zu sehen ist, wenn ich Array übergeben, die TestB in die neue MTreeNode myRoot enthält, TestB Kinderarraylist wird auch Array. Irgendwelche Hilfe, was hier vor sich geht?
Holy shit mein Mann es Ihnen danken –