×

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

Java Integer equals() method

The equals() method of Integer class compares the given object to the specified object. Syntax public boolean equals(Object obj) Parameters The parameter ‘obj’ represents the object to be compared with. Overrides The equals() method overrides equals...

1 minute read.

BigDecimal toString() in Java

BigDecimal is a Java class that is a part of the java.math package and the java.base module. It implements the ComparableBigDecimal> interface and extends the Number class. The BigDecimal class...

3 minutes read.

How to remove last character from String in Java

In java, there are predominantly three classes connected with the string. The classes are String, StringBuilder, and StringBuffer class that gives techniques connected with string control. Eliminating the first and...

5 minutes read.

Java this keyword

This Keyword in Java This keyword can be used in many different ways in Java. This is a reference variable in Java that points to the active object. In Java, the...

8 minutes read.

Swapping Program in Java

Swapping Program in Java The swapping program in Java is used to interchange the values of the two variables. For example, if X = 12 and Y = 24, then the...

4 minutes read.

House Numbers in Java

In this section, we will discuss about house number in Java. It is a sum of cubes, each of which has a dimension of h + 1. There is a...

3 minutes read.

Java Enum vs Class

Enumerations are used in programming languages to represent collections of named constants. For instance, the four suits in a deck of playing cards might represent four integrators named Club, Diamond,...

7 minutes read.

Magic Square in Java

A square with numbers is referred to as a magic square. The numbers in a magic square of order n are arranged to ensure that the sum of all of...

4 minutes read.

Difference between Static and Instance Methods in Java

Static Method in Java The static technique has a place with the class as opposed to the object of the course. These are intended to be divided between every one of...

10 minutes read.

How to add 24 Hours to Date in Java?

In this tutorial, we will learn how to add 24 hours to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

Java Subtract Days from Current Date

Dealing with date and time in Java is not a particularly challenging operation because Java has an API for date and time that simplifies duties for developers. There are two...

3 minutes read.

Sylvester Sequence in Java

A Sylvester Sequence is a series of numbers in which each term is the sum of the terms before it plus 1. The sequence's first two terms are 2 and...

3 minutes read.

Segment Tree in Java

Binary trees can address a variety of issues; however, the Segment Tree is more efficient in terms of time complexity. The segment tree in Java is represented using an array. Native...

4 minutes read.

Java Integer toHexString() method

The toHexString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 16. Syntax public static String toHexString (int  i)  Parameters The parameter ‘i’ represents...

1 minute read.

Java Xml Parser

In this article, we acknowledge you about what is a XML parser in Java, how is it going to work and what is the purpose of it. Also, the article discusses...

6 minutes read.

Maximum length of string in java

In java, String can act as a data type and a class. The string can be defined as the collection of characters that are enclosed with double quotes(“ “). The...

3 minutes read.

Java Session

Session indicates interval of time. A session is a simple  time interval in which servers and client interacts. To maintain the state of the client or user we use technologies...

5 minutes read.

Types of Logical Operators in Java

In this tutorial, we are going to study logical operators. A logical operator is an operator that accomplishes a logical operation that is to connect two or more operations. These...

5 minutes read.

Minimum Difference Between Groups of Size Two in Java

There is given an array with various integers in it. The goal is to divide the elements into distinct groups, each of which has just two, so that the difference...

3 minutes read.

Java Wrapper classes

Java Wrapper classes A wrapper class is a class whose object contains a primitive data type; moreover it provides a way to use primitive data type (int, boolean, etc.) as objects. Wrapper...

2 minutes read.