Java – rotate array
•
Java
Therefore, the goal is to rotate the elements in the array once
This is what I have:
for (int x = 0; x <= array.length-1; x++){ array[x+a] = array[x]; }
However, this does not explain when [x a] is greater than the length of the array I read that I should store larger data in different arrays, but as variables, I'm not sure this is the best solution Thank you in advance
Solution
Add modulus group length to code:
// create a newArray before of the same size as array // copy for(int x = 0; x <= array.length-1; x++){ newArray[(x+a) % array.length ] = array[x]; }
You should also create a new array to copy to so that you do not overwrite the values you need later
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码