Powered by Blogger.
Showing posts with label array copy example. Show all posts
Showing posts with label array copy example. Show all posts

Array copy Optimization in Java Program

Array copy Optimization:

In Java technology, all array indexes begin at 0. The number of elements in an array is stored as part of the array object in the length attribute. If an out-of-bounds runtime access occurs, then a runtime exception is thrown. After array id created, you cannot resize an array. However, you can use the same reference variable to refer to an entirely new array. Java provides a special method in the System class, arraycopy() to copy arrays.


Array Copy Example:-

 public void arrayCopy(){
 int[] x = {1,2,3,4,5,6};
 int[] y = {11,12,13,14,15,16,17,18,19,20};
 System.arraycopy(x,0,y,0,x.length);
  }
 }

Arraycopy method that you can use to efficiently copy data from one array into another:

public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)