Why is the pixels array "unidimensional" and not bidimensional?

Here are some drawbacks of multidimensional arrays in Processing (Java):

  • you need to create one array to hold all the rows and then one array for each row - initialization is more complicated
  • some rows can have different length than others
  • some rows can be null
  • it’s harder to iterate over all pixels: now you need two loops instead of one
  • harder to copy and resize: instead of System.arrayCopy() one array you need to copy each row separately
  • it might be a bit slower, because to access a value you need to follow two steps of indirection instead of one (load array holding the rows, load array holding the values in the row, access the value); with single array all the values are contiguous in memory, which should be faster to access
  • there is probably more

Basically the complexity of most operations increases, more things can go wrong, it might be slower, and the only benefit seems to be an “easier” access.

3 Likes