java中的System.copyof()与Array.copyof()区别
来源:互联网 发布:three.js加载3d模型 编辑:程序博客网 时间:2024/06/06 08:59
在复制数组时我们可以使用System.copyof(),也可以使用Array.copyof(),但是它们之间是有区别的。以一个简单的例子为例:
System.arraycopy()
int[] arr = {1,2,3,4,5};int[] copied = new int[10];System.out.println(Arrays.toString(copied));System.arraycopy(arr, 0, copied, 1, 5);//5是复制的长度System.out.println(Arrays.toString(copied));
输出
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0][0, 1, 2, 3, 4, 5, 0, 0, 0, 0]
Arrays.copyOf()
int[] copied = Arrays.copyOf(arr, 10); //10是新数组的长度System.out.println(Arrays.toString(copied));copied = Arrays.copyOf(arr, 3);System.out.println(Arrays.toString(copied));
输出
[1, 2, 3, 4, 5, 0, 0, 0, 0, 0][1, 2, 3]
最主要的区别
区别是Arrays.copyOf() 不只复制数组元素,也创建一个新数组。
System.arrayCopy 只复制已有的数组。
但是如果我们读Arrays.copyOf()的源码也会发现,它也用到了System.arrayCopy()。
public static int[] copyOf(int[] original, int newLength) { int[] copy = new int[newLength]; System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); return copy; }
0 0
- java中的System.copyof()与Array.copyof()区别
- java中System.copyArray与Arrays.copyof区别
- java array.copyof用法
- Java中的copyOf()
- 数组拷贝 Array.copyof()方法与 System.arraycopy()方法
- 关于System.ArrayCopy()与copyOf()
- System.arraycopy() 与 Arrays.copyOf()两个方法的区别
- java copyOf 和 System.arraycopy()使用区别详解
- Java中System.arraycopy()和Arrays.copyOf()的区别
- Java编程:Arrays.copyOf()与System.arraycopy()的分析
- Arrays.copyof 和 System.arraycopy区别
- System.arraycopy()和Arrays.copyOf()的区别
- System.arraycopy 和 Arrays.copyOf()的区别
- System.arraycopy()和Arrays.copyOf()的区别
- System.arraycopy()和Arrays.copyOf()的区别
- System.arraycopy 与 Arrays.copyOf 分析
- System.arraycopy() vs. Arrays.copyOf() in Java
- Arrays.copyof(···)与System.arraycopy(···)区别
- 棋盘问题-简单搜索练习
- onTouchEvent、onClick和onLongClick的调用机制
- [1]java:你真的懂你的第一个Hello World程序吗
- UIViewControllerAnimatedTransitioning UIViewControllerTransitionCoordinator 回顾
- 使用MyEclipse创建Maven项目出现的错误及解决方法
- java中的System.copyof()与Array.copyof()区别
- 欢迎使用CSDN-markdown编辑器
- ubuntu 下开机项设置
- 逆波兰表达式(前缀表达式)
- Android文件的读写操作
- Linux笔记(60)——详解apache集成php
- Nginx 记录请求分发日志设置
- Git 忽略一些文件不加入版本控制
- 2016大连理工大学计算机考研复试