×

Java Clone Array

We frequently need to copy an array to back up its original components. We have a few unique numbers and strings, including Armstrong numbers, palindrome numbers, and palindrome strings. To test their uniqueness, we must clone the array. For instance, to determine whether or not a string is a palindrome, we first transform the string into a character array and create a temporary copy of the char array. The char array's elements are now reversed and compared to the temporary file containing the original string.

Using Array Elements to Copy

It is a simple-minded method of array cloning. This approach involves iterating the initial array and putting each piece into a different array.

CloneArray1.java

// import packages   
import java. Util. Scanner ;  
public class CloneArray1 {  
    // Main method where execution of the program starts
    public static void main ( String [ ] args )  
    {  
         int original [ ] ;  
         int clone [ ] ;  
         int s ;  
         Scanner sc = new Scanner ( System . in) ;  
         System . out. println ( " Enter Size of the array " ) ;  
         s = sc . nextInt ( ) ;  
         original = new int [ s ] ;  
         clone = new int [ s ] ;  
         System . out . println ( " Elements of the original array is " ) ;  
         for ( int i = 0 ; i < s ; i++ )
         {  
             original [i] = sc . nextInt ( ) ;  
         }  
         sc . close ( ) ;  
         for ( int i = 0 ; i < original . length ; i++ )
         {  
             clone [ i ] = original [ i] ;  
         }    
         System . out . println ( " Elements of the original array: " ) ;  
         for (int i = 0 ; i < original . length ; i++ )
         {  
             System . out . print ( original [ i ] + "  " ) ;  
         }  
         System . out . println ( " \n \n Elements of the clone array : " ) ;  
         for (int i = 0; i < clone . length ; i++ ) 
         {  
             System . out . print ( clone [ i ] + " " ) ;  
         }  
    }  
}

Output

Java Clone Array

Java clone() method

The original array was iterated in the function above to create a copy of it. The clone() function of the Object class is another option that may be used to duplicate the provided array quickly.

Clone2.java

//import packages 
import java . util . Scanner ;  
public class Clone2 {  
    public static void main ( String [ ] args )  
    {  
         int original [ ] ;  
         int clone [ ] ;  
         int s ;  
         Scanner sc = new Scanner ( System . in ) ;  
         System . out . println ( " size of the array is " ) ;  
         s = sc . nextInt ( ) ;  
         original = new int [ s ] ;  
         clone = new int [ s ] ;  
         System . out . println ( " Enter elements of the original array : " ) ;  
         for ( int k = 0 ; k < s ; k++ ) {  
             original [ k ] = sc . nextInt ( ) ;  
         }  
         sc . close ( ) ;  
         clone = original . clone ( ) ;  
         System . out . println ( " Elements of the original array : " ) ;  
         for ( int j = 0 ; j < original . length ; j++ ) {  
             System . out . print ( original [ j ] + " " ) ;  
         }    
         System . out . println ( " \n\n Elements of the clone array: " ) ;  
         for ( int k = 0 ; k < clone . length ; k++ ) {  
             System . out . print ( clone [ k ] + " " ) ;  
         }  
    }  
}

Output

Java Clone Array

Java arraycopy() method 

The arraycopy() function may be found in the System class in the java.lang.* package. This technique copies a specific area of one array into another array. Copies an array, starting at the specified position in the source array, to the specified position in the destination array. A subsequence of array components are transferred from the source array identified by src to the destination array indicated by dest.

Program for clone array using arraycopy() method

ArrayCopy.java

//import  packages 
import java . util . *;  
public class ArrayCopy {  
    // Main section where execution of the program starts 
    public static void main(String[] args)  
    {  
        // declaring array variables
         int original[];  
         int clone[];  
         // size of the array
         int s;  
         // creating object for scanner class
         Scanner sc = new Scanner(System.in);  
         System . out . println ( " size of the array is " ) ;  
         s = sc . nextInt ( ) ;  
         original = new int[s];  
         clone = new int[s];  
         System . out . println ( " the  array elements are " ) ;  
         for ( int k = 0 ; k < s ; k++ )
         {  
             original [ k ] = sc . nextInt ( ) ;  
         }  
         // closing the scanner class
         sc . close ( ) ;  
         System . arraycopy ( original , 0 , clone , 0 , s ) ;  
         System . out . println ( " the original array elements are " ) ;  
         for (int j = 0 ; j < original . length ; j++ )
         {  
             System . out . print ( original [ j ] + " " ) ;  
         }  
         System . out . println ( "\n Elements of  clone array :" ) ;  
         for ( int k = 0 ; k < clone . length ; k++ ) 
         {  
             System . out . print ( clone [ k ] + " " ) ;  
         }  
    }  
}  

Output

Java Clone Array

Java CopyOf() Method

The copyOf() function of the Arrays class, found in the java.util package, is comparable to the clone() and arraycopy() methods.Here, len designates the length of the array that will be copied, while arr designates the original array.

Program for clone array using copyOf() method

CopyOf.java

//import packages
import java.util.*;  
public class CopyOf {  
    public static void main(String[] args)  
    {  
         int original[];  
         int clone[];  
         int s;  
         Scanner sc = new Scanner(System.in);  
         System . out . println ( " size of array  the array is" ) ;  
         s = sc . nextInt ( ) ;  
         original = new int [ s ] ;  
         clone = new int [ s ] ;  
         System . out . println ( " the original array elements are " ) ;  
         for ( int k = 0 ; k < s ; k++ ) 
         {  
             original [ k ] = sc . nextInt ( ) ;  
         }  
         sc.close();  
         clone = Arrays . copyOf ( original , s ) ;  
         System . out . println ( " The original array  elements are : " ) ;  
         for ( int j = 0 ; j < original . length ; j++ )
         {  
             System . out . print ( original [ j ] + " " ) ;  
         }  
         System . out . println ( " The cloned array elements are " ) ;  
      
         for  ( int j = 0 ; j  <  clone . length ; j++ ) {  
             System . out . print (clone [ j ] + " " ) ;  
         }  
    }  
}  

