package Test;
import java.util.Arrays;
public class Test01 {
public static void main(String[] args) {
Integer[] array = {20,32,11,56,78,39};
System.out.println(Arrays.toString(array));
moveToRight(array,3);
System.out.println(Arrays.toString(array));
}
@SuppressWarnings("unchecked")
private static <T> void moveToRight(T[] x,int k){
k = k % x.length;
if(k>0){
T[] y = (T[]) new Object[x.length];
for (int i = 0; i < x.length; i++) {
y[(i+k)%x.length] = x[i];
}
System.arraycopy(y,0,x,0,x.length);
}
}
}
Java实现把一个数组中的某个数向后移动
最新推荐文章于 2024-06-12 00:08:45 发布