java int數(shù)組拷貝怎么實(shí)現(xiàn)

小億
125
2023-10-27 10:29:15
欄目: 編程語言

要實(shí)現(xiàn)Java中int數(shù)組的拷貝,可以通過以下兩種方法:

方法一:使用Arrays類的copyOf()方法

int[] sourceArray = {1, 2, 3, 4, 5};
int[] targetArray = Arrays.copyOf(sourceArray, sourceArray.length);

方法二:使用System類的arraycopy()方法

int[] sourceArray = {1, 2, 3, 4, 5};
int[] targetArray = new int[sourceArray.length];
System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);

無論是使用Arrays.copyOf()方法還是System.arraycopy()方法,都可以實(shí)現(xiàn)int數(shù)組的拷貝。使用哪種方法取決于個(gè)人的偏好和具體需求。

0