0
2.0kviews
Write a detail note on System.arraycopy ().

Mumbai University > Information Technology > Sem 3 > Object Oriented Programming Methodology

Marks: 5 M

Year: Dec 2013, May 2014, May 2015, Dec 2014

1 Answer
1
2views
  • The arraycopy( ) method can be used to copy quickly an array of any type from one place to another. This is much faster than the equivalent loop written out longhand in Java.
  • Let’s take an example of two arrays being copied by the arraycopy( ) method. First, array named a is copied to array named b.
  • Next, all of a’s elements are shifted down by one. Then, b is shifted up by one.

    class ACDemo 
    {
    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));
    }
    }
    
  • As you can see from the following output, you can copy using the same source and destination in either direction:

a = ABCDEFGHIJ

b = MMMMMMMMMM

a = ABCDEFGHIJ

b = ABCDEFGHIJ

a = AABCDEFGHI

b = BCDEFGHIJJ

Please log in to add an answer.