Home > Java > javaTutorial > body text

How to copy an array in java

WBOY
Release: 2023-04-29 22:22:17
forward
1380 people have browsed it

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));
 
 
    }
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template