Copy an array into another in java
You can either use any of this following methods only for one dimensional array- Object.clone()
- System.arraycopy()
- Arrays.copyOf()
- Arrays.copyOfRange()
- ArrayUtils.clone() from apache commons
Java code
/*** Copying an array in Java
*
*/
package com.Konzerntech.tutorial;
import java.util.Arrays;
/**
* @author Konzerntechnologies
*
*/
public class CopingArray {
/**
* @param args
*/
public static void main(String[] args) {
// Source array
int a[] = {8,2,8,1,8,0,8,0,2,9};
/**
* Since array is also an Object
* you can use clone method
* you can copy the whole object
* But partial copying is not possible
*/
int d[] = a.clone();
System.out.println(" using clone method ");
for(int i =0; i< d.length; i++) {
System.out.print(d[i]+"\t");
}
/**
* The best way to copy array
* you can do partial copying
* From a source to destination to a different position
*
* The below code will copy 4 elements from
* starting from first arrays second position to second arrays 3rd position
*/
System.out.println("\n using System.arrayCopy() method ");
int b[] = new int[a.length];
System.arraycopy(a, 2, b, 3, 4);
for(int i =0; i< b.length; i++) {
System.out.print(b[i]+"\t");
}
/**
* If you want to copy array partially or fully
* use array copy method
*/
System.out.println("\n using Arrays.copyOf() method ");
int c[] = Arrays.copyOf(a, a.length);
for(int i =0; i< c.length; i++) {
System.out.print(c[i]+"\t");
}
/**
* If you want to copy array partially or fully
* use array copy method
*/
System.out.println("\n rusing Arrays.copyOfRange() method ");
int e[] = Arrays.copyOfRange(a, 2 ,a.length);
for(int i =0; i< e.length; i++) {
System.out.print(e[i]+"\t");
}
}
}
Output
using clone method
8 2 8 1 8 0 8 0 2 9
using System.arrayCopy() method
0 0 0 8 1 8 0 0 0 0
using Arrays.copyOf() method
8 2 8 1 8 0 8 0 2 9
rusing Arrays.copyOfRange() method
8 1 8 0 8 0 2 9
You can use ArrayUtils.clone from apache commons
int arr[] = ArrayUtils.clone(source_array);
All the methods discussed above performs shallow copy so they are suitable for immutable objects and primitive data types.
If you want to copy a mutable object go for deep copy ( Create a new memory for object while copying )
email us at itatcalicut(at)gmail(dot)com
Follow this link to join my WhatsApp group: https://chat.whatsapp.com/HR0H7MG7AeA2PMApmfEeZn
Description
If you are going behind performance dont use clone method, as the clone method works on Object class, there will be internal type casting to datatypesYou can use ArrayUtils.clone from apache commons
int arr[] = ArrayUtils.clone(source_array);
All the methods discussed above performs shallow copy so they are suitable for immutable objects and primitive data types.
If you want to copy a mutable object go for deep copy ( Create a new memory for object while copying )
email us at itatcalicut(at)gmail(dot)com
Follow this link to join my WhatsApp group: https://chat.whatsapp.com/HR0H7MG7AeA2PMApmfEeZn
No comments:
Post a Comment
Your feedback may help others !!!