Java System copy array

Introduction

The following code shows how to copy array.


public class Main {
  static byte a[] = { 65, 66, 67, 68, 69, 70, 71, 72, 73, 74 };
  static byte b[] = { 77, 77, 77, 77, 77, 77, 77, 77, 77, 77 };

  public static void main(String args[]) {
    System.out.println("a = " + new String(a));
    System.out.println("b = " + new String(b));
    System.arraycopy(a, 0, b, 0, a.length);
    System.out.println("a = " + new String(a));
    System.out.println("b = " + new String(b));
    System.arraycopy(a, 0, a, 1, a.length - 1);
    System.arraycopy(b, 1, b, 0, b.length - 1);
    System.out.println("a = " + new String(a));
    System.out.println("b = " + new String(b));
  }/*from   w  w  w. ja v a2s. c  om*/
}
import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    int[] intArray = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };

    int[] arrayCopy = new int[intArray.length];

    System.out.println(Arrays.toString(intArray));
    //from ww w  .j ava  2s . c  om
    System.arraycopy(intArray, 0, arrayCopy, 0, intArray.length);

    System.out.println(Arrays.toString(arrayCopy));
  }
}



PreviousNext

Related