Chapter 3: array [5 common algorithm] - [4 Copy]

① custom logic

int[] array1 = new int[]{1,2,3,4};

 

int[] array2 = new int[array1.length];

for(int i = 0;i < array2.length;i++){

    array2[i] = array1[i];

}

System.arraycopy () - the interval [a, b]

 

 

 

/*

* @Param src the source array.// source array

* @Param srcPos starting position in the source copying start position of the source array array.//

* @Param dest the destination array.// target array

* @Param destPos starting position in the destination data.// destination array starting position

* @Param length the number of array elements to be copied.// target array end position

*/

 

//public static native void arraycopy(Object src,  int  srcPos, Object dest, int destPos,  int length);

 

String[] A = {"H","e","l","l","o"};

String[] B = new String[3];

System.arraycopy(A, 0, B, 1, B.length - 1); // [a,b]

for(int i = 0; i < B.length; i ++){

  System.out.print(B[i] + " ");

}

//null H e

 

③Arrays.copyOf()

String[] a = {"a","d","e","w","f"};

String[] b = new String[4];

String[] c = new String[5];

b = Arrays.copyOf(a, b.length);

c = Arrays.copyOf(a, c.length);

System.out.println ( "b element array:" + Arrays.asList (b)) ; element array // b: [a, d, e, w]

System.out.println ( "c array element:" Arrays.asList + (c)); // c array element: [a, d, e, w, f]

④Arrays.copyOfRange () - the interval [a, b) 

String[] a = {"a","d","e","w","f"};

String[] b = new String[4];

b = Arrays.copyOfRange(a, 2, 4);// [a,b)

System.out.println ( "array element b:" Arrays.asList + (b)); // b of the array element: [e, w];

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/Lucky-stars/p/11010122.html
Recommended