My question should actually be really simple: I have an object array of Players.(players[]) I want to have a function that rotates this array until an index:
public void rotateArray(Object[] array, int index)
This would convert
{Player1, Player2, Player3, Player4, Player5}
with an index of 2 to:
{Player3, Player4, Player5, Player1, Player2}
But I want to prevent issues with references. I've tried System.arraycopy() but either I was to dumb to get it working or I is the wrong method for this.
This one-line solution rotates the array in-place, with constant extra memory and linear time:
Collections.rotate(Arrays.asList(array), -index);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With