java杂记——数组拷贝
这里介绍两种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杂记——数组拷贝的更多相关文章
- Java基础知识强化85:System类之arraycopy()方法(数组拷贝)
1. arraycopy方法(数组拷贝) public static void arraycopy(object src,int srcPos,Object dest,int destPos, int ...
- Java 数组拷贝方法 System.arraycopy
System类提供的数组拷贝方法: public static native void arraycopy(Object src, int srcPos, Object dest, int destP ...
- Java数组拷贝的五种方法
在Java中有多种方法可以拷贝一个数组,到另外一个数组. 1.循环拷贝 在循环拷贝方法中,只需要利用i,移动指针即可复制所有数组到arrayB中. for(int i=0;i<arrayA.le ...
- java中的拷贝文件FileChannel
以前用Java拷贝文件,只知道写byte数组循环拷贝,今天知道了可以用FileChannel进行拷贝,上代码: 下边是传统的byte数组拷贝方法 </pre><pre name=&q ...
- Java实现文件拷贝的4种方法.
原文地址:http://blog.csdn.net/ta8210/article/details/2073817 使用 java 进行文件拷贝 相信很多人都会用,,不过效率上是否最好呢? 最近看了看N ...
- java:数组操作工具类 java.util.Arrays包 主要方法详解
Arrays类位于Java.util包下,是一个对数组操作的工具类,现将Arrays类中的方法做一个总结(JDK版本:1.6.0_34).Arrays类中的方法可以分为八类: sort(对数组排序) ...
- Java中数组的插入,删除,扩张
Java中数组是不可变的,但是可以通过本地的arraycop来进行数组的插入,删除,扩张.实际上数组是没变的,只是把原来的数组拷贝到了另一个数组,看起来像是改变了. 语法: System.arrayc ...
- java笔记 -- 数组
概念: 数组是一种数据结构, 用来存储同一类型值的集合. 通过一个整型的下标可以访问数组中的每一个值. 声明: int[] a(推荐,将类型int[](整形数组)和变量名分开了) 或者int a[] ...
- java去除数组重复元素的方法
转载自:https://blog.csdn.net/Solar24/article/details/78672500 import java.util.ArrayList; import java.u ...
随机推荐
- Call to undefined function mysql_connnect()
PHP Fatal error: Call to undefined function mysql_connnect() in /var/www/html/mysqltest.php on line ...
- ios Tabbar Item 的图标
1,tabBarItem图片的推荐尺寸和最大支持尺寸 下面是标签栏(UITabBar)中tab按钮图标分别在1x.2x.3x下不会压缩变形的尺寸: @1x : 推荐 25 x 25 (最大: 48 ...
- 9.nodejs权威指南--Socket.IO
1. Socket.IO 1.1 服务器 var http = require('http'); var sio = require('socket.io'); var fs = require('f ...
- Effective C++ -----条款06:若不想使用编译器自动生成的函数,就该明确拒绝
为驳回编译器自动提供的功能,可将相应的成员函数声明为private并且不予实现. 使用像Uncopyable这样的base class也是一种做法(即先声明一个基类,然后私有继承它).这其实有点像使用 ...
- Divide and conquer:Aggressive Cows(POJ 2456)
侵略性的牛 题目大意:C头牛最大化他们的最短距离 常规题,二分法即可 #include <iostream> #include <algorithm> #include < ...
- 【leetcode】Number of 1 Bits (easy)
做太多遍了,秒杀. class Solution { public: int hammingWeight(uint32_t n) { ; ), num++); return num; } };
- 【leetcode】atoi (hard) ★
虽然题目中说是easy, 但是我提交了10遍才过,就算hard吧. 主要是很多情况我都没有考虑到.并且有的时候我的规则和答案中的规则不同. 答案的规则: 1.前导空格全部跳过 “ 123” ...
- 【linux】配置SSH免密码登陆
1.输入ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa 其中''里的是登陆的密码,因为要设置免密码,所以''里是什么都没有的,空格也不行 2.输入 cat ~/.ss ...
- yii过滤器的原理和使用
过滤器官方页面:http://www.yiiframework.com/doc/guide/1.1/zh_cn/basics.controller 过滤器是一段代码,可被配置在控制器动作执行之前或之后 ...
- [Android Pro] CPU占用计算方法
1: AVTest CPU计算方法读取每个进程的 stat 文件 (/proc/<PID>/stat)计算采样间隔10min下utime的差值minusUtime,stime的差值min ...