Copy array

ReturnMethodSummary
static voidarraycopy(Object src, int srcPos, Object dest, int destPos, int length)Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.

The arraycopy( ) method can be used to copy quickly an array of any type from one place to another.

 
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));
  }

}

The output:


a = ABCDEFGHIJ
b = MMMMMMMMMM
a = ABCDEFGHIJ
b = ABCDEFGHIJ
a = AABCDEFGHI
b = BCDEFGHIJJ
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.