这里介绍两种java提供的数组拷贝方法:

(1)Arrays提供的copyOf(T src, T desLength)和copyOfRange(T src, int from, int to)

(2)System.arraycopy(T src, int from, T des, int from, int length)

使用例子:

(1)

int [] a = {1,2,3,4,5,6};
int [] b = Arrays.copyOf(a, 8); //把a拷贝到长度为8的b中。b的长度可比a长,长的部分补0;可比a短,把a截断。
int [] c = Arrays.copyOfRange(a, 2, 5); //把a的数组下标为2的到下标为5的前一个,拷贝到c中,c的长度为5-2=3。

(2)

int [] a = {1,2,3,4,5,6};
int [] b = new int[8];
System.arraycopy(a, 2, b, 3, 4); //把数组a从下标为2的元素开始,拷贝长度为4的元素到b中,从b的下标为3的位置开始放。
                //如果b的位置不足以放下指定长度的元素,会报java.lang.ArrayIndexOutOfBoundsException异常

在方法(2)中,如果目标数组和源数组相同,则先拷贝到一个temp数组中,再从temp数组中拷回目标数组(源)。

当数组是对象数组的时候,上面的两种方法都不能实现深度拷贝。例如:源代码中的

Lesson9_arrayCopyTest.deepArrayCopy()

详细代码如下:

package javaBase16Lesson;

import java.util.Arrays;

