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 ...
随机推荐
- (转)Spring Boot(二十):使用 spring-boot-admin 对 Spring Boot 服务进行监控
http://www.ityouknow.com/springboot/2018/02/11/spring-boot-admin.html 上一篇文章<Spring Boot(十九):使用 Sp ...
- python之常用模块(续)
time模块 random模块 sys模块 os模块 序列化模块 time模块 有三种方式表示 在Python中,通常有三种方式来表示时间:时间戳.元组(struct_time).格式化的时间字符串 ...
- TortoiseHg 学习笔记
0.前言 TortoiseHg是分布式的源代码管理工具Mercurial的GUIclient. mercurial 作为3大主流的分布式源代码管理工具.已经被广泛的使用.比如 googleco ...
- Jmeter插件安装及使用
1 安装Plugins Manager插件 1.1 下载Plugins Manager插件 插件下载官方地址:https://jmeter-plugins.org/downloads/all/ 将下载 ...
- [matlab] 16.多约束非线性规划 ga工具箱解决 [带不等式约束]
下面举例说明如何运用GA工具箱求解多约束非线性规划问题: function f =fitness(x) f=exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2 ...
- WPF 样式(定义样式、引用样式、样式作用域、Trigger触发器)
1.定义 资源字典 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presenta ...
- Apache 2.4.27 局域网访问提示 You don't have permission to access / on this server
问题: 本机用localhost和ip都可以访问,局域网不可以访问,并且出现提示 You don't have permission to access / on this server. 解决: 如 ...
- Python:Day53 Template基础
一.模板由 html代码+逻辑控制代码 组成 二.逻辑控制代码的组成 1.变量(使用双大括号来引用变量) 语法格式:{{ var_name }} -----------------------Temp ...
- pytorch .detach() .detach_() 和 .data用于切断反向传播
参考:https://pytorch-cn.readthedocs.io/zh/latest/package_references/torch-autograd/#detachsource 当我们再训 ...
- nginx之十三:搭建 nginx 反向代理用做内网域名转发
user www www;worker_processes 1;error_log logs/error.log;pid logs/nginx.pid;worker_rlimit_nofile 6 ...