Wednesday, September 24, 2008

make an array copy from System -java

How to make an array copy from System?
There is a method called arraycopy in the System class. You can do it:
System.arraycopy(sourceArray, srcOffset, destinationArray, destOffset, numOfElements2Copy);
When you use this method, the destinationArray will be filled with the elements of sourceArray at the length specified.

Can we use System.arraycopy() method to copy the same array?
Yes, you can. The source and destination arrays can be the same if you want to copy a subset of the array to another area within that array.

What is shallow copy or shallow clone in array cloning?
Cloning an array invloves creating a new array of the same size and type and copying all the old elements into the new array. But such copy is called shallow copy or shallow clone because any changes to the object would be reflected in both arrays.

When is the ArrayStoreException thrown?
When copying elements between different arrays, if the source or destination arguments are not arrays or their types are not compatible, an ArrayStoreException will be thrown.

How to check two arrays to see if contents have the same types and contain the same elements?
One of options is to use the equals() method of Arrays class.
Arrays.equals(a, b);
If the array types are different, a compile-time error will happen.

No comments:

Post a Comment

Thanks to given comments.......

My Blog List