System.arraycopy 和 Arrays.copyOf
System.arraycopy
/*
native关键字 本地方法 System类 java.lang.System.class
参数说明:
src - 源数组。
srcPos - 源数组中的起始位置。
dest - 目标数组。
destPos - 目标数据中的起始位置。
length - 要复制的数组元素的数量。
*/
public static native void arraycopy(Object src, int srcPos,Object dest, int destPos,int length);
Arrays.copyOf();该方法对于不同的数据类型都有相应的方法重载
/*
original - 要复制的数组
newLength - 要返回的副本的长度
newType - 要返回的副本的类型
*/
//基本数据类型
public static int[] copyOf(int[] original, int newLength)
//复杂数据类型 由U类型复制为T类型
public static <T,U> T[] copyOf(U[] original, int newLength, Class<?extends T[]> newType)
Arrays.copyOfRange()方法
/*
original 要复制的数组
from初始索引
to最终索引
newType 要返回的副本类型
*/
//基本类型 可以使short、int、byte.....
public static <T> T[] copyOfRange(T[] original, int from, int to)
{
return copyOfRange(original, from, to, (Class<T[]>)original.getClass());
}
//复杂类型 由U类型转为T类型
public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType)
代码:
System.arraycopy
int a[]={0,1,2,3,4,5,6,7,8,9,11};
int ab[] = new int[5];
System.arraycopy(a, 0, ab, 0, 5);
for (int i : ab) {
System.out.println(i);
}
Arrays.copyOf 基本数据类型
int a[]={0,1,2,3};
//original a[] newLength 新数组长度 如果大于老数组长度数组元素为0
int c[] = Arrays.copyOf(a, 5);
for (int i : c) {
System.out.println(i);
}
Arrays.copyOf 复杂数据类型
// Short 数组
Short shortArr[] = new Short[]{5, 2, 15, 52, 10};
// copy Short 数组 返回 Number[]数组
Number[] arr2 = Arrays.copyOf(shortArr, 5, Number[].class);
//遍历Number[]
System.out.println("arr2 数组值:");
for (Number number : arr2) {
System.out.println("Number = " + number);
}
Arrays.copyOfRange
int a[]={0,1,2,3,4,5};
//original a[]数组 from初始索引 to最终索引
int ab[] = Arrays.copyOfRange(a, 0, 8);
for (int i : ab) {
System.out.println(i);
}
复制数组: Arrays.copy 实现通过System.arraycopy完成
System.arraycopy 和 Arrays.copyOf的更多相关文章
- Java-Java中System.arraycopy() 和 Arrays.copyOf()两者之间的区别
如果我们想拷贝一个数组,我们可能会使用System.arraycopy()或者Arrays.copyof()两种方式.在这里,我们将使用一个比较简单的示例来阐述两者之间的区别. 1.示例代码: Sys ...
- Java中 System.arraycopy() 和 Arrays.copyOf()方法
System.arraycopy() 和 Arrays.copyOf()方法 阅读源码的话,我们就会发现 ArrayList 中大量调用了这两个方法.比如:我们上面讲的扩容操作以及add(int in ...
- 论java中System.arrayCopy()与Arrays.copyOf()的区别
如果我们想拷贝一个数组,我们可能会使用System.arraycopy()或者Arrays.copyof()两种方式.在这里,我们将使用一个比较简单的示例来阐述两者之间的区别. 首先先说System. ...
- System.arraycopy()和Arrays.copyOf()的区别
先看看System.arraycopy()的声明: public static native void arraycopy(Object src,int srcPos, Object dest, in ...
- java数组的拷贝四种方法:for、clone、System.arraycopy、arrays.copyof
public class ArrayCopy{ public static void main(String []args){ int []a = {1,3,4,5}; toPrint(a); int ...
- System.arraycopy和arrays.copyOf
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length); 这 ...
- 再谈System.arraycopy和Arrays.copyOf
之前转载过一篇博文,介绍过这两个方法,今天想要再次详细的了解一下. public static native void arraycopy(Object src, int srcPos, Object ...
- JAVA System.arraycopy 和Arrays.copyof 效率比较
System.arraycopy()源码.可以看到是native方法: native关键字说明其修饰的方法是一个原生态方法,方法对应的实现不是在当前文件,而是在用其他语言(如C和C++)实现的文件中. ...
- Arrays.copyOf() 和 System.arrayCopy()分析
java数组的拷贝四种方法:for.clone.System.arraycopy.Arrays.copyof public class Test1 { public static void main( ...
随机推荐
- C#并发集合
并发集合 并发集合 1 为什么使用并发集合? 原因主要有以下几点: System.Collections和System.Collections.Generic名称空间中所提供的经典列表.集合和数组 ...
- DDD实战4 实现产品仓储
a.要实现仓储,首先要定义仓储接口.在领域层定义仓储接口,IProductRepository.cs. public interface IProductRepository { void Creat ...
- 利用WPF的ListView进行大数据量异步加载
原文:利用WPF的ListView进行大数据量异步加载 由于之前利用Winform的ListView进行大数据量加载的时候,诟病良多,所以今天试着用WPF的ListView来做了一下,结果没有让我失望 ...
- windows 7 SDK和DDK下载
检查小数据,获取地址.顺便记录下来. Windows Driver Kit Version 7.1.0 http://www.microsoft.com/downloads/details.aspx? ...
- .net reactor 学习系列(四)---.net reactor应用场景
原文:.net reactor 学习系列(四)---.net reactor应用场景 前面已经学习了.net reactor一些基础知识,现在准备学习下实际的应用场景,只是简单的保护和 ...
- WPF与缓动(一) N次缓动
原文:WPF与缓动(一) N次缓动 WPF与缓动(一) N次缓动 ...
- OpenWrt 主的发展版本号trunk MT7620N 无线驱动程序bug
周边环境: OpenWrt Development Trunk: svn co svn://svn.openwrt.org/openwrt/trunk/ BUG: 1. 无线无法建立连接. 2. 无线 ...
- eXtremeDB -- the shared memory 80error
The customers got the shared memory 80error on AIX environment; utility truss is used to track the d ...
- WPF刷新界面
Winform 里有 Application.DoEvents();可刷新! WPF 里没这个,尽管可用委托实现多线程,但是刷新还是不行! 后来找到了 类似App.DoEvents()的方法(): 代 ...
- MEF 插件式开发 - WPF 初体验
原文:MEF 插件式开发 - WPF 初体验 目录 MEF 在 WPF 中的简单应用 加载插件 获取元数据 依赖注入 总结 MEF 在 WPF 中的简单应用 MEF 的开发模式主要适用于插件化的业务场 ...