Say I have a two dimensional array: vectors[x][y]
, and the initial array structure looks like this:
vectors = [
[0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0,]
]
After some calculations, the data in the array is randomized. What is the fastest way and most efficient way to return the array to it's initial state?
I know that I could just hardcode the above zeroed array and set vectors equal to it again, but I also know that an algorithm such as:
for (var x = 0; x < vectors.length; x++) {
for (var y = 0; y < vectors[x].length; y++) {
vectors[x][y] = 0;
}
}
is O(x * y).
So which is the better way? And is there a better, even faster/more efficient way to solve this?
And for the general case of zeroing a multi-dimensional array of any length, which is the best way? (I'm working in JavaScript if it matters)