Tuesday, October 11, 2011

Copy Array

The System class provide an arraycopy() method to copy array.

Copy Array

import java.lang.String;

public class exCopyArray{

public static void main(String[] args){

String src[] ={
"ABC",
"DEF",
"GHI",
"JKL",
"MNO",
"PQR",
"STU",
"VWX",
"YZ"};

System.out.println("Copy Array Exerecise");
showAll(src);

System.out.println();

String[] dest = new String[5];
System.arraycopy(src, //Copy from
2, //Source position
dest, //Copy to
0, //Destination position
5); //length to copy
showAll(dest);

}

static void showAll(String[] a){
System.out.println(a.toString());
for(int i = 0; i < a.length; i++){
System.out.println(i + ": " + a[i]);
}
}

}

No comments:

Post a Comment