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