java写一个方法使数组[1,3,5,7]循环右移2位后为[5,7,1,3]

java写一个方法使数组[1,3,5,7]循环右移2位后为[5,7,1,3]
2024-12-25 07:35:10
推荐回答(1个)
回答1:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class $ {

public static void main(String... _) {

Integer[] arr = { 1, 3, 5, 7 };

List data = new ArrayList();

int len = arr.length / 2;

for (int i = len; i < arr.length; i++) {
data.add(arr[i]);
}
for (int i = 0; i < len; i++) {
data.add(arr[i]);
}

arr = (Integer[]) data.toArray(new Integer[0]);
System.out.println(Arrays.deepToString(arr));
}
}