通过构造函数传递时对象发生变化
Object changed when passed through a constructor
我正在实现一个 m 叉树,其中 MTreeNode 有一个 AnyType 元素,一个 int m 确定它的最大子元素数量,以及一个嵌套的 ArrayList 作为它的子元素的 link。当我通过新的 MTreeNode 的构造函数传递 MTreeNode 的 ArrayList 时,所述 ArrayList 内的 MTreeNodes 也将它们的 ArrayList 更改为它们所属的 ArrayList。
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;
}
}
这是我的主要测试:
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");
}
这导致:
MTreeNode@2cfb4a64: testB Node
null: testB's 1st child
MTreeNode@2cfb4a64: array's first element
null: testB's 1st child after being added to array
MTreeNode@2cfb4a64: testB's 1st child after being set as myRoot's child
MTreeNode@2cfb4a64: testB after testB.children = new ArrayList();
null: testB's first child after children = new ArrayList()
MTreeNode@61a52fbd: myRoot Node
null: myRoot's 1st child
null: myRoot's 2nd child
MTreeNode@233c0b17: testB's child after adding a new MTreeNode to testB's children
MTreeNode@233c0b17: myRoots's child after adding a new MTreeNode to testB's children
如图所示,当我将包含 testB 的数组传递到新的 MTreeNode myRoot 时,testB 的子 ArrayList 也变成了数组。对这里发生的事情有什么帮助吗?
删除 children 上的静态修饰符。将其声明为静态意味着您对任何 MTreeNode 实例所做的任何更改 children 将应用于每个 MTreeNode。这就是为什么当您将 myRoot 的 children 设置为数组时,testB 和所有其他 MTreeNode 的 children 也设置为数组
阅读更多:https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
我正在实现一个 m 叉树,其中 MTreeNode 有一个 AnyType 元素,一个 int m 确定它的最大子元素数量,以及一个嵌套的 ArrayList 作为它的子元素的 link。当我通过新的 MTreeNode 的构造函数传递 MTreeNode 的 ArrayList 时,所述 ArrayList 内的 MTreeNodes 也将它们的 ArrayList 更改为它们所属的 ArrayList。
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;
}
}
这是我的主要测试:
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");
}
这导致:
MTreeNode@2cfb4a64: testB Node
null: testB's 1st child
MTreeNode@2cfb4a64: array's first element
null: testB's 1st child after being added to array
MTreeNode@2cfb4a64: testB's 1st child after being set as myRoot's child
MTreeNode@2cfb4a64: testB after testB.children = new ArrayList();
null: testB's first child after children = new ArrayList()
MTreeNode@61a52fbd: myRoot Node
null: myRoot's 1st child
null: myRoot's 2nd child
MTreeNode@233c0b17: testB's child after adding a new MTreeNode to testB's children
MTreeNode@233c0b17: myRoots's child after adding a new MTreeNode to testB's children
如图所示,当我将包含 testB 的数组传递到新的 MTreeNode myRoot 时,testB 的子 ArrayList 也变成了数组。对这里发生的事情有什么帮助吗?
删除 children 上的静态修饰符。将其声明为静态意味着您对任何 MTreeNode 实例所做的任何更改 children 将应用于每个 MTreeNode。这就是为什么当您将 myRoot 的 children 设置为数组时,testB 和所有其他 MTreeNode 的 children 也设置为数组
阅读更多:https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html