Home  >  Article  >  Java  >  How to copy an array in java

How to copy an array in java

WBOY
WBOYforward
2023-04-29 22:22:171359browse

Copy of array

First type:

  把一个数组的值拷贝到另一个数组
 public static int[] copyArray(int[] array){
        int[] copy = new int[array.length];
        for (int i = 0; i <array.length ; i++) {
            copy[i] = array[i];
        }
        return copy;
    }
 
    public static void main(String[] args) {
        int[] array = {1,4,6,3,8,9};
        int[] ret = copyArray(array);
        System.out.println(Arrays.toString(ret));
 
 
    }

Print result:

How to copy an array in java

Second type:

Copy array (self) function

How to copy an array in java

Print result:

How to copy an array in java

The length can also be multiplied by 2, but not in the original On the basis of expanding 2 times, here is a new object

How to copy an array in java

Copy array (part) function:

How to copy an array in java

All source codes in Java from and to are left-closed and right-open.

The third type:

How to copy an array in java

Print result:

How to copy an array in java

The fourth copy:

How to copy an array in java

The above is the detailed content of How to copy an array in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete