byte数组快速拷贝,byte数组合并,System.arraycopy详解

article/2025/9/30 15:07:13

博客来源:

项目过程中用到byte[]数组相加问题,给出两个byte[] 需要合并成一个byte[]进行计算…那么需求来了……数据量达10W级,怎么合并

 调用系统自带方法(System.arraycopy)

参考程序

@org.junit.Test
public void fun(){//创建一个存储被拷贝的字节数组,长度一定要比被拷贝字节数组大byte[] bytes = new byte[20];//创建一个需要字节拷贝的数组byte[] byte_name = "xiaoming".getBytes();//调用系统方法进行拷贝System.arraycopy(byte_name, 0, bytes, 5, byte_name.length);//输出原字节数组,两者进行比较for (byte bb :byte_name) {System.out.print(bb);}//换行System.out.println();//输出被拷贝的数组,两者进行比较for (byte bb :bytes) {System.out.print(bb);}
}

运行结果,已经按照程序要就进行拷贝

下面进行详细介绍下,系统方法  (System.arraycopy)

简单用法:

java.lang.System public static void

arraycopy(@NotNull     Object src,           被拷贝的字节数组
                                              int srcPos,          从被拷贝的字节数组的第几位开始拷贝
                                              @NotNull Object dest,    拷贝到这个字节数组中
                                              int destPos,        从第几位开始拷贝被拷贝的数组
                                              int length            拷贝多少位被拷贝的字节数组

来!看一下官方解释, 其实也就是上面的解释,加之上面的程序,尝试几次就好啦

 

java.lang.System public static void arraycopy(@NotNull Object src,
                                                         int srcPos,
                                                         @NotNull Object dest,
                                                         int destPos,
                                                         int length)
Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. A subsequence of array components are copied from the source array referenced by src to the destination array referenced by dest. The number of components copied is equal to the length argument. The components at positions srcPos through srcPos+length-1 in the source array are copied into positions destPos through destPos+length-1, respectively, of the destination array.
If the src and dest arguments refer to the same array object, then the copying is performed as if the components at positions srcPos through srcPos+length-1 were first copied to a temporary array with length components and then the contents of the temporary array were copied into positions destPos through destPos+length-1 of the destination array.
If dest is null, then a NullPointerException is thrown.
If src is null, then a NullPointerException is thrown and the destination array is not modified.
Otherwise, if any of the following is true, an ArrayStoreException is thrown and the destination is not modified:

  •     The src argument refers to an object that is not an array.
  •     The dest argument refers to an object that is not an array.
  •     The src argument and dest argument refer to arrays whose component types are different primitive types.
  •     The src argument refers to an array with a primitive component type and the dest argument refers to an array with a reference component type.
  •     The src argument refers to an array with a reference component type and the dest argument refers to an array with a primitive component type.

Otherwise, if any of the following is true, an IndexOutOfBoundsException is thrown and the destination is not modified:

  •     The srcPos argument is negative.
  •     The destPos argument is negative.
  •     The length argument is negative.
  •     srcPos+length is greater than src.length, the length of the source array.
  •     destPos+length is greater than dest.length, the length of the destination array.

Otherwise, if any actual component of the source array from position srcPos through srcPos+length-1 cannot be converted to the component type of the destination array by assignment conversion, an ArrayStoreException is thrown. In this case, let k be the smallest nonnegative integer less than length such that src[srcPos+k] cannot be converted to the component type of the destination array; when the exception is thrown, source array components from positions srcPos through srcPos+k-1 will already have been copied to destination array positions destPos through destPos+k-1 and no other positions of the destination array will have been modified. (Because of the restrictions already itemized, this paragraph effectively applies only to the situation where both arrays have component types that are reference types.)

Params:
                    src – the source array.
                    srcPos – starting position in the source array.
                    dest – the destination array.
                    destPos – starting position in the destination data.
                    length – the number of array elements to be copied.
                    
Throws:
                    IndexOutOfBoundsException – if copying would cause access of data outside array bounds.
                    ArrayStoreException – if an element in the src array could not be stored into the dest array because of a type mismatch.
                    NullPointerException – if either src or dest is null.
                    
External annotations:
                    Parameter src: @org.jetbrains.annotations.NotNull
                    Parameter dest: @org.jetbrains.annotations.NotNull

 

 

 

 

 


http://chatgpt.dhexx.cn/article/emr3gU9x.shtml

相关文章

程序、进程、线程的区别

程序、进程、线程的区别 进程是程序的实体,而线程又是进程的实体。进程又是线程的容器。 程序、进程、线程三者区别如下: 1.程序:程序并不能单独执行,是静止的,只有将程序加载到内存中,系统为其分配资源后才能够执…

操作系统-进程与线程的区别

操作系统-进程与线程的区别 1.什么是进程 简单的讲,进程是执行的程序,资源分配的最小单位。 进程包括:文本段(程序代码)、程序计数器的值、CPU寄存器的内容、堆、栈、数据段 进程的状态:新的、就绪、运行…

进程和线程的区别(重点)

来源:http://www.cnblogs.com/lmule/archive/2010/08/18/1802774.html 简而言之,一个程序至少有一个进程,一个进程至少有一个线程. 线程的划分尺度小于进程,使得多线程程序的并发性高。 另外,进程在执行过程中拥有独立的内存单元&#xff0c…

进程与线程的区别和联系

程序并不能单独执行,只有将程序加载到内存中,系统为他分配资源后才能够执行,这种执行的程序称之为进程,也就是说进程是系统进行资源分配和调度的一个独立单位,每个进程都有自己单独的地址空间。所以说程序与进程的区别…

进程和线程的区别和联系

我们都知道计算机的核心是CPU,它承担了所有的计算任务,而操作系统是计算机的管理者,它负责任务的调度,资源的分配和管理,统领整个计算机硬件;应用程序是具有某种功能的程序,程序是运行于操作系统之上的。 进程 进程是一个具有一定独立功能的程序在一个数据集上的一次动…

进程与线程的区别及联系

目录 1. 操作系统功能简介 2. 进程 2.1 认识进程 2.2 进程操作系统中如何管理 2.3 PCB如何描述 2.3.1 pid 2.3.2 内存指针 2.3.3 文件描述符表 2.3.4 进程调度相关属性 3. 内存管理 4. 线程 4.1 认识线程 4.2 进程与线程的关系 4.3 线程安全问题 1.操作系统功能简…

Linux进程与线程的区别

进程与线程的区别,早已经成为了经典问题。自线程概念诞生起,关于这个问题的讨论就没有停止过。无论是初级程序员,还是资深专家,都应该考虑过这个问题,只是层次角度不同罢了。一般程序员而言,搞清楚二者的概…

win10安装时,提示“我们无法创建新的分区,也找不到现有分区”

win10安装时,提示“我们无法创建新的分区,也找不到现有分区”,如图所示: 解决办法: 将win10安装包(ios文件)解压,将以下文件复制到系统盘,然后重启电脑,自动…

我们无法创建新分区。【错误:0x80042468】

一台服务器6块1.8T SAS 10K,做RAID10. 安装windows server 2012R2系统在分区时报错,总有个分区不能创建成功。(正常安装系统后,磁盘管理也有一个磁盘不能创建新的分区) 提示“我们无法创建新分区。【错误:0…

重装系统“无法创建新的分区也找不到现有分区”

如题,一开始我使用分区工具对硬盘进行分区后再进入安装系统,一直出现这个问题报错。 (因我没拍照片,从网上找的图片,侵权请联系我) 后来我删除硬盘分区,直接在这里进行分区,就可以装了。如图&#xff0c…

解决无法创建新的分区,也找不到现有的分区。

注意:问题多发生在m2装系统(要拔出其他硬盘) 原创b站理想与天阳

关于windows安装过程中“我们无法创建新的分区,也找不到现有的分区”问题解决办法

最近在安装电脑系统过程中碰到了这个问题,首先说明下我电脑bios已经设置了uefi引导启动,硬盘分区格式也是GPT格式,还是出现这个问题有点纳闷,后面折腾了好久才找到解决办法: 即在对磁盘进行分区的时候不要创建ESP分区…

系统安装无法创建新的系统分区的解决方法

在安装Windows7时,想必有很多人都安碰到这样的情况吧!在安装界面里选择安装时,却出现“安装程序无法创建新的系统分区,也无法定位现有系统分区” 安装程序无法创建新的系统分区,也无法定位现有分区 网上提供的另外解决方法大全&a…

服务器系统安装提示无法创建新的系统分区,提示无法创建新的分区是怎么回事_安装win10系统无法新建分区的解决办法...

不少朋友在装win10的过程中,可能会遇到“我们无法创建新的分区,也找不到现有的分区”的提示,那么我们应该如何操作来解决此问题呢?下面就给大家讲解安装win10系统无法新建分区的解决办法。 安装win10系统无法新建分区的解决办法:…

Win10提示“无法创建新的分区也找不到现有的分区”

1 Win10提示“无法创建新的分区也找不到现有的分区” 由于公司电脑拷贝账户时,不小心,将原有的桌面文件夹直接替换,而不是将桌面内的文件替换,差生了诸多的问题: 我的电脑—打开后无法正常看到驱动器酷我无法切换歌曲…

启动盘安装windows系统时提示:“windows安装程序无法创建新的分区,也找不到现有分区”的解决方法

在使用启动盘安装Windows系统时有时会遇到无法安装的问题,选定主分区或新建主分区出现“windows安装程序无法创建新的分区,也找不到现有分区”的提示信息,导致安装失败。提示信息如下图所示: 出现这种问题就很头疼,我…

安装win10提示“我们无法创建新的分区,也找不到现有分区”的解决方法

用U盘安装操作系统,但是遇到了这种问题。——我是用微软官方工具制作的启动盘,因为是给M.2 NVME固态硬盘装win10,不能用pe。 虽然我用DiskGenius工具将硬盘格式化成GPT格式,但是安装时还是出问题。 出现这种情况可能是硬盘格式通…

win10:我们无法创建新的分区,也找不到现有的分区,

今日一朋友拜托我帮他笔记本升级安装win10。 加了一个内存条以后,就进入了安装win10的工作。 在选完我没有序列号,win10版本等等以后,一直跳出来的这个⚠信息挺烦人。(因手机没拍照片,从网上找到一张合适的,图片来源…

安装win10时,提示“我们无法创建新的分区,也找不到现有分区”

安装win10时,提示“我们无法创建新的分区,也找不到现有分区” 笔者在安装问10时,出现了“我们无法创建新的分区,也找不到现有分区”这个提示 查来查去,发现没有按照安装教程走,直接按照自己的想法格式化…