Java 程序,将阵列元素左移
我们首先创建一个 int 数组 −
int[] arr = { 10, 20, 30, 40, 50, 60, 70, 80, 90 };
现在,使用 arraycopy() 将阵列元素左移并正确放置元素,使其左移 −
System.arraycopy(arr, 1, arr, 0, 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...
"+Arrays.toString(arr)); System.arraycopy(arr, 1, arr, 0, arr.length - 1); System.out.println("Array after shifting to the left..."); System.out.println(Arrays.toString(arr)); } }
输出
Initial array... [10, 20, 30, 40, 50, 60, 70, 80, 90] Array after shifting to the left... [20, 30, 40, 50, 60, 70, 80, 90, 90]
广告