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]
以上是 Java程序将数组元素向右移动 的全部内容, 来源链接: utcz.com/z/351475.html