Java - Rotating array -
So the target elements are to rotate the right a
bar in the example; If a == 2
, then the array = {0,2,2,3,4}
will become array = {3,4,0,1, 2}
Here I have:
for (int x = 0; x & lt; = array.length-1; x ++ ) {Array [x + a] = array [x]; }
However, it fails to account for when [x + a]
is greater than the length of the array I have read that those people Should be stored which are more in a different array, but seeing as a
variable I'm not sure that this is the best solution to add a modulo array length to your code:
//, copy the array // copy (Int x = 0; x & lt; = array.length-1; x ++) {newArray [(x + a)% array.length] = array [x]; }
You should also create a new array
to copy, so that you do not overwrite prices, which you will need later.
Comments
Post a Comment