Home Tutorial Java Corejava Java arraycopy example- 1

 
 

Java arraycopy example- 1
Posted on: April 9, 2010 at 12:00 AM
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.
 

Related Tags for Java arraycopy example- 1:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.