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

How to copy an array in java

(*-*)浩
(*-*)浩Original
2019-05-22 13:36:412310browse

There are several ways to copy an array in java:

How to copy an array in java

(1) clone

(2) System.arraycopy

(3) Arrays.copyOf

(4) Arrays.copyOfRange

The following introduces their usage:

(1) The clone method is inherited from the Object class. Basic data types (String, boolean, char, byte, short, float, double.long) can be cloned directly using the clone method. Note that String Types can be used because their values ​​are immutable.

int a1[]={1,3};
int a2[]=a1.clone();
a1[0]=666;
System.out.println(Arrays.toString(a1));//[666,3]
System.out.println(Arrays.toString(a2));//[1,3]

2) The System.arraycopy method is a local method, defined as follows in the source code:

/**
     * System.arraycopy(src,srcPos,dest,destPos,length);
     * src :源数组          srcPos:源数组要复制的起始位置;
     * dest:目的数组   destPos:目的数组放置的起始位置;
     * length:复制的长度.
     */    
public static native void arraycopy(Object src,  int  srcPos,
                                        Object dest, int destPos,
                                        int length);

Usage example:

int  a1[]={1,2,3,4,5};
int a2[]=new int[10];
System.arraycopy(a1,1,a2,3,3);
System.out.println(Arrays.toString(a1));//[1, 2, 3, 4, 5]
System.out.println(Arrays.toString(a2));//[0, 0, 0, 2, 3, 4, 0, 0, 0, 0]

Note: This method requires us to create a new array ourselves

(3) The bottom layer of Arrays.copyOf actually also uses System.arraycopy. The source code is as follows:

public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
        @SuppressWarnings("unchecked")
        T[] copy = ((Object)newType == (Object)Object[].class)
            ? (T[]) new Object[newLength]
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
        System.arraycopy(original, 0, copy, 0,
                         Math.min(original.length, newLength));
        return copy;
}

Usage Example:

int  a1[]={1,2,3,4,5};
int a2[]=Arrays.copyOf(a1,3);//(原数组,拷贝的个数)
System.out.println(Arrays.toString(a1));//[1, 2, 3, 4, 5]
System.out.println(Arrays.toString(a2));//[1, 2, 3]    
//这个方法不需要我们new新的数组

(4) The bottom layer of Arrays.copyOfRange actually uses System.arraycopy, but it encapsulates a method

int  a1[]={1,2,3,4,5};
int a2[]=Arrays.copyOfRange(a1,0,1);//(原数组,开始位置,拷贝的个数)
System.out.println(Arrays.toString(a1));//[1, 2, 3, 4, 5]
System.out.println(Arrays.toString(a2));//[1]

The last thing to note is that the copy of the basic type is If it does not affect the value of the original array, it cannot be used if it is a reference type, because the copy (replication) of the array is a shallow copy.

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:
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
Previous article:What is java cv?Next article:What is java cv?