java一维数组的深拷贝方式是什么
作者:慢热型
时间:2023-04-30
浏览:1
1、三种方法(1)调用clone(2)调用System.arraycopy以上两种对基本类型和对象类型数据效果等同。(3)使用FOR循环,将数组的每个元素复制。(注意调用clone方法)2、实例Object[]src=newObject[]{newString("Zhao"),Integer.valueOf(1),Integer.valueOf(2),Integer.valueOf(3),Integer.valueOf(4)};Object[]dest=src.clone();//1.拷贝数据//Obje
1、三种方法
(1)调用clone
(2)调用System.arraycopy
以上两种对基本类型和对象类型数据效果等同。
(3)使用FOR循环,将数组的每个元素复制。(注意调用clone方法)
2、实例
Object[] src = new Object[]{ new String("Zhao"),
Integer.valueOf(1),
Integer.valueOf(2),
Integer.valueOf(3),
Integer.valueOf(4)};
Object[] dest = src.clone(); //1.拷贝数据
// Object[] dest = new Object[5];
// System.arraycopy(src, 0, dest, 0, dest.length);
System.out.println( dest.equals(src));
System.out.println( dest == src );
for (int i = 0; i < dest.length; i++) {
System.out.print( dest[i]+", " );
dest[i] = new String("KE"); //2.改变新数组内容
System.out.print( dest[i]+", " );
System.out.println( src[i]+","); //3.不影响原始数组
}
System.out.println();注意: 一维数组下的深拷贝在多维数组只是浅拷贝。
作者最新文章
win10笔记本外出联网如何阻止自动下载更新包
2026-10-06 11:33
photoshop智能对象不能直接编辑
2026-10-05 13:52
3dmax动画制作教程视频零基础入门步骤讲解
2026-09-23 12:06
CPU硅脂涂抹方法图解及正确操作步骤
2026-09-22 15:23
网易2026年Q2财报:营收301亿元,游戏收入增10%,三款重点新游披露进展
2026-09-08 17:51
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多










































