Share & learn

Through Innovative Digital Library

How to make copy of an array in Java?

Write a code in java to make copy of an array?
 



Answers

Posted by Paul on April 30th 2018 14:01 PM

There are multiple ways of making a copy of array in Java : 1. System.arraycopy(): This method provides you an easy way to specify the total number of elements to copy and the source and destination array index positions. e.g. System.arraycopy(source, 3, destination, 2, 5) will copy 5 elements from source to destination, beginning from 3rd index of source to 2nd index of destination. 2. Object.clone(): clone() method is provided by object and as array in java is also an Object, you can make use of this method in order to get the full array copy. 3. Arrays.copyOfRange(): This method is helpful where you need few elements of an array to be copied, where starting index is not 0, you can use this method to copy partial array. 4. Arrays.copyOf(): When you want to copy first few elements of an array or full copy of array, you can use this method. Let's implement these methods :

// Java program to copy array
import java.util.Arrays;

public class 
CopyArrayDemo {

    public static 
void main(String[] args) {
        
int[] arr = { 10203040506070 };
        
int[] arr1 = { 102230 };
        
int[] copy_arr null;

        
System.out.println("Original Array : " Arrays.toString(arr));

        
copy_arr copyFirstFiveFieldsOfArrayUsingSystem(arr);
        
System.out
                
.println("Copy First five elements of array if available. Result array : " Arrays.toString(copy_arr));

        
copy_arr copyFirstFiveFieldsOfArrayUsingSystem(arr1);
        
System.out
                
.println("Copy First five elements of array if available. Result array : " Arrays.toString(copy_arr));

        
copy_arr copyFullArrayUsingSystem(arr);
        
System.out.println(
                
"Copy full array using System.copyarray() function. Result array : " Arrays.toString(copy_arr));

        
copy_arr copyFullArrayUsingClone(arr);
        
System.out.println("Copy full array using clone() function. Result array : " Arrays.toString(copy_arr));

        
copy_arr copyFullArrayUsingArrayCopyOf(arr);
        
System.out
                
.println("Copy full array using Arrays.copyOf() function. Result array : " Arrays.toString(copy_arr));

        
copy_arr copyLastThree(arr);
        
System.out.println("Copy last three elements using Arrays.copyOfRange() function. Result array = "
                
Arrays.toString(copy_arr));
    }

    
/**
     * Arrays.copyOf() method to copy all the elements of an array
     */
    
private static int[] copyFullArrayUsingArrayCopyOf(int[] arr) {
        return 
Arrays.copyOf(arrarr.length);
    }

    
/**
     * This method copy partial array (last 3 elements) using
     * Arrays.copyOfRange() function
     */
    
private static int[] copyLastThree(int[] arr) {
        
        return 
Arrays.copyOfRange(arrarr.length 3arr.length);
    }

    
/**
     * This method copy full array using clone() function
     */
    
private static int[] copyFullArrayUsingClone(int[] arr) {
        return 
arr.clone();
    }

    
/**
     * This method copy full array using System.arraycopy() function
     */
    
private static int[] copyFullArrayUsingSystem(int[] arr) {
        
int[] temp = new int[arr.length];
        
System.arraycopy(arr0temp0arr.length);
        return 
temp;
    }

    
/**
     * This method copy full first five elements of array using
     * System.arraycopy() function
     */
    
private static int[] copyFirstFiveFieldsOfArrayUsingSystem(int[] arr) {
        if (
arr.length 5) {
            
int[] temp = new int[5];
            
System.arraycopy(arr0temp05);
            return 
temp;
        } else {
            
int[] temp = new int[arr.length];
            
System.arraycopy(arr0temp0arr.length);
            return 
temp;
        }

    }

}
Answer Question

Your email address will not be published. Required fields are marked *

  • Minimum 20 character
MM9H

related questions

Most liked questions

Most answered questions

Login
MM9H Refresh
Register
MM9H Refresh
Forgot password
Ask a Question
Minimum 20 character
Minimum 20 character
Reply
Minimum 20 character
MM9H Refresh