zoukankan      html  css  js  c++  java
  • 【Java基础】System.arraycopy()的使用详解

    由于在Java中System.arraycopy()方法在一维数组和二维数组中的表现不同,所以做了一个测试


    public static void main(String[] args) {
    
    		int[] a = new int[] { 1, 2, 3, 4, 5, 6 };
    		int[] b = new int[8];
    		//System.arraycopy(原数组, 原数组起始位置, 目标数组, 目标数组起始位置, 复制长度);
    		System.arraycopy(a, 0, b, 0, 3);
    		b[1] = 100;
    		System.out.print(Arrays.toString(a) + " ");
    		System.out.print(Arrays.toString(b) + " ");
    		System.out.println();
    		//结果:[1, 2, 3, 4, 5, 6] [1, 100, 3, 0, 0, 0, 0, 0]
    		//一维数组b中的值的改变并没有影响到原数组a中的元素的值
    		
    		int[][] c = new int[][] { { 1, 1 }, { 2, 2 }, { 3, 3 } };
    		int[][] d = new int[3][];
    		//System.arraycopy(原数组, 原数组起始位置, 目标数组, 目标数组起始位置, 复制长度);
    		System.arraycopy(c, 0, d, 0, 3);
    		d[1][1] = 100;
    		
    		for (int i = 0; i < c.length; i++) {
    			for (int j = 0; j < c[i].length; j++) {
    				System.out.print(c[i][j]+" ");
    			}
    		}
    		System.out.println();
    		for (int i = 0; i < d.length; i++) {
    			for (int j = 0; j < d[i].length; j++) {
    				System.out.print(d[i][j]+" ");
    			}
    		}
    		//输出结果,可以看出,在二维数组中,目标数组的元素的改变影响到了原二维数组的值
    		//1 1 2 100 3 3 
    		//1 1 2 100 3 3 
    	}


  • 相关阅读:
    在windwos创建的脚本文件在linux环境中无法执行的问题
    shell的文件锁操作
    systemd target
    算法-排序数组
    算法-存在重复元素
    算法-移除元素
    算法-两数之和
    touch事件详解
    小程序 打包太大
    taro/vue 左滑删除购物车
  • 原文地址:https://www.cnblogs.com/oversea201405/p/3749581.html
Copyright © 2011-2022 走看看