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( ...
随机推荐
- 机器学习: 基于MRF和CNN的图像合成
前面我们介绍了基于卷积神经网络的图像风格迁移,利用一张content image 和 style image,可以让最终的图像既保留content image的基本结构,又能显示一定的style im ...
- TBB、OpenCV混合编程
TBB提供了Parallel_for.Parallel_do.Parallel_reduce等通用并行算法,可以应用在不同的并行算法场合,Parallel_for适合用在多个数据或请求之间彼此没有依赖 ...
- 解析Android的 消息传递机制Handler
1. 什么是Handler: Handler 网络释义"机械手.经理"意思,在Android它用于管理多个线程UI操作: 2. 为什么会出现Handler: 在Android里面的 ...
- javascript自定义事件讲解
自定义事件 什么是自定义事件? 自定义事件:这要是跟函数有关系,就是让函数能够具备事件的某些特性 为什么要使用自定义事件? 有利于多人协作开发代码,一同开发不冲突 如何去挂载自定义事件与事件函数? 1 ...
- 熵、交叉熵、相对熵(KL 散度)意义及其关系
熵:H(p)=−∑xp(x)logp(x) 交叉熵:H(p,q)=−∑xp(x)logq(x) 相对熵:KL(p∥q)=−∑xp(x)logq(x)p(x) 相对熵(relative entropy) ...
- WPF实现抽屉效果
原文:WPF实现抽屉效果 界面代码(xaml): <Window x:Class="TransAnimation.MainWindow" xmlns="http:/ ...
- Android自注-15-Activity生命周期
很长一段时间没有写博客,懒,感慨一下. Activity的生命周期是一块以下附图: 通过代码下面简单的介绍一下.一些内容看代码的凝视: package com.mxy; import android. ...
- 推荐几个js的好链接
JavaScript 之美 其一:http://fxck.it/post/72326363595 其二:http://fxck.it/post/73513189448
- web开发中../、./、/的区别
原文:web开发中../.././的区别 最近在业余时间慢慢玩起了网站开发,觉得挺有意思的.在开发过程中,老是分不清 ../.././三者之间的区别,也老是弄混,最后仔细搜索研究了一下,现在终于懂了. ...
- INCORRECT PERMISSIONS ON /USR/LIB/PO1KIT-AGENT-HELPER-1(NEEDS TO BE SETUID ROOT)
INCORRECT PERMISSIONS ON /USR/LIB/PO1KIT-AGENT-HELPER-1(NEEDS TO BE SETUID ROOT) # sudo chmod +s /us ...