Share on Google+Share on Google+

Java arraycopy example- 1

Advertisement
This tutorial demonstrate the arraycopy method. In the below listed program you will see how arraycopy method is help in copy one array variable's values into a new array variable. An arraycopy method in java copies an array from the specified source array, beginning or from the specified position, to the specified position of the destination array.

Description:

An arraycopy method have the following parameters.
arraycopy(Object source, int srcPositon, Object destination, int destPosition, int length). Here in this tutorial you will see a simple example of the arraycopy method of java.

Code:

class DisplyaArrayCopy {
  DisplyaArrayCopy() {
    int[] intArray = new int[] { 1234};
    int[] arrayCopy = new int[intArray.length];
    System.arraycopy(intArray, 0, arrayCopy, 0, intArray.length);
    for (int i = 0; i < arrayCopy.length; i++)
      System.out.println(arrayCopy[i]);
  }
}

public class ArrayCopyExample {
  public static void main(String[] args) {
    new DisplyaArrayCopy();
  }
}

Output:

1
2
3
4
5

Java arraycopy example- 2
An arraycopy method in java copies an array from the specified source array, beginning or from the specified position, to the specified position of the destination array.
 
Java copyOf example
This tutorial demonstrate the copyOf method. In the below listed program you will see how copyOf method is help in copy one array variable's values into a new array variable.
 

Advertisements

Advertisement

Posted on: April 9, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.