这里介绍两种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. WinForm开发框架--动态读取DLL模式

    1\ WinForm开发框架--动态读取DLL模式   http://www.2cto.com/kf/201306/217199.html 2\ 广州爱奇迪     http://www.iqidi. ...

  2. POJ 3414

    http://poj.org/problem?id=3414 这是一个广搜的题目,不难,就是有些许麻烦.对于练习还是个不错的题目. 题意就是给你两个杯子,这两个杯子的容量分别为a和b,要你通过一些操作 ...

  3. svn上传报Authorization failed错误解决办法

    svn上传文件时没有弹出用户登录界面,而是直接报Authorization failed错误.出现该问题基本都是三个配置文件的问题,下面把这个文件列出来 svnserve.conf配置文件中的 [ge ...

  4. HDU 4870 Rating(概率、期望、推公式) && ZOJ 3415 Zhou Yu

    其实zoj 3415不是应该叫Yu Zhou吗...碰到ZOJ 3415之后用了第二个参考网址的方法去求通项,然后这次碰到4870不会搞.参考了chanme的,然后重新把周瑜跟排名都反复推导(不是推倒 ...

  5. Js 旋转木马 轮播

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. C# 总复习

    1.循环语句 四要素:初始条件.循环条件.循环体.状态改变 循环的最后一句:循环条件不再满足 2. ++     --int a = 5; //在赋值语句中,后++需要,先进性赋值,然后进行+1运算 ...

  7. JavaScript for循环里边异步操作问题。

    问题:(DRIVING.search是异步操作) for循环中做异步操作会导致aDistances数组里边的数据全部都是从A_SHOP_INFO数组中最后一条数据获取的值. var iIdx = 0; ...

  8. 你缺什么(codevs 2928)

    2928 你缺什么  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 继“你幸福吗”之后,央视又推出了“ ...

  9. 三、jQuery--jQuery基础--jQuery基础课程--第4章 jQuery表单选择器

    1.:input表单选择器 如何获取表单全部元素?:input表单选择器可以实现,它的功能是返回全部的表单元素,不仅包括所有<input>标记的表单元素,而且还包括<textarea ...

  10. C#调用C++DLL的小总结5---和C++的DLL的联合调试

    http://fpcfjf.blog.163.com/blog/static/5546979320134922938373/ http://blog.csdn.net/jiangxinyu/artic ...