首页 > 其他分享 >数组拷贝System.arraycopy

数组拷贝System.arraycopy

时间:2024-08-13 18:49:24浏览次数:3  
标签:src dest System Arrays int 数组 拷贝 arraycopy

数组拷贝

第一种方式:

package com.coding.demo.concurrent;

import java.util.Arrays;

/**
 * 使用Arrays.copyOf()
 */
public class TestArraysCopyOf {
    public static void main(String[] args) {
        int[] src = {1,2,3,4,5,6,7,8,9};
        int[] dest = Arrays.copyOf(src, src.length);
        System.out.println(Arrays.toString(dest));
        // output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
    }
}

第二种方式:

package com.coding.demo.concurrent;

import java.util.Arrays;

/**
 * 使用System.arraycopy
 */
public class TestSystemArrayCopyOf {
    public static void main(String[] args) {
        int[] src = {1,2,3,4,5,6,7,8,9};
        int[] dest = new int[10];
        //   * @param      src      the source array.原数组
        //    * @param      srcPos   starting position in the source array.从原数组的哪个位置进行拷贝,默认下标从0开始。
        //    * @param      dest     the destination array.目标数组
        //    * @param      destPos  starting position in the destination data.从目标数组的哪个位置开始存放
        //    * @param      length   the number of array elements to be copied.要拷贝多少个元素到新数组中,
        // 如果新数组的长度10,但是我就从原来的数组中拿8个元素拷贝到新数组中,新数组剩余的两个位置将会用0来填补。
        System.arraycopy(src,1, dest, 0, src.length - 1);
        System.out.println(Arrays.toString(dest));
        // output: [2, 3, 4, 5, 6, 7, 8, 9, 0, 0]
    }
}java

标签:src,dest,System,Arrays,int,数组,拷贝,arraycopy
From: https://www.cnblogs.com/dongyaotou/p/18357535

相关文章

  • java浅拷贝BeanUtils.copyProperties引发的RPC异常
    背景近期参与了一个攻坚项目,前期因为其他流程原因,测试时间已经耽搁了好几天了,本以为已经解决了卡点,后续流程应该顺顺利利的,没想到人在地铁上,bug从咚咚来~没有任何修改的服务接口,抛出异常:java.lang.ClassCastException:java.util.HashMapcannotbecasttocn.xxx.xxx.xxx.xx......
  • HTTP请求错误:System.Net.WebException: 请求被中止: 未能创建 SSL/TLS 安全通道。
    调用有赞API时,报如下错误:HTTP请求错误:System.Net.WebException:请求被中止:未能创建SSL/TLS安全通道。经过排查得知,有赞的的api仅支持TLSv1.2协议版本我的程序使用的.NETFramework4.0不支持TLSv1.2协议版本 .NETFramework4.0Ssl3、Tls.NETFramework4.5-4.6.2......
  • C++浅拷贝和深拷贝
    在C++编程中,对象的拷贝是一项常见的操作。深拷贝和浅拷贝是两种常用的拷贝方式,对于理解对象拷贝的内部机制和避免潜在的问题至关重要。本文将深入解析C++中的深拷贝和浅拷贝的概念、原理以及使用场景,帮助读者更好地掌握和运用这两种拷贝方式。浅拷贝(ShallowCopy)是指在拷贝对象时......
  • C++类和对象(中):构造函数、析构函数、拷贝构造、赋值运算符重载
    文章目录C++类和对象4、类的默认成员函数5、构造函数5.1构造函数的特点5.2实例分析6、析构函数6.1析构函数的特点6.2实例分析7、拷贝构造函数7.1拷贝构造函数的特点7.2实例分析7.3浅拷贝和深拷贝8、赋值运算符重载8.1运算符重载8.1.1运算符重载的特点8.1.2实例分析8.......
  • 改变IntelliJ IDEA 中的system和config/plugins的默认C盘的路径
    1,问题,在为idea在线安装插件时,如JProfiler,会默认安装到C盘,而本人则是希望安装到软件所在的D盘目录下,那么如何修改呢:C:\Users\xxx.IntelliJIdea\config\plugins2,修改方法:打开IntelliJIDEA的安装目录,如本人的为D:\JetBrains\IntelliJIDEA2018.2然后在bin目录下找到idea.pr......
  • C# System.DateTime.Now 的一些用法
    C#中的日期处理函数     //2007年4月24日     this.TextBox6.Text=System.DateTime.Now.ToString("D");     //2007-4-24     this.TextBox7.Text=System.DateTime.Now.ToString("d");     //2007年4月24日16:30:15     this.TextBox8......
  • 使用WIN7 CMD 时出现了“The system cannot write to the specified device”
    使用WIN7CMD时出现了“Thesystemcannotwritetothespecifieddevice”(1)输入chcp可以查看cmd的编码(2)常见编码编号:65001:utf-820936:GB2312936:GBK437:美国英语(3)修改cmd的编码:chcpXXXX(编码编号) 1、右键点击Bat批处理,选择编辑,然后打开,重新另存为编码选择ANSI......
  • System has not been booted with systemd as init system (PID 1). Can't operate on
    昨天为了安装mariadb,不小心可能安装了sysinit的东西,在启动gogs服务时报了这个错'Systemhasnotbeenbootedwithsystemdasinitsystem(PID1).Can'toperate'找到了解决方案:我的理解是这样的linux系统大致有两种管理服务的方式,一种是sysinit一种是systemctl ......
  • System to practice
    1、Linux中哪个系统调用可以用于设置一个定时器,当时间到时,发送一个信号给进程?(B)a)setitimer()b)alarm()c)timer_create()d)time()tips:timer_create()是一个用于创建定时器的系统调用函数,定义在POSIX标准中,属于Linux系统的时间管理功能。它用于创建一个定时器对象,并......
  • System类day12
    /*System类:和系统相关操作的类publicstaticvoidgc()垃圾回收的publicstaticvoidexit(intstatus)强制结束java进程publicstaticlongcurrentTimeMillis()获取当前系统的时间戳,毫秒形式*/publicclassSystemDemo1{......