I’ve got an array array of size N. For every 3 indexes in it I want to take them out and declare and assign those values to another array of size 3. I then want to go back to that array and take the next 3 and put it in a different array of size 3. I’ll iterate like this for 3 different arrays of size 3 a1,a2,a3 once this is done I want to empty a1,a2,a3 and re add the NEXT 3 values to the 3 arrays of size 3 repeating this on till we reach array.length
What would be the best / most efficient way of doing this?
That’s a pretty brittle but fast way of doing it. Each time the previous values are overwritten, so no need to clear. If you do need to have
arrayNclear, you can justArrays.fill(arrayN, null)after the loop.EDIT: For the less brittle answer, I’m going to assume you’d be inflating m x n arrays. Instead of hard coding a1, a2, … am, make a 2D array
a[m][n].and, as Adrian suggests in the comments, declare
ioutside the loop and use its value relative to N to deal with leftovers as appropriate.