System提供了一个静态方法arraycopy(),我们可以使用它来实现数组之间的复制。其函数原型是: public static void arraycopy(Object src,
int srcPos,
Object dest,
int destPos,
int length) src:源数组; srcPos:源数组要复制的起始位置; dest:目的数组; destPos:目的数组放置的起始位置; length:复制的长度。 注意:src and dest都必须是同类型或者可以进行转换类型的数组. 有趣的是这个函数可以实现自己到自己复制,比如: int[] fun ={0,1,2,3,4,5,6}; System.arraycopy(fun,0,fun,3,3); 则结果为:{0,1,2,0,1,2,6}; 实现过程是这样的,先生成一个长度为length的临时数组,将fun数组中srcPos 到srcPos+length-1之间的数据拷贝到临时数组中,再执行System.arraycopy(临时数组,0,fun,3,3).
分享到:
相关推荐
System提供了一个静态方法arraycopy(),我们可以使用它来实现数组之间的复制。
使用System.arraycopy()实现数组之间的复制
个人研究所得,包含对其内部jdk源码的分析。 同时会结合ArrayList中对该两个方法的调用做进一步说明。...总结一句话:在允许的情况下,尽量调用System.arraycopy方法,实在不行再调用Arrays.copyOf方法。
System.arraycopy(writeStringToBytes, i*tempLength, temp, 0, writeStringToBytes.length%tempLength); ops.write(new String(temp,"GBK").trim().getBytes(),0,writeStringToBytes.length%tempLength);...
System.arraycopy(ia,0,ib,0,3); // 复制源数组中从下标0开始的3个元素到目的数组,从下标0的位置开始存储。 for(int i=0;i<ia.length;i++) System.out.print(ia[i]); System.out.println(); for(int j=0;...
/** * 动态调整数组的长度 */ public class AdjustArrayLength { private static int DEFAULT_LENGTH = 10; public static Integer... System.arraycopy(src, 0, result, 0, src.length); return result; }
System.arraycopy(arrays[i], 0, result, currentIndex, arrays[i].length); currentIndex += arrays[i].length; } return result; } public static byte[] getCurrentDateAsByteArray( String sFormat ) { ...
System.arraycopy(num, 0, ch, ch.length-num.length, num.length); String ball = new String(ch); red[i] = ball; } String[] blue = ("01,02,03,04,05,06,07," + "08,09,10,11,12,13,14,15,16").split(",...
数组拷贝,建议使用System.arraycopy()速度更快,把source数据内容拷贝到destination中使用Object则支持多种对象数组;去除数组中的重复元素;四舍五入;判断字符串是否是小数;半角数字的unicode范围;取得ascii码;
● 复制数组:使用System.arraycopy()方法或Arrays.copyOf()方法将一个数组复制到另一个数组中。 ● 排序数组:使用Arrays.sort()方法对数组进行排序。 ● 搜索数组:使用Arrays.binarySearch()方法在一个已排序的...
目录一、深拷贝与浅拷贝解析浅拷贝深拷贝二、数组拷贝的方式1.for循环来拷贝2.System.arraycopy( )拷贝3.Arrays.copyOf( )拷贝4.clone( )拷贝5.解释三、四种拷贝方式效率比较1. System.arraycopy( )2.Arrays.copyOf...
编写一个类MyArray,包含一个复制数组的方法,功能与System.arraycopy()相同。请使用泛型改写MyArray类实现数组复制. 请编写程序。首先在程序中定义一个枚举类型,它含有7个枚举常量,分别表示一个星期每一天的名称...
90行Java代码实现小巧俄罗斯方块~~ 66行改为: System.arraycopy(matrix[j-1],0,matrix[j],0,10); 修改的一个Bug 最新版本在:http://wireless.javaeye.com/blog/595321
基本数据类型的包装类 •八大数据类型的包装类分别为:Byte、Short、Integer、Long、Character、 Float、Double、Boolean。 把基本数据类型变量包装类实例是通过对应包装类的构造器来实现的,不仅如此,8个...
数组的复制:System.arraycopy(src, srcPos, dest, destPos, length); Arrays.copyOf(original, newLength); 数组的排序:排序方法和排序分类 Arrays.sort(arr);//对arr进行升序排列 2.方法(函数、过程): ...
System.arraycopy(blockdb, 0, tmpdb, 0, BlockSize);// raf.seek( (util.getBlockSize() + 4) * BlockIndex); //(+4)是用于存放文件大小信息的 //文件块写入临时文件中将相应数据块放到相应的位置中去 raf....
Java数组扩容的原理 1)Java数组对象的大小是固定... 3)System.arraycopy()可以复制数组。 4)Arrays.copyOf()可以简便的创建数组副本。 5)创建数组副本的同时将数组长度增加就变通的实现了数组的扩容。
System.arraycopy方法4. Arrays.copyOfRange方法 1. 直接赋值 在java中,我们可以将一个数组变量直接拷贝给另一个数组变量。但拷贝后,两个数组变量引用的是同一个数组。若改变其中一个数组中的内容,另一个数组也...
import java .util.*; public class SystemTest { public static void ... System.out.println(System.arraycopy); int[] dst = new int[src.length]; System.arraycopy(src, 0, dst
b) System.arraycopy()方法 c) Arrays.copyOf()方法 d) 使用clone()方法 e) Arrays.copyOfRange()方法 接下来,我们看下这几种方法的效率和源码,以及如何使用序列化和反序列化实现组合类的深复制。 我们以百万级和...