Java程序将数组元素向右移动
让我们首先创建一个int数组-
int[] arr = { 10, 20, 30, 40, 50, 60, 70, 80, 90 };现在,将数组元素向右移动arraycopy()并正确放置元素,以使它向右移动-
System.arraycopy(arr, 0, arr, 1, arr.length - 1);
示例
import java.util.Arrays;
public class Demo {
public static void main(String[] argv) throws Exception {
int[] arr = { 10, 20, 30, 40, 50, 60, 70, 80, 90 };
System.out.println("Initial array...\n"+Arrays.toString(arr));
System.arraycopy(arr, 0, arr, 1, arr.length - 1);
System.out.println("Array after shifting to the right...");
System.out.println(Arrays.toString(arr));
}
}输出结果
Initial array... [10, 20, 30, 40, 50, 60, 70, 80, 90] Array after shifting to the right... [10, 10, 20, 30, 40, 50, 60, 70, 80]
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短