这里介绍两种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. C#之常见数组编码错误

    摘抄自C#本质论(第四版,P55) 常见错误 错误描述 改正后的代码 int numbers[] 用于声明数组的方括号放在数据类型之后,而不是在变量标识符之后 int[] numbers; int[] ...

  2. python模块介绍- collections(5)-OrderedDict 有序字典

    1.3.5 OrderedDict 有序字典 OrderedDict是dict的子类,它记住了内容添加的顺序. import collections print 'Regular dictionary ...

  3. Unity3d 解析文本执行已注册的自定函数

    最近有个需求是想让程序解析策划编辑一个文本生成一段CG,内容使用大致是这样 cgSetCameraEx(118.6324,30.71189,75.55666,45,-45,0,0) cgCloneMy ...

  4. ASM:《X86汇编语言-从实模式到保护模式》1-4章:处理器,内存和硬盘基础

    其实很久之前就学完了实模式了,但是一直没有总结,感觉现在直接在书上做笔记的弊端就是有些知识点不能很很深刻地记下来(毕竟手写最明显的优点就是能深刻地记住知识,但是就是用太多的时间罢了).一下内容都是一些 ...

  5. git remote 相关用法

    为了便于管理,Git要求每个远程主机都必须指定一个主机名.git remote  命令就用于管理主机名. 不带选项的时候,git remote命令列出所有远程主机. $ git remote orig ...

  6. IOS - 控件的AutoresizingMask属性

    在 UIView 中有一个autoresizingMask的属性,它对应的是一个枚举的值(如下),属性的意思就是自动调整子控件与父控件中间的位置,宽高. enum {   UIViewAutoresi ...

  7. October 10th 2016 Week 42nd Monday

    What makes life dreary is the want of motive. 没有了目标,生活便黯然无光. Motive and goal, are absolutely indispe ...

  8. August 31st 2016 Week 36th Tuesday

    A friend without faults will never be found. 没有缺点的朋友是永远找不到的. You can't find a friends without faults ...

  9. UbuntuLinux安装java

    jdk1.7,jdk1.8详情,参见:http://www.cnblogs.com/a2211009/p/4265225.html

  10. android bitmap的放大缩小

    private static Bitmap big(Bitmap bitmap) { Matrix matrix = new Matrix(); matrix.postScale(1.5f,1.5f) ...