To copy an array in JAVA we have 3 options.
- Manually iterating elements of the source array and placing each one into the destination array using a loop.
- arraycopy() Method of java.lang.System class
Method syntax
- public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
Demo Program
- class ArrayCopyDemo {
- public static void main(String[] args) {
- char[] copyFrom = {
- 'd',
- 'e',
- 'c',
- 'a',
- 'f',
- 'f',
- 'e',
- 'i',
- 'n',
- 'a',
- 't',
- 'e',
- 'd'
- };
- char[] copyTo = new char[7];
- System.arraycopy(copyFrom, 2, copyTo, 0, 7);
- System.out.println(new String(copyTo));
- }
- }
Gowtham RajamanickamPosted May 19, 2015, 12:46 AM
good one