# Differentiating Array definitions

**URL:** https://discourse.processing.org/t/differentiating-array-definitions/5834
**Category:** Coding Questions
**Created:** [November 22, 2018, 8:31pm UTC](https://discourse.processing.org/t/differentiating-array-definitions/5834 "2018-11-22T20:31:28Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![LordCacops](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lordcacops/32/2280_2.png) [@LordCacops](https://discourse.processing.org/u/LordCacops)
#### Post date: [November 22, 2018, 8:31pm UTC](https://discourse.processing.org/t/differentiating-array-definitions/5834/1 "2018-11-22T20:31:28Z")

</div>

Hello friends,

I am programming a _genome_ that’s made of _chromosomes_. Each chromosome is an array. Each defines its values through symmetry producing operations (symmetry as in retrogrades of the integer sequence of its segments).

I made a function for this, so that I can call it in setup. It makes an array called _tempChromosome_. I would like that array to define each separate chromosome array. The problem is that it is defining all of the chromosomes as the same, as you can see in the console, but I want them to be different from each other!

```auto
int[] tempChromosome = new int[16];
int[] chromosome0 = new int[16];
int[] chromosome1 = new int[16];
int[] chromosome2 = new int[16];
int[] chromosome3 = new int[16];
int[] chromosome4 = new int[16];
int[] chromosome5 = new int[16];

//int[] genotype = new int[6];

void setup() {

  determineSymmetry(); //stochastic generation of number pattern. it defines tempChromosome.
  chromosome0 = tempChromosome;
  determineSymmetry(); //I would like this to redefine tempChromosome, so that chromosome1 is different from chromosome0.
  chromosome1 = tempChromosome;
  determineSymmetry(); // and so on, with each call to the function..., so that every chromosome is different. 
  chromosome2 = tempChromosome;
  determineSymmetry();
  chromosome3 = tempChromosome;
  determineSymmetry();
  chromosome4 = tempChromosome;
  determineSymmetry();
  chromosome5 = tempChromosome;

  println("=o=o=o=o=chromosome0");
  printArray(chromosome0);
  println("=o=o=o=o=chromosome1");
  printArray(chromosome1);
  println("=o=o=o=o=chromosome2");
  printArray(chromosome2);
  println("=o=o=o=o=chromosome3");
  printArray(chromosome3);
  println("=o=o=o=o=chromosome4");
  printArray(chromosome4);
  println("=o=o=o=o=chromosome5");
  printArray(chromosome5);
}

void draw() {
}

 int[] determineSymmetry() {

  for (int i = 0; i < 4; i++) {
    tempChromosome[i] = int(random(5));
  }

  float smallSymm1 = random(3);
  float largeSymm = random(3);
  float smallSymm2 = random(3);

  if (smallSymm1 < 1) { //repeat sequence
    tempChromosome[4] = tempChromosome[0];
    tempChromosome[5] = tempChromosome[1];
    tempChromosome[6] = tempChromosome[2];
    tempChromosome[7] = tempChromosome[3];
  } else if (smallSymm1 < 2) { //retrograde sequence     
    tempChromosome[4] = tempChromosome[3];
    tempChromosome[5] = tempChromosome[2];
    tempChromosome[6] = tempChromosome[1];
    tempChromosome[7] = tempChromosome[0];
  } else {                         
    for (int i = 4; i < 8; i++) { //generate another random sequence
      tempChromosome[i] = int(random(5));
    }
  }

  if (largeSymm < 1) { //repeat sequence (large scale)
    tempChromosome[8] = tempChromosome[0];
    tempChromosome[9] = tempChromosome[1];
    tempChromosome[10] = tempChromosome[2];
    tempChromosome[11] = tempChromosome[3];
    tempChromosome[12] = tempChromosome[4];
    tempChromosome[13] = tempChromosome[5];
    tempChromosome[14] = tempChromosome[6];
    tempChromosome[15] = tempChromosome[7];
  } else if (largeSymm < 2) { //retrograde sequence (large scale) 
    tempChromosome[8] = tempChromosome[7];
    tempChromosome[9] = tempChromosome[6];
    tempChromosome[10] = tempChromosome[5];
    tempChromosome[11] = tempChromosome[4];
    tempChromosome[12] = tempChromosome[3];
    tempChromosome[13] = tempChromosome[2];
    tempChromosome[14] = tempChromosome[1];
    tempChromosome[15] = tempChromosome[0];
  } else {                         
    for (int i = 8; i < 12; i++) { //generate another random sequence of four numbers
      tempChromosome[i] = int(random(5));
    }

    if (smallSymm2 < 1) { //repeat sequence
      tempChromosome[12] = tempChromosome[8];
      tempChromosome[13] = tempChromosome[9];
      tempChromosome[14] = tempChromosome[10];
      tempChromosome[15] = tempChromosome[11];
    } else if (smallSymm2 < 2) { //retrograde sequence     
      tempChromosome[12] = tempChromosome[11];
      tempChromosome[13] = tempChromosome[10];
      tempChromosome[14] = tempChromosome[9];
      tempChromosome[15] = tempChromosome[8];
    } else {                         
      for (int i = 12; i < 16; i++) { //generate another random sequence
        tempChromosome[i] = int(random(5));
      }
    }
  }
  
  return tempChromosome;
}

```

I feel this is simple to fix but I don’t know how. Help would be appreciated. Thank you.

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [November 22, 2018, 9:07pm UTC](https://discourse.processing.org/t/differentiating-array-definitions/5834/2 "2018-11-22T21:07:53Z")

</div>

> [@How to copy an array](https://discourse.processing.org/t/how-to-copy-an-array/5170/4):
>
> Just directly invoke method [clone()](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Object.html#clone()) over the array container: nerd_face class Testos { final float[] a, b; Testos(final float... arr) { b = (a = arr).clone(); } }

> [@Array Index Out of Bounds error on Array List](https://discourse.processing.org/t/array-index-out-of-bounds-error-on-array-list/3006/5):
>
> String[] originalArray = { "apple", "orange", "grape", "banana", "lemon" }; String[] clonedArray = originalList.clone();

---

<div class="post-metadata">

### Author: ![LordCacops](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lordcacops/32/2280_2.png) [@LordCacops](https://discourse.processing.org/u/LordCacops)
#### Post date: [November 22, 2018, 9:57pm UTC](https://discourse.processing.org/t/differentiating-array-definitions/5834/3 "2018-11-22T21:57:29Z")

</div>

I don’t understand why, but that worked. Thank you!

---

<div class="post-metadata">

### Author: ![Jakub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jakub/32/2192_2.png) [@Jakub](https://discourse.processing.org/u/Jakub)
#### Post date: [November 22, 2018, 11:34pm UTC](https://discourse.processing.org/t/differentiating-array-definitions/5834/4 "2018-11-22T23:34:03Z")

</div>

Arrays in Processing (and Java) are objects, which means that the variable only holds a reference. When you assign the array to a different variable, it does not create a copy of the array. Both variables will point to the same array. See this example:

```auto
int[] array1 = {1, 2, 3};
int[] array2 = {4, 5, 6};

printArray(array1); // 1, 2, 3
printArray(array2); // 4, 5, 6

array1 = array2;
// now both variables refer to the same array

printArray(array1); // 4, 5, 6
printArray(array2); // 4, 5, 6

// there is now only one array, changes are
// visible through both variables (references)
array2[0] = 42;

printArray(array1); // 42, 5, 6
printArray(array2); // 42, 5, 6

array1 = array2.clone();
// now there are two separate arrays
array2[0] = 21;

printArray(array1); // 42, 5, 6
printArray(array2); // 21, 5, 6

```

---

<div class="post-metadata">

### Author: ![LordCacops](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lordcacops/32/2280_2.png) [@LordCacops](https://discourse.processing.org/u/LordCacops)
#### Post date: [November 23, 2018, 12:06pm UTC](https://discourse.processing.org/t/differentiating-array-definitions/5834/5 "2018-11-23T12:06:28Z")

</div>

How do I concatenate all my arrays into a single long array? _concat_ does not seem to be working as the error appears: The function “concat()” expects parameters like: “concat(int[],int[])”

```auto
void setup() {

  
  
  println("chromosome0");
  determineSymmetry(); 
  int [] chromosome0 = tempChromosome.clone();
  printArray(chromosome0);

  println("chromosome1");  
  determineSymmetry();
  int [] chromosome1 = tempChromosome.clone();
  printArray(chromosome1);  
  
  println("chromosome2");  
  determineSymmetry(); 
  int [] chromosome2 = tempChromosome.clone();
  printArray(chromosome2);  
  
  println("chromosome3");  
  determineSymmetry();
  int [] chromosome3 = tempChromosome.clone();
  printArray(chromosome3);  

  println("chromosome4");
  determineSymmetry();
  int [] chromosome4 = tempChromosome.clone();
  printArray(chromosome4);  
  
  println("chromosome5");
  determineSymmetry();
  int [] chromosome5 = tempChromosome.clone();
  printArray(chromosome5);
  
int[] genotype = concat(chromosome0, chromosome1, chromosome2, chromosome3, chromosome4, chromosome5);

 
 println("genotype");
 printArray(genotype);
}

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [November 23, 2018, 12:13pm UTC](https://discourse.processing.org/t/differentiating-array-definitions/5834/6 "2018-11-23T12:13:11Z")

</div>

> [@LordCacops](#):
>
> The function **concat()** expects parameters like: “concat(int,int)”

You need to do it a pair at once rather than all at once: 👫

```java
int[] genotype = concat(chromosome0, chromosome1);
genotype = concat(genotype, chromosome2);
genotype = concat(genotype, chromosome3);

// and so on...

```

> **[concat() / Reference](https://processing.org/reference/concat_.html)**
>
> Concatenates two arrays. For example, concatenating the array { 1, 2, 3 } and the array { 4, 5, 6 } yields { 1, 2, 3, 4, 5, 6 }. Both parameters must be arrays of the same datatype. When u…
