1. public static int[] copyOf(int[] original, int newLength) {
  2. int[] copy = new int[newLength];
  3. System.arraycopy(original, 0, copy, 0,
  4. Math.min(original.length, newLength));
  5. return copy;
  6. }

首先new一个新数组 然后copy过去 return这个新数组

  1. int[] strArray = new int[] {1,2,3,4};
  2. int[] copyArray=Arrays.copyOf(strArray,2);

结果copyArray就是1,2

  1. int[] strArray = new int[] {1,2,3,4};
  2. int[] copyArray=Arrays.copyOf(strArray,10);

结果1 2 3 4 0 0 0 0 0 0

不会报错 因为最后的数组总是按后面那个newLength规定的新数组来说

用System.arraycopy:

  1. int[] strArray = new int[] {1,2,3,4};
  2. int[] copyArray = new int[4];
  3. System.arraycopy(strArray, 0, copyArray, 0, 5);

直接报错:java.lang.ArrayIndexOutOfBoundsException

如果把最后的5改成3

  1. copyArray :1 2 3 0 

定义一个数组int[] a={3,1,4,2,5}; int[] b=a;  数组b只是对数组a的又一个引用,即浅拷贝。如果改变数组b中元素的值,其实是改变了数组a的元素的值

要实现深度复制,可以用clone或者System.arrayCopy
如下面的代码

1 int[] a={3,1,4,2,5};
2 int[] b=a.clone();
3 b[0]=10;
4 System.out.println(b[0]+"  "+a[0]);

输出为10  3
可见改变了b的值,但是没有改变a的元素的值

但是clone和System.arrayCopy都是对一维数组的深度复制。对于二维数组

1 int[][] a={{3,1,4,2,5},{4,2}};
2 int[][] b=a.clone();
3 b[0][0]=10;
4 System.out.println(b[0][0]+"  "+a[0][0]);

输出为10  10
所以clone并不能直接作用于二维数组
因为java中没有二维数组的概念,只有数组的数组。所以二维数组a中存储的实际上是两个一维数组的引用。当调用clone函数时,是对这两个引用进行了复制。
要证明,只需看下面的输出

1 int[][] a={{3,1,4,2,5},{4,2}};
2 int[][] b=a.clone();
3 b[0][0]=10;
4 System.out.println(b[0][0]+"  "+a[0][0]);
5 System.out.println(a[0]==b[0]);

第5句输出为true。

用clone对二维数组进行复制,要在每一维上调用clone函数

1 int[][] a={{3,1,4,2,5},{4,2}};
2 int[][] b=new int[a.length][];
3 for(int i=0;i<a.length;i++){
        b[i]=a[i].clone();
}
 b[0][0]=10;
6 System.out.println(b[0][0]+"  "+a[0][0]);
7 System.out.println(b[0]==a[0]);

输出为
10  3
false

Arrays.copyof的更多相关文章

  1. System.arraycopy()和Arrays.copyOf()的区别

    先看看System.arraycopy()的声明: public static native void arraycopy(Object src,int srcPos, Object dest, in ...

  2. Java-Java中System.arraycopy() 和 Arrays.copyOf()两者之间的区别

    如果我们想拷贝一个数组,我们可能会使用System.arraycopy()或者Arrays.copyof()两种方式.在这里,我们将使用一个比较简单的示例来阐述两者之间的区别. 1.示例代码: Sys ...

  3. java数组的拷贝四种方法:for、clone、System.arraycopy、arrays.copyof

    public class ArrayCopy{ public static void main(String []args){ int []a = {1,3,4,5}; toPrint(a); int ...

  4. System.arraycopy(src, srcPos, dest, destPos, length) 与 Arrays.copyOf(original, newLength)区别

    //System.arraycopy,只拷贝已存在的数组元素 int[] src = {0, 1, 2}; int[] dest = new int[3]; System.arraycopy(src, ...

  5. 数组复制的五种方式(遍历循环一一赋值、System.arraycopy、地址赋值、克隆clone()、Arrays.copyof())

    package com.Summer_0424.cn; import java.util.Arrays; import java.util.concurrent.CopyOnWriteArrayLis ...

  6. Arrays.copyOf() 和 System.arrayCopy()分析

    java数组的拷贝四种方法:for.clone.System.arraycopy.Arrays.copyof public class Test1 { public static void main( ...

  7. Java中 System.arraycopy() 和 Arrays.copyOf()方法

    System.arraycopy() 和 Arrays.copyOf()方法 阅读源码的话,我们就会发现 ArrayList 中大量调用了这两个方法.比如:我们上面讲的扩容操作以及add(int in ...

  8. System.arraycopy和arrays.copyOf

    public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length); 这 ...

  9. java.lang.System.arraycopy() 与java.util.Arrays.copyOf()的区别

    java.lang.System.arraycopy() 与java.util.Arrays.copyOf()的区别 一.java.lang.System.arraycopy() 该方法的声明: /* ...

随机推荐

  1. Android WebView 软键盘挡住输入框

    解决方法一: 在所在的Activity中加入 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RES ...

  2. servlet乱码以及解决

    //  浏览器提交的数据是000110011(码表中对应的<编码> )等东西. // 浏览器以什么<码表>打开浏览器(而空中浏览器使用的编码是:<meta http-eq ...

  3. MVC View基础(转)

    View主要用于呈现数据.由于Controller和相关的Service已经处理完业务逻辑并将结果打包成model实体,View只需要怎么去获得model并将其转为Html 1选择需要渲染的视图 在上 ...

  4. 【SQL语句】 - Ctrl+3 查询表属性的存储过程 [sp_select_talberowName]

    /**-- ============================================= Author: xftCteateDate: 2013-10-11Description:查看表 ...

  5. latex 模版

    texlive2012 通过 \documentclass[a4paper]{article} %\documentclass[twocolumn]{article}   %\usepackage{g ...

  6. MySQL 初学笔记 ① -- MySQL用户登录权限控制

    1. MySQL 登录 MySQL -u username -p 2. MySQL 创建用户 use mysql //进入mysql 表 INSERT INTO user (Host,User,Pas ...

  7. java读取xml(当xml放在包里时)

  8. php error_log 详解

    定义和用法 error_log() 函数向服务器错误记录.文件或远程目标发送一个错误. 成功,返回 true,否则返回 false. error_log(error,type,destination, ...

  9. Expedition(优先队列)

    Expedition 点我 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9465   Accepted: 2760 Des ...

  10. BZOJ 2741 【FOTILE模拟赛】L(可持久化trie)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2741 思路:我们先将a变成a的异或前缀,这样问题就变成了,在l-1到r区间内,找出i,j令a[i]^ ...