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:

Create a new array before the same size for
 

//, 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

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -