I am using a coordinate system x (width), y (height), z (Depth)
Just to clear confusion if there is any x & y are a flat plane and I am using Z as elevation.
I am going to be accessing the array millions of times per second and benchmarking shows that a 1D array using index is faster and I would like to squeeze as much efficiency as possible so that other things can use that time
For example a 2D array --> 1D array creation is just
Object[] oneDArray = new Object[width * height]
and to index the array I can just use the following.
Object obj = oneDArray[x + y * width]
I did find the following on stackoverflow but I am not entirely sure which one is correct How to "flatten" or "index" 3D-array in 1D array?
The "Correct" answer says to index the array do the following
Object[] oneDArray = new Object[width * height * depth]
Object obj = oneDArray[x + WIDTH * (y + DEPTH * z)]
But then another answer says that the "Correct" answer is wrong and uses the following
Object[] oneDArray = new Object[width * height * depth]
Object obj = oneDArray[x + HEIGHT* (y + WIDTH* z)]
What is the correct way to read a flattened 3D array?