通过构造函数

时间:2017-11-11 23:21:15

标签: java arraylist constructor tree field

我正在实现一个m-ary树,其中一个MTreeNode有一个AnyType元素,一个确定它的最大子节点的int m,以及一个嵌套的ArrayList作为它的子节点的链接。当我通过新的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也成为数组。对这里发生的事情有任何帮助吗?

1 个答案:

答案 0 :(得分:0)

删除子项上的static修饰符。将其声明为静态意味着您对任何MTreeNode实例的子项所做的任何更改都将应用于每个MTreeNode。这就是为什么当你将myRoot的子项设置为数组时,testB和其他每个MTreeNode的子项都设置为数组

了解详情:https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html