Arrays.copyOf() 和 System.arrayCopy()分析
java数组的拷贝四种方法:for、clone、System.arraycopy、Arrays.copyof
public class Test1 {
public static void main(String[] args) {
int[] arr1 = {0, 1, 2, 3, 4, 5, 6};
int[] arr2 = new int[7];
// for循环
for ( int i = 0; i < arr1.length; i++ ) {
arr2[i] = arr1[i];
}
for ( int i = 0; i <arr2.length; i++ ) {
System.out.print(arr2[i]); // 0123456
}
System.out.println();
//clone方法复制数组
int[] arr3 = new int[7];
arr3 = arr1.clone();
for ( int i = 0; i < arr3.length; i++ ) {
System.out.print(arr3[i]); // 0123456
}
System.out.println();
// System.arraycopy方法
int[] arr4 = new int[7];
System.arraycopy(arr1, 0, arr4, 1, 3);
for ( int i = 0; i < arr4.length; i++ ) {
System.out.print(arr4[i]); // 0012000
}
System.out.println();
// Arrays.copyOf方法
int[] arr5 = Arrays.copyOf(arr1, 2);
for ( int i = 0; i < arr5.length; i++ ) {
System.out.print(arr5[i]); // 01
}
}
}
先看看System.arraycopy()的声明:
public static native void arraycopy(Object src,int srcPos, Object dest, int destPos,int length);
src - 源数组。
srcPos - 源数组中的起始位置。
dest - 目标数组。
destPos - 目标数据中的起始位置。
length - 要复制的数组元素的数量。
该方法用了native关键字,说明调用的是其他语言写的底层函数。
再看Arrays.copyOf()
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
@SuppressWarnings("unchecked")
T[] copy = ((Object)newType == (Object)Object[].class)?(T[]) new Object[newLength]:(T[])
Array.newInstance(newType.getComponentType(), newLength);System.arraycopy(original,0, copy,0,
Math.min(original.length, newLength));
return copy;
}
该方法对应不同的数据类型都有各自的重载方法
original - 要复制的数组
newLength - 要返回的副本的长度
newType - 要返回的副本的类型
仔细观察发现,copyOf()内部调用了System.arraycopy()方法
区别在于:
- arraycopy()需要目标数组,将原数组拷贝到你自己定义的数组里,而且可以选择拷贝的起点和长度以及放入新数组中的位置
- copyOf()是系统自动在内部新建一个数组,调用arraycopy()将original内容复制到copy中去,并且长度为newLength。返回copy; 即将原数组拷贝到一个长度为newLength的新数组中,并返回该数组。
总结
Array.copyOf()可以看作是受限的System.arraycopy(),它主要是用来将原数组全部拷贝到一个新长度的数组,适用于数组扩容。
Arrays.copyOf() 和 System.arrayCopy()分析的更多相关文章
- Arrays.copyof(···)与System.arraycopy(···)区别
首先观察先System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)的声明: public stati ...
- [java]Arrays.copyOf() VS System.arrayCopy()
If we want to copy an array, we can use either System.arraycopy() or Arrays.copyOf(). In this post, ...
- Java数组的复制Arrays.copyOf()、System.arraycopy()、nums.clone()
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length); a ...
- System.arraycopy()和Arrays.copyOf()的区别
先看看System.arraycopy()的声明: public static native void arraycopy(Object src,int srcPos, Object dest, in ...
- Java-Java中System.arraycopy() 和 Arrays.copyOf()两者之间的区别
如果我们想拷贝一个数组,我们可能会使用System.arraycopy()或者Arrays.copyof()两种方式.在这里,我们将使用一个比较简单的示例来阐述两者之间的区别. 1.示例代码: Sys ...
- 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(src, srcPos, dest, destPos, length) 与 Arrays.copyOf(original, newLength)区别
//System.arraycopy,只拷贝已存在的数组元素 int[] src = {0, 1, 2}; int[] dest = new int[3]; System.arraycopy(src, ...
- 数组复制的五种方式(遍历循环一一赋值、System.arraycopy、地址赋值、克隆clone()、Arrays.copyof())
package com.Summer_0424.cn; import java.util.Arrays; import java.util.concurrent.CopyOnWriteArrayLis ...
- Java中 System.arraycopy() 和 Arrays.copyOf()方法
System.arraycopy() 和 Arrays.copyOf()方法 阅读源码的话,我们就会发现 ArrayList 中大量调用了这两个方法.比如:我们上面讲的扩容操作以及add(int in ...
随机推荐
- UEditor学习笔记1
首先对于前段时间遇到的一些总结做下记录: 0 == '' => true transition,渐变属性,其值可以是某属性,也可以是all,如transition: all 0.3s:鼠标放到 ...
- JavaScript在网页中使用以及注意事项
<script>标签的解析<script>xxx</script>这组标签,是用于在 html 页面中插入 js 的主要方法.它主要有以下几个属性:charset: ...
- MySQL高级知识(七)——索引面试题分析
前言:该篇随笔通过一些案例,对索引相关的面试题进行分析. 0.准备 #1.创建test表(测试表). drop table if exists test; create table test( id ...
- chrome-performance页面性能分析使用教程
运行时性能表现(runtime performance)指的是当你的页面在浏览器运行时的性能表现,而不是在下载页面的时候的表现.这篇指南将会告诉你怎么用Chrome DevTools Performa ...
- 【转】C盘不能扩展卷怎么回事 C盘扩展卷灰色的解决办法
今天有百事网网友“丅亿页”遇到了这样一个问题:电脑C盘剩余容量太小,在看到百事网的一篇“如何合并磁盘分区 windows7调整分区大小方法”文章后,也想将自己C盘系统盘空间扩大.按照上面文章中介绍的步 ...
- PHPsql
下面员工3的薪水大于其主管的薪水,一条SQL找到薪水比下属低的主管 id username salary pid 1 a 3000 null 2 b 8000 null 3 c 5000 1 4 d ...
- hadoop学习笔记叁--简单应用
1.通过命令向HDFS传输文件 上传:./hadoop fs -put hdfs.cmd (本地文件名) hdfs://主机名称:9000/ hadoop fs -copyFromLoca ...
- 初学Python——软件目录结构规范
为什么要设计好目录结构? 可读性高: 不熟悉这个项目的代码的人,一眼就能看懂目录结构,知道程序启动脚本是哪个,测试目录在哪儿,配置文件在哪儿等等.从而非常快速的了解这个项目. 可维护性高: 定义好组织 ...
- handsontable-chosen-editor
https://github.com/mydea/handsontable-chosen-editor handsontable-chosen-editor是handsontable column的扩 ...
- RichTextbox下Hyperlink的Click无效
原文:RichTextbox下Hyperlink的Click无效 两种方式解决: 1.<RichTextBox IsReadOnly="True" IsDocumentEna ...