Tag: processing

Processing: arrayCopy() for multidimensional arrays

I discovered a quirk in how Processing‘s arrayCopy() method works, and as it took me a while to figure out, I thought I’d share it:

If you’re copying a 2d, 3d, etc array, then do NOT use arrayCopy(). It’s designed for use on 1d arrays only. If you try it, it will seem to work, however rather than copying the data, it actually copies the references – in layman’s terms, if you then make a change to one array, the change will also occur in the other.

Assuming you want the two arrays to be different for most of the time, you’ll need to iterate through them, like so:

for (int x=0; x<src.length; x++) {
  for (int y=0; y<src[0].length; y++) {
    dest[x][y] = src[x][y];
  }
}

Where src and dest are your source and destination arrays.