Java - Rotating array

后端 未结 14 1142

So the goal is to rotate the elements in an array right a times. As an example; if a==2, then array = {0,1,2,3,4} would become

14条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-27 20:02

    Another way is copying with System.arraycopy.

        int[] temp = new int[array.length];
        System.arraycopy(array, 0, temp, a, array.length - a);
        System.arraycopy(array, array.length-a, temp, 0, a);
    

提交回复
热议问题