Arrays.copyof
public static int[] copyOf(int[] original, int newLength) {
int[] copy = new int[newLength];
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
首先new一个新数组 然后copy过去 return这个新数组
int[] strArray = new int[] {1,2,3,4};
int[] copyArray=Arrays.copyOf(strArray,2);
结果copyArray就是1,2
int[] strArray = new int[] {1,2,3,4};
int[] copyArray=Arrays.copyOf(strArray,10);
结果1 2 3 4 0 0 0 0 0 0
不会报错 因为最后的数组总是按后面那个newLength规定的新数组来说
用System.arraycopy:
int[] strArray = new int[] {1,2,3,4};
int[] copyArray = new int[4];
System.arraycopy(strArray, 0, copyArray, 0, 5);
直接报错:java.lang.ArrayIndexOutOfBoundsException
如果把最后的5改成3
copyArray :1 2 3 0
定义一个数组int[] a={3,1,4,2,5}; int[] b=a; 数组b只是对数组a的又一个引用,即浅拷贝。如果改变数组b中元素的值,其实是改变了数组a的元素的值
要实现深度复制,可以用clone或者System.arrayCopy
如下面的代码
2 int[] b=a.clone();
3 b[0]=10;
4 System.out.println(b[0]+" "+a[0]);
输出为10 3
可见改变了b的值,但是没有改变a的元素的值
但是clone和System.arrayCopy都是对一维数组的深度复制。对于二维数组
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函数时,是对这两个引用进行了复制。
要证明,只需看下面的输出
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函数
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的更多相关文章
- System.arraycopy()和Arrays.copyOf()的区别
先看看System.arraycopy()的声明: public static native void arraycopy(Object src,int srcPos, Object dest, in ...
- Java-Java中System.arraycopy() 和 Arrays.copyOf()两者之间的区别
如果我们想拷贝一个数组,我们可能会使用System.arraycopy()或者Arrays.copyof()两种方式.在这里,我们将使用一个比较简单的示例来阐述两者之间的区别. 1.示例代码: Sys ...
- 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 ...
- 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, ...
- 数组复制的五种方式(遍历循环一一赋值、System.arraycopy、地址赋值、克隆clone()、Arrays.copyof())
package com.Summer_0424.cn; import java.util.Arrays; import java.util.concurrent.CopyOnWriteArrayLis ...
- Arrays.copyOf() 和 System.arrayCopy()分析
java数组的拷贝四种方法:for.clone.System.arraycopy.Arrays.copyof public class Test1 { public static void main( ...
- Java中 System.arraycopy() 和 Arrays.copyOf()方法
System.arraycopy() 和 Arrays.copyOf()方法 阅读源码的话,我们就会发现 ArrayList 中大量调用了这两个方法.比如:我们上面讲的扩容操作以及add(int in ...
- System.arraycopy和arrays.copyOf
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length); 这 ...
- java.lang.System.arraycopy() 与java.util.Arrays.copyOf()的区别
java.lang.System.arraycopy() 与java.util.Arrays.copyOf()的区别 一.java.lang.System.arraycopy() 该方法的声明: /* ...
随机推荐
- FlexSlider是一个非常出色的jQuery滑动切换插件
FlexSlider是一个非常出色的jQuery滑动切换插件,它支持所有主流浏览器,并有淡入淡出效果.适合所有初级和高级网页设计师使用.不过很多人都只是使用默认的参数,今天来说说具体的参数来给大家看看 ...
- bootstrap 常用类名
一. 常用类1.container居中的内容展示2.row 行内容显示3.col 列内容显示, 列必须在row 中xs 宽度小于768 ,sm宽度小于990 大于768 ,md 宽度大于990,小于 ...
- PL/SQL文档
http://www.oracle.com/technetwork/database/features/plsql/index.html 注册表学习 http://itlab.idcquan.com/ ...
- PHP学习笔记三十三【自定义错误处理器】
<?php //自定义错误处理器 //$errorno 错误号 //$errmes错误信息 //这两个参数是必须的 function my_error($errorno,$errmes) { e ...
- 写个点击input框 下方弹出月份时间等
<input type="text" name="test" id="test" value="" "& ...
- iOS开发-单例模式的解读
现在网上的有很多人写单例模式,一个很基本的东西但是版本也有很多,新人看了难免有些眼花缭乱的感觉.自己最新比较闲,也过来写一些自己的心得. 在往下看之前,我们要明白一点,那就是在什么情况下我们才要用到单 ...
- UVALive 4123 Glenbow Museum (组合数学)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 易得,当n为奇数或者n<3时,答案为0,否则该序列中必定有(n+4)/2个R ...
- UVALive3516Exploring Pyramids(dp)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 题目意思:有一棵多叉树,每个结点的子节点有左右之分(即要按照顺序查找),从跟结点开 ...
- jquery 单选框整个选中
问题:遇到单选框,如图 解决办法:利用jqurey click->checked <!DOCTYPE html> <html lang="en"> & ...
- 用Python设计第一个游戏 - 零基础入门学习Python002
用Python设计第一个游戏 让编程改变世界 Change the world by program 有些鱼油可能会说,哇,小甲鱼你开玩笑呐!这这这这就上游戏啦?你不打算给我们讲讲变量,分支,循环,条 ...