Passing an Array through recursive class objects

Hello !
I am working on a data visualization for a tree, generated with processing.
I have created a class that creates a branch of the tree and then recursively creates another object for each child branch.

To connect this to a database, I want to give each branch an ID number, where the trunk will have the ID of 1,0,0… branches on the next level will have IDs 1,1,0… , 1,2,0…, 1,3,0,… (of course up to a bigger depth than 3 in the final patch).

My understanding is, that when calling the constructor of the child branches from within the parent branch, I can pass variables to them. I have successfully done so with “normal” integer or float variables. But when I try to use an IntList or an array of any sorts, something happens that I can not figure out.

While the code traverses to the branches of each tree in the expected order, the array (called myIDList) does behave strangely.

As you can see in the output screenshot, integers are passed fine, while arrays are not.
The example below tries to create one branch (the trunk) with two sub-branches.
The variable (depthLevel) is passed on from the trunk to each sub-branches individually, as I think it should.
The array is passed from the trunk to the first branch (called left) correctly, but instead of passing the same values to the right branch, it receives the values from its sibling.

I am really wrecking my brain on this… Can someone enlighten me as to why something that works for int variables but not for arrays ? Seeing my deadline approaching I would be really thankful if one of you guys know a way out of this :wink:

(below is a screenshot of the output window)


Branch b;
int[] defaultArray; // an initial array to pass values to the first level of iteration

//-------------------------------------------------------
//------------------------------------------------- SETUP
void setup() {
  size(300, 400);
  //--------------------- put zeros in the first array to be passed
  defaultArray = new int[3];
  for (int i = 0; i<defaultArray.length; i++) {
    defaultArray[i] = 0;
  }
  //--------------------- create the inital class instance
  b = new Branch (defaultArray, 1, 0, "begin");
}

//-------------------------------------------------------
//------------------------------------------ Branch Class
class Branch {
   //------------------------------------------ variables
  int depthLevel;
  int [] myIDList; //the list in which ID information is stored
  int addID;
  String text;
  Branch subBranch;
  
  //------------------------------------------ constructor
  Branch (int[] array, int aID, int level, String s) {
    println(s);
    depthLevel = level;
    println("level is: "+depthLevel);
    myIDList = array;
    println("received: ");
    println(myIDList);
    addID = aID;
    println("received addID: "+addID);
    calculate();
  }
  
  //------------------------------------------ methods
  void calculate () {
    myIDList[depthLevel] = addID;
    println("---");
    println(myIDList);
    println("------------");
    depthLevel++;
    if (depthLevel < 2) {
      addID = 1;
      subBranch = new Branch (myIDList, addID, depthLevel, "left branch");
      addID = 2;
      subBranch = new Branch (myIDList, addID, depthLevel, "right branch");
    }
  }
}

Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Object.html#clone()

myIDList = array;myIDList = array.clone();

That is… incredible. Thank you so much ! The world saved with a single line :smiley:

1 Like