Home > Java > javaTutorial > body text

Example of efficiency comparison analysis of Java two-dimensional array copy

WBOY
Release: 2023-04-25 15:04:08
forward
808 people have browsed it

1.for loop

public static void show1(int[][] array){
        for(int i = 0;i < array.length;i++){
            for(int j = 0;j < array[i].length;j++){
                System.out.print(array[i][j]+" ");
            }
            System.out.println();
        }
    }
Copy after login

2.clone()

/**
 * 使用数组本身的 clone() 方法
 *
 * @param sourceArr 源数组
 * @param destArr   目标数组
 */
public static void copy3(int[][] sourceArr, int[][] destArr) {
    for (int i = 0; i < sourceArr.length; i++) {
        destArr[i] = sourceArr[i].clone();
    }
}
Copy after login

3.Arrays.arraycopy

int[][] array = {{1,2,3},{4,5,6}};
        int[][] array2 = new int[2][3];
        for(int i = 0;i < array.length;i++){
            System.arraycopy(array[i], 0, array2[i],
                    0, array[i].length);
Copy after login

4. Speed ​​comparison

arraycopy source code, this method copies the fastest and has no return value.

@HotSpotIntrinsicCandidate
public static native void arraycopy(Object src,  int  srcPos,
                                    Object dest, int destPos,
                                    int length);
Copy after login

The above is the detailed content of Example of efficiency comparison analysis of Java two-dimensional array copy. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!