/**
* (1)
* int [] a = {1,2,3,4,5,6};
* int [] b = Arrays.copyOf(a, 8); //把a拷贝到长度为8的b中。b的长度可比a长,长的部分补0;可比a短,把a截断。
* int [] c = Arrays.copyOfRange(a, 2, 5); //把a的数组下标为2的到下标为5的前一个,拷贝到c中,c的长度为5-2=3。
*
*(2)
* int [] a = {1,2,3,4,5,6};
* int [] b = new int[8];
* System.arraycopy(a, 2, b, 3, 4); //把数组a从下标为2的元素开始,拷贝长度为4的元素到b中,从b的下标为3的位置开始放。
* //如果b的位置不足以放下指定长度的元素,会报java.lang.ArrayIndexOutOfBoundsException异常
*
*(3)
*方法(1)(2)都只是数组的浅拷贝,无法实现深度拷贝
* @author cnx
* @Description : arrayCopyTest
* @CreateDate ; 2014年7月9日 下午6:43:26
*/
public class Lesson9_arrayCopyTest {
public static void main(String[] args) {
// Lesson9_arrayCopyTest.arraysCopyOf();
// Lesson9_arrayCopyTest.systemArraycopy();
Lesson9_arrayCopyTest.deepArrayCopy();
} public static void arraysCopyOf(){
int [] a = {1,2,3,4,5,6};
int [] b = Arrays.copyOf(a, 8); //把a拷贝到长度为8的b中。b的长度可比a长,长的部分补0;可比a短,把a截断。
int [] c = Arrays.copyOfRange(a, 2, 5); //把a的数组下标为2的到下标为5的前一个,拷贝到c中,c的长度为5-2=3。
System.out.println("a:"+ a.length);
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
System.out.println("b:"+ b.length);
for (int i : b) {
System.out.println(i);
}
System.out.println("c:"+ c.length);
for (int i : c) {
System.out.println(i);
}
}
/**
* Copies an array from the specified source array, beginning at the specified position,
* to the specified position of the destination array. A subsequence of array components
* are copied from the source array referenced by src to the destination array referenced by dest.
* The number of components copied is equal to the length argument. The components at positions
* srcPos through srcPos+length-1 in the source array are copied into positions destPos through
* destPos+length-1, respectively, of the destination array.
* If the src and dest arguments refer to the same array object, then the copying is performed
* as if the components at positions srcPos through srcPos+length-1 were first copied to a temporary
* array with length components and then the contents of the temporary array were copied into
* positions destPos through destPos+length-1 of the destination array. <br>
* 如果目标数组和源数组相同,则先拷贝到一个temp数组中,再从temp数组中拷回目标数组(源)
*/
public static void systemArraycopy(){
int [] a = {1,2,3,4,5,6};
int [] b = new int[8];
System.arraycopy(a, 2, b, 3, 4); //把数组a从下标为2的元素开始,拷贝长度为4的元素到b中,从b的下标为3的位置开始放。
//如果b的位置不足以放下指定长度的元素,会报java.lang.ArrayIndexOutOfBoundsException异常
System.out.println("a:"+ a.length);
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
System.out.println("b:"+ b.length);
for (int i : b) {
System.out.println(i);
}
} /**
* 上面的两种方法都无法实现深度拷贝
*/
public static void deepArrayCopy(){
Lesson9_array1 [] a1 = {new Lesson9_array1(),new Lesson9_array1()};
Lesson9_array1 [] a2 = null;
Lesson9_array1 [] a3 = new Lesson9_array1[3];
Lesson9_array2 [] b1 = {new Lesson9_array2(),new Lesson9_array2()};
Lesson9_array2 [] b2 = null; a2 = Arrays.copyOf(a1, 3);
System.arraycopy(a1, 0, a3, 0, 2);
b2 = Arrays.copyOf(b1, 3); a1[1].a = "a1";
System.out.println(a2[1].a);
System.out.println(a3[1].a);
b1[1].a = "b1";
System.out.println(b2[1].a);
}
} class Lesson9_array1 implements Cloneable{
public String a = "deep"; public Object clone(){
Object o = null;
try {
o = (Lesson9_array1)super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return o;
} public String toString(){
return a;
}
} class Lesson9_array2{
public String a = "deep"; public String toString(){
return a;
}
}

java杂记——数组拷贝的更多相关文章

  1. Java基础知识强化85:System类之arraycopy()方法(数组拷贝)

    1. arraycopy方法(数组拷贝) public static void arraycopy(object src,int srcPos,Object dest,int destPos, int ...

  2. Java 数组拷贝方法 System.arraycopy

    System类提供的数组拷贝方法: public static native void arraycopy(Object src, int srcPos, Object dest, int destP ...

  3. Java数组拷贝的五种方法

    在Java中有多种方法可以拷贝一个数组,到另外一个数组. 1.循环拷贝 在循环拷贝方法中,只需要利用i,移动指针即可复制所有数组到arrayB中. for(int i=0;i<arrayA.le ...

  4. java中的拷贝文件FileChannel

    以前用Java拷贝文件,只知道写byte数组循环拷贝,今天知道了可以用FileChannel进行拷贝,上代码: 下边是传统的byte数组拷贝方法 </pre><pre name=&q ...

  5. Java实现文件拷贝的4种方法.

    原文地址:http://blog.csdn.net/ta8210/article/details/2073817 使用 java 进行文件拷贝 相信很多人都会用,,不过效率上是否最好呢? 最近看了看N ...

  6. java:数组操作工具类 java.util.Arrays包 主要方法详解

    Arrays类位于Java.util包下,是一个对数组操作的工具类,现将Arrays类中的方法做一个总结(JDK版本:1.6.0_34).Arrays类中的方法可以分为八类: sort(对数组排序) ...

  7. Java中数组的插入,删除,扩张

    Java中数组是不可变的,但是可以通过本地的arraycop来进行数组的插入,删除,扩张.实际上数组是没变的,只是把原来的数组拷贝到了另一个数组,看起来像是改变了. 语法: System.arrayc ...

  8. java笔记 -- 数组

    概念: 数组是一种数据结构, 用来存储同一类型值的集合. 通过一个整型的下标可以访问数组中的每一个值. 声明: int[] a(推荐,将类型int[](整形数组)和变量名分开了) 或者int a[] ...

  9. java去除数组重复元素的方法

    转载自:https://blog.csdn.net/Solar24/article/details/78672500 import java.util.ArrayList; import java.u ...

随机推荐

  1. 左侧导航栏复制粘贴保存html即可

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. Mac下DIY文件浏览器

    2015-07-14 15:07:53 Mac下的finder不能浏览Linux文件目录, 一些优秀的资源管理器是收费的..... 于是想到了既然Mac的本质是类Unix, 而在windows下查看L ...

  3. codeforces 556B. Case of Fake Numbers 解题报告

    题目链接:http://codeforces.com/problemset/problem/556/B 题目意思:给出 n 个齿轮,每个齿轮有 n 个 teeth,逆时针排列,编号为0 ~ n-1.每 ...

  4. JavaScript高级程序设计学习笔记--函数表达式

    关于函数声明,它的一个重要特征就是函数声明提升,意思是在执行代码之间会读取函数声明,意思是在执行代码之前会先读取函数声明.这就意味着可以把函数声明放在调用它的语句 后面. sayHi(); funct ...

  5. 【XLL 文档翻译】【第2部分】C API 回调函数 Excel4, Excel12

    Excel4 和 Excel12 函数使得 DLL 可以调用 Excel 工作表函数.宏表函数.命令.XLL特定函数或命令.最近的一些 Excel 版本都支持 Excel12 函数.这两个函数支持下面 ...

  6. GCD的使用

    什么是 GCD Grand Central Dispatch (GCD) 是 Apple 开发的一个多核编程的解决方法.该方法在 Mac OS X 10.6 雪豹中首次推出,并随后被引入到了 iOS4 ...

  7. CSS语法小记

    一.CSS语法结构 语法:选择符{属性:值} 例如:body{font-size:12px;} 参数说明: 1.选择符(Selector):指明这组样式所要针对的对象.可以是一个XHTML标签,例如h ...

  8. [Android Pro] DES加密 version1

    reference to : http://blog.csdn.net/wfung_kwok/article/details/7766029 加密和解密要用同一個key AES: import jav ...

  9. ViewPager部分源码分析二:FragmentManager对Fragment状态的管理完成ViewPager的child添加或移出

    ViewPager维护child代码流程: 注:PagerAdapter 使用的是FragmentPagerAdapter类或者它的子类. viewPager.populate(): void pop ...

  10. 查看centos的版本

    [root@NB Desktop]# lsb_release -a LSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4 ...