title: Why is my Object array not keeping the object it is being assigned?
body: <p>I am currently attempting a genetic algorithm program and as such in trying to find a solution I am utilising the genetic operators reproduction, mutation and crossover on a group of tree objects. I have created an initial population according to the “FULL” tree method, after which I am attempting to run a while loop for N generations. The idea is to take the array of the initial population, manipulate it according to the genetic operators and then replace the original population with the new one. </p>
  
  <p>The problem I am having is that my tree objects that are reproduced/mutated/crossed over are not mapping correctly to the correct row in the tree array. Below is a sample of my main method and my object class as well as the code I am using for creating a new population, labeled “regenerate”</p>
  
  <pre><code>tree= genTree.regenerate(tree,repRuns, mutRuns, crossRuns,param[7],tournSize,terms,Functs);
  </code></pre>
  
  <p>where tree is the original array </p>
  
  <pre><code>public static genTree[] regenerate(genTree[] original, int repRuns, int mutRuns, int crossRuns, int newDep, int tournSize, Function terms[][], Function Functs[]) {
      genTree[] copy = new genTree[original.length];
      int trace = 0;
  
      genTree tournResult;
      while (trace &lt; repRuns) {
          tournResult = genTree.beginTourn(tournSize, original);
          genTree reprodOperation = genTree.reprodOperator(tournResult);
  
          copy[trace] = reprodOperation;
          trace++;
      }
  
      while (trace &lt; (mutRuns + repRuns)) {
          tournResult = genTree.beginTourn(tournSize, original);
  
  
          genTree mutateOP = genTree.mutateOperator(tournResult, newDep, terms, Functs);
  
          copy[trace] = mutateOP;
          trace++;
      }
  
      while (trace &lt; (repRuns + mutRuns + crossRuns)) {
          tournResult = genTree.beginTourn(tournSize, original);
          genTree winner2 = genTree.beginTourn(tournSize, original);
          genTree[] crossover = genTree.crossoverOperator(tournResult, winner2);
          copy[trace] = crossover[0];
          trace++;
  
          copy[trace] = crossover[1];
          trace++;
      }
  
      for (int i = 0; i &lt; copy.length; i++) { //tried deep copy here
          original[i] = copy[i];
      }
    //used to trace through the original array,see what it looks like
      System.out.println("ORIGINAL ARRAY : ");
      for (int i = 0; i &lt; original.length; i++) {
          System.out.println(original[i].displayTreeOnly());
      }
     //see whhat copy array looks like 
      System.out.println("COPY ARRAY : ");
      for (int i = 0; i &lt; copy.length; i++) {
          System.out.println(copy[i].displayTreeOnly());
      }
  
      //  original =copy.clone();
      copy = null;
  
      return original;
  }
  </code></pre>
  
  <p>above is my attempt at creating a new population. I have tried .clone() but I still get the same results. As a test case consider the following. “Original” contains the following trees:</p>
  
  <p><code>OR AND NOT NOT NOT AND d5 a0 d0 d1
    AND AND NOT OR NOT NOT a0 d4 d3 d6
    AND NOT AND AND AND OR a3 d1 d3 d1 d4 a3
    AND OR AND OR NOT NOT OR d0 d6 d4 d4 d5 a1
    OR AND AND AND AND AND AND d0 d2 d2 d7 d2 a1 a3 d7</code></p>
  
  <p>And after running the method (before copy is made to equal original), copy should contain</p>
  
  <pre><code>AND AND NOT OR NOT NOT OR d2 d3 d6 OR AND OR OR NOT d2 d0 d7 d5 a0 d6
  AND OR AND OR NOT NOT OR AND d6 d4 d4 d5 a1 d5 d4
  AND OR AND OR NOT NOT OR AND d6 AND d4 d5 a1 d5 d4 NOT NOT d2 d3
  AND NOT AND AND AND OR a3 d1 d3 d1 d4 d2
  AND AND NOT OR NOT NOT OR d2 d3 d6 OR AND OR OR NOT a3 d0 d7 d5 a0 d6
  </code></pre>
  
  <p>However instead ,both “original" and “copy" contain the following: </p>
  
  <pre><code>AND AND NOT OR NOT NOT OR d2 d3 d6 OR AND OR OR NOT a3 d0 d7 d5 a0 d6
  AND OR AND OR NOT NOT OR AND d6 AND d4 d5 a1 d5 d4 NOT NOT d2 d3
  AND OR AND OR NOT NOT OR AND d6 AND d4 d5 a1 d5 d4 NOT NOT d2 d3
  AND NOT AND AND AND OR a3 d1 d3 d1 d4 d2
  AND AND NOT OR NOT NOT OR d2 d3 d6 OR AND OR OR NOT a3 d0 d7 d5 a0 d6
  </code></pre>
  
  <p>Where some trees are mapped to the correct position yet the array is not correctly mapped completely</p>
  
  <p>Function class:</p>
  
  <pre><code>public class Function {
      private String label;
      private int arity;
  
      public Function() {
  
      }
  
      public void setArity(int arty) {
          arity = arty;
      }
  
      public void setLabel(String lbl) {
          label = lbl;
      }
  
      public String getLabel() {
          return label;
      }
  
      public int getArity() {
          return arity;
      }
  }
  </code></pre>
  
  <p>genTree basic: </p>
  
  <pre><code>public class genTree {
  
      public Node root;
      private int hits = 0;
      static long seed = System.currentTimeMillis();
      static Random rand = new Random(seed);
      static Random randBool = new Random();
      static genTree[] newPop;
      static genTree winner;
      private int numberNodes = 0;
      private boolean success;
  
      public genTree() {
          numberNodes = 0;
      }
  </code></pre>