Output

Java Clone Array

Java copyOfRange() Method

Arrays class has copyOfRange() function to clone an array, which is similar to copyOf() method. The original array's elements from the specified range are copied into the new array using the copyOfRange() function. Here, the element's starting and end indexes are defined.

Program for clone array using copyOfRange() method

CopyOfRange.java

//import packages  
import java.util.Scanner;  
import java.util.Arrays;  
public class CopyOfRange {  
    public static void main(String[] args)  
    {  
         int original [ ] ;  
         int clone [ ] ;  
         int s ;  
         Scanner sc = new Scanner(System.in);  
         System . out . println ( " Enter  array size ." ) ;  
         s = sc.nextInt ( ) ;  
         original = new int [ s ] ;  
         clone = new int [ s ] ;  
         System . out . println ( " The original array elements are " ) ;  
         for ( int j = 0 ; j < s ; j++ ) {  
             original [ j ] = sc . nextInt ( ) ;  
         }  
         clone = Arrays.copyOfRange ( original , 0 , s ) ;  
         System . out . println ( " Elements of  original array: " ) ;  
         for ( int j = 0 ; j < original . length ; j++ )
         {  
             System . out . print (original [ j ] + " " ) ;  
         }  
         System . out . println ( " \n New  cloned array elements are : " ) ;  
         for ( int k = 0 ; k < clone . length ; k++ )
         {  
             System . out . print ( clone [ k ] + " " ) ;  
         }  
    }  
}

Output

Java Clone Array

Related Topics

Relatively Prime in Java

In this article, you will be very well equipped with a knowledge of a relatively prime number and also Java programs to determine whether a given integer is a relatively...

4 minutes read.

Prime Points in Java

The points that divide an integer into two halves containing a prime number are known as prime points. Printing every prime point of a specific number is the task. Let's...

6 minutes read.

Java Integer shortValue() method

The shortValue() method of Java Integer class returns a short value for this Integer after a narrowing primitive conversion. Syntax public short shortValue () Parameters NA Overrrides: The shortValue () overrides : shortValue in class Number  Return Value This...

1 minute read.

Decagonal Numbers in Java

This section explains what is a decagonal number and how to write Java programmes that compute decagonal numbers. Both academics and Java programmer interviews regularly question about the Decagonal number...

3 minutes read.

Java file Reader

File Reader: It is used to read the data from files. This class inherits the properties from Input Stream Reader Class. File Reader is for reading characters from the file. Input Stream: Java.io...

4 minutes read.

How to Compare Two Strings in Java

How to Compare Two Strings in Java On the basis of reference or content, one can compare two strings in Java. String comparison is used in reference matching (== operator), sorting...

4 minutes read.

Java throws

Java throws: The Java throws keyword is used with the signature of the method to indicate that the method may raise an exception. The method that uses the Java throws...

3 minutes read.

Java Substring

What is a substring in Java? Here by the name  " substring " itself, we can easily come to know it is a part of a string or a subset of...

4 minutes read.

Java Code Optimization

We encounter the idea of optimization while working on any Java application. It is essential that the code we write is not only clear and error-free but also optimized, meaning...

9 minutes read.

Java Math atan2() Method

The atan2() method of Math class returns an angle theta from the conversion of rectangular coordinates to polar coordinates. Syntax: public static double atan2(double y, double x) Parameters: The parameter ‘y’ represents the ordinate...

3 minutes read.

Check whether Java is installed or not

As we know that there are various operating systems, to check whether Java is installed or not in Windows and Mac we use the following ways.           Windows Operating System: There are several...

2 minutes read.

Tilt operator in Java | Tilde operator in Java Example

The symbol designates it as a unary operator (pronounced as the tilde). It gives back the bit's complement or inverse. Every 0 turns into a 1, and every 1 back...

3 minutes read.

What is the ambiguity problem in Java?

The ambiguity problem in Java occurs when a method or constructor is overloaded with two or more methods with the same name but different parameters. This can confuse when multiple...

6 minutes read.

Delete a Cycle from a Linked List in Java

Assume a method detectAndRemoveLoop(), which examines if a given Linked List includes a loop and, if so, eliminates this Loop and returns true. This returns false if the list does...

7 minutes read.

Java Char Keyword

The java char keyword is a data type which is defined as character data type.Char keyword belongs to a primitive data type where the data types are classified into primitive...

4 minutes read.

Automorphic Number Program in Java

We will learn about automorphic numbers through examples in this article, and we'll also make Java programmes that can determine whether a specific number was automorphic or not. What is an...

3 minutes read.

Convert IP to Binary in Java

Fundamental conversion, such as going from binary to decimal or vice versa, is a crucial activity in computers. Understanding IP addressing and subnetting is crucial for networking. The primary networking...

4 minutes read.

Java Delete File

There are two techniques to erase a record in Java: Utilizing File.delete() technique.Utilizing File.deleteOnExit() technique. Using File.delete() technique: In Java, we can erase a document by utilizing the File.delete() technique for File class....

2 minutes read.

Find next greater number with same set of digits in Java

It contains a number (num). Finding the smallest number that has the same number of elements as num that is also larger than num is the task at hand. If...

8 minutes read.

Java Serialization

JAVA SERIALIZATION Serialization is a process by which objects can be represented as a sequence of bytes. These bytes have information about object's data, object's type and datatypes of members in...

3 minutes read.