• 【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 
    	}


  • 相关阅读:
    POJ3164 Command Network
    UVa11401 Triangle Counting
    UVa11174 Stand in a Line
    UVa11806 Cheerleaders
    Uva11538 Chess Queen
    Bzoj3130 [Sdoi2013]费用流
    Bzoj3262 陌上花开
    模拟25A 题解
    模拟24 题解
    模拟23 题解
  • 原文地址:https://www.cnblogs.com/oversea201405/p/3749581.html
Copyright © 2020-2023  润新知