This blog link: https://www.cnblogs.com/ping2yingshi/p/12531721.html
1. Use .arraycopy method - taken array
Usage: arraycopy (former name of the array, the original array subscripts starting target array name, destination array subscripts starting intercept length)
For example:
1. Description: Array arr1 the original array, the array is arr2 target array, you need to three the number of array arr2 assignment to the array arr1.
2. added: can be assigned from anywhere.
code show as below:
import java.awt.List; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class test { public static void main(String[] args) { int arr1[]=new int []{1,2,3,4,5}; int arr2[]=new int []{7,8,9,10,11}; for(int i=0;i<5;i++) System.out.print(arr1[i]); System.out.println(); for(int i=0;i<5;i++) System.out.print(arr2[i]); System.out.println(); System.arraycopy(arr2,0,arr1,0,3); for(int i=0;i<5;i++) System.out.print(arr1[i]); }
}
Output:
2. The method of using the method java.util.Arrays.copyOf - array taken
use: java.util.Arrays.copyOf (former name of the array, a new array length);
For example:
1. Description: Array arr1 the original array, the array is arr2 new array, you need to three the number of the original array arr1 assignment reassigned to a new array arr2.
2. Note:
Only a portion of all the definitions in the original array to a new array.
code show as below:
import java.awt.List; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class test { public static void main(String[] args) { int arr1[]=new int []{1,2,3,4,5}; for(int i=0;i<5;i++) System.out.print(arr1[i]); System.out.println(); int arr2[]=java.util.Arrays.copyOf(arr1,3); for(int i=0;i<3;i++) System.out.print(arr2[i]); } }
Results are as follows: