当前位置: 首页 > news >正文

Arrays.copyOfRange(),System.arraycopy() 数组复制,数组扩容

Arrays.copyOfRange()

当需要将数组中的 长度扩容时, 数组复制 使用

需要用到Arrays 类提供的的

 

参数解析

* @param original the array from which a range is to be copied
* @param from the initial index of the range to be copied, inclusive
* @param to the final index of the range to be copied, exclusive.

                                                                                                                ---------来自源码注释

第一个参数  从中复制范围的数组

第二个参数  起始的index(索引)  inclusive(包括)

第三个参数  末尾的index(索引)  exclusive(不包括)

举个例子

public static void main(String[] args) {int[]  a  ={1,2,3,4,5,6};int[] c = Arrays.copyOfRange(a,0 , 10);//数组扩容System.out.println(Arrays.toString(c));System.out.println("c"+c.length);System.out.println("a"+a.length);}

 此时输出

[1, 2, 3, 4, 5, 6, 0, 0, 0, 0]
c10
a6

 查看源码  

                                                                                                                           ----来java.util.Arrays

此时可以看到,新创建了一个数组copy   长度为 to - from   返回的是一个新的数组

System.arraycopy() 

并调用  java.lang.System中的

public static native void arraycopy(Object src,  int  srcPos,Object dest, int destPos,int length);

System中arraycopy()参数

* @param      src      the source array.
* @param      srcPos   starting position in the source array.
* @param      dest     the destination array.
* @param      destPos  starting position in the destination data.
* @param      length   the number of array elements to be copied.

src   原数组

srcPos 原数组起始的位置

dest   目标数组

destpos  目标数组起始位置

length  复制length长度从原数组到起始数组

实例  使用这个方法模仿队列的出队

 public static void main(String[] args) {int[]  a  ={1,2,3,4,5,6};int[] c = Arrays.copyOfRange(a,0 , 10);//数组扩容System.out.println(Arrays.toString(c));System.out.println("c的长度为:"+c.length);System.out.println("a的长度为:"+a.length);System.arraycopy(a,1,a,0, a.length-1);System.out.println(Arrays.toString(a));//出队列}

输出

[1, 2, 3, 4, 5, 6, 0, 0, 0, 0]
c的长度为:10
a的长度为:6
[2, 3, 4, 5, 6, 6]

所以Arrays.copyOfRange()方法底层调用了System.arraycopy()

 总结

本文讲了Arrays.copyOfRange()方法针对数组的使用

底层创建一个长度为to-from的数组

将原数组中的数据从从索引form 复制长度为min(form到to,length-from)  复制到copy这个数组中并返回copy这个数组.

复制时有两种情况

to-from小于原数组的长度        此时相当于截取复制

to-from大于原数组的长度         此时相当于扩容复制

毕竟当调用   System.arraycopy 方法时要保证取的 长度要小于src的长度  放的长度要 小于copy的长度

所以要调用Math.min()

http://www.lryc.cn/news/493968.html

相关文章:

  • Python学习37天
  • flask的第一个应用
  • 【论文格式】同步更新中
  • Java-GUI(登录界面示例)
  • 看华为,引入IPD的正确路径
  • 计算机毕业设计Spark+大模型知识图谱中药推荐系统 中药数据分析可视化大屏 中药爬虫 机器学习 中药预测系统 中药情感分析 大数据毕业设计
  • pcb线宽与电流
  • w~视觉~合集26
  • Qt支持RKMPP硬解的视频监控系统/性能卓越界面精美/实时性好延迟低/录像存储和回放/云台控制
  • 【Qt】图片绘制不清晰的问题
  • 2008年IMO几何预选题第3题
  • NAT拓展
  • Flink四大基石之State
  • Spacy小笔记:zh_core_web_trf、zh_core_web_lg、zh_core_web_md 和 zh_core_web_sm区别
  • 第六届智能控制、测量与信号处理国际学术会议 (ICMSP 2024)
  • docker服务容器化
  • 【QT】控件8
  • 漫谈推理谬误——错误因果
  • 【数据结构】队列实现剖析:掌握队列的底层实现
  • 【C++】IO库(二):文件输入输出
  • 105.【C语言】数据结构之二叉树求总节点和第K层节点的个数
  • 力扣637. 二叉树的层平均值
  • 【前端】Next.js 服务器端渲染(SSR)与客户端渲染(CSR)的最佳实践
  • 路径规划之启发式算法之一:A-Star(A*)算法
  • Android复习代码1-4章
  • 【问题】webdriver.Chrome()设置参数executable_path报不存在
  • win10系统安装docker-desktop
  • 小程序-基于java+SpringBoot+Vue的乡村研学旅行平台设计与实现
  • 组件A底部栏(position: fixed )事件使用$emit更新内容失败bug解决
  • 数据结构——排序第三幕(深究快排(非递归实现)、快排的优化、内省排序,排序总结)超详细!!!!