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( ...
随机推荐
- IT引导学生成长的文章链接(十二)
链接:IT学子成长指导类文章链接(1)(2)(3) (4) (5)(6)(7)(8)(9)(10)(11) "IT学子成长指导"类我收藏过的好文(十二期:至2014年4月26日) ...
- C#常用多线程方法
1. Thread类 C#多线程编程中Thread类需要包含名称空间System.Threading. class Program { static void Main(string[] args) ...
- Unity3d基础组件 (Component) 和脚本 (Script) 关系
原版的:http://edu.china.unity3d.com/learning_document/getData?file=/Manual/TheComponent-ScriptRelations ...
- 嵌入式开发(*(volatile unsigned long *)) 认识
一个.说明 (*(volatile unsigned long *)) 这个语句对于不同的计算机体系结构,设备可能是port映射,也可能是内存映射的. 假设系统结构支持独立的IO地址空间.而且是por ...
- Git配置用户名、邮箱、密码
配置用户名:username git config --global user.name username 配置邮箱:user@email git config --global user.email ...
- 【Quartz】定时器初步实验(一)
原文:[Quartz]定时器初步实验(一) 以前就了解了Quartz这个定时框架,但是一直没有认真的去关注他,最近忽然看到已经更新到3.0.4支持异步操作了所以就写个简单的小例子看看好用不. ...
- MQTT协议学习及实践(Linux服务端,Android客户端的例子)
前言 MQTT(Message Queuing Telemetry Transport),是一个物联网传输协议,它被设计用于轻量级的发布/订阅式消息传输,旨在为低带宽和不稳定的网络环境中的物联网设备提 ...
- WPF 界面实现多语言支持 中英文切换 动态加载资源字典
1.使用资源字典,首先新建两个字典文件en-us.xaml.zh-cn.xaml.定义中英文的字符串在这里面[注意:添加xmlns:s="clr-namespace:System;assem ...
- XF 主从页面
using System; using Xamarin.Forms; using Xamarin.Forms.Xaml; [assembly: XamlCompilation (XamlCompila ...
- zendframework 初始化配置
https://framework.zend.com/manual/2.4/en/tutorials/config.advanced.html#environment-specific-system- ...