logo

Java Array Clone

De multe ori, trebuie să clonăm o matrice pentru a face backup elementelor originale ale acesteia. Avem câteva șiruri și numere speciale, cum ar fi numărul palindrom, șirul palindrom și numărul Armstrong, și pentru a verifica specialitatea lor, trebuie să clonăm matricea. De exemplu, pentru a verifica dacă un șir este un palindrom sau nu, mai întâi convertim un șir într-o matrice de caractere și clonăm tabloul char ca temp. Acum, inversăm elementele matricei char și o comparăm cu temperatura care conține șirul original.

Cu cuvinte simple, trebuie să clonăm matricea atunci când dorim să facem o copie de rezervă a datelor originale. În Java, avem patru moduri de a clona o matrice, care sunt după cum urmează:

Prin copierea elementelor de matrice

Este modalitatea naivă de a clona o matrice. În această metodă, repetăm ​​matricea originală și punem fiecare element al matricei într-o altă matrice. Clonăm matricea prin copierea elementelor în felul următor:

CloneArrayExample1.java

 // import required classes and packages if any import java.util.Scanner; // create class CloneArrayExample1 to clone an array public class CloneArrayExample1 { // main() method start public static void main(String[] args) { // declare originalArray and cloneArray variables int originalArray[]; int cloneArray[]; int size; // create Scanner class object to take input from user Scanner sc = new Scanner(System.in); System.out.println(&apos;Enter the size of the array.&apos;); size = sc.nextInt(); // initialize both the array with the given size originalArray = new int[size]; cloneArray = new int[size]; System.out.println(&apos;Enter elements of the original array:&apos;); //take input from user to fill originalArray for(int i = 0; i <size; i++) { originalarray[i]="sc.nextInt();" } close scanner class object sc.close(); use for loop to copy elements from originalarray clonearray (int i="0;" < originalarray.length; clonearray[i]="originalArray[i];" display of the original array system.out.println('elements array:'); system.out.print(originalarray[i] + ' '); cloned system.out.println('

elements clone clonearray.length; system.out.print(clonearray[i] pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/18/java-array-clone.webp" alt="Java Array Clone"> <h3>Using clone() Method</h3> <p>In the above method, we have iterated the original array to make a clone of it. We have another way that takes less time to clone the given array, i.e., <strong>the clone()</strong> method of the Object class. The syntax of the clone() method is as follwos:</p> <pre> protected Object clone() throws CloneNotSupportedException </pre> <p>Let&apos;s implement the code to clone an array by using Object&apos;s clone() method:</p> <p> <strong>CloneArrayExample2.java</strong> </p> <pre> //import required classes and packages if any import java.util.Scanner; //create class CloneArrayExample2 to clone an array using clone() method public class CloneArrayExample2 { // main() method start public static void main(String[] args) { // declare originalArray and cloneArray variables int originalArray[]; int cloneArray[]; int size; // create Scanner class object to take input from user Scanner sc = new Scanner(System.in); System.out.println(&apos;Enter the size of the array.&apos;); size = sc.nextInt(); // initialize both the array with the given size originalArray = new int[size]; cloneArray = new int[size]; System.out.println(&apos;Enter elements of the original array:&apos;); //take input from user to fill originalArray for(int i = 0; i <size; i++) { originalarray[i]="sc.nextInt();" } close scanner class object sc.close(); use clone() method of to clone originalarray clonearray="originalArray.clone();" display elements the original array system.out.println('elements array:'); for (int i="0;" < originalarray.length; system.out.print(originalarray[i] + ' '); cloned system.out.println('

elements clonearray.length; system.out.print(clonearray[i] pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/18/java-array-clone-2.webp" alt="Java Array Clone"> <h3>Using arraycopy() Method</h3> <p>Just like the clone() method, we can also use the arraycopy() method of the System class available in the <strong>java.lang</strong> package. The arraycopy() method has the following syntax:</p> <pre> public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) </pre> <p>Here, <strong>src</strong> defines the source array, <strong>srcPos</strong> defines the index from where copying should be started, <strong>dest</strong> defines the array in which elements will be copied, <strong>destPos</strong> defines the index from which the copied elements are placed in the clone array, and <strong>length</strong> is the size of the subarray to be copied.</p> <p>Let&apos;s implement the code to clone an array by using the System.arraycopy() method:</p> <p> <strong>CloneArrayExample3.java</strong> </p> <pre> //import required classes and packages if any import java.util.Scanner; //create class CloneArrayExample3 to clone an array using System.arraycopy() method public class CloneArrayExample3 { // main() method start public static void main(String[] args) { // declare originalArray and cloneArray variables int originalArray[]; int cloneArray[]; int size; // create Scanner class object to take input from user Scanner sc = new Scanner(System.in); System.out.println(&apos;Enter the size of the array.&apos;); size = sc.nextInt(); // initialize both the array with the given size originalArray = new int[size]; cloneArray = new int[size]; System.out.println(&apos;Enter elements of the original array:&apos;); //take input from user to fill originalArray for(int i = 0; i <size; i++) { originalarray[i]="sc.nextInt();" } close scanner class object sc.close(); use system.arraycopy() method to clone originalarray system.arraycopy(originalarray, 0, clonearray, size); display elements of the original array system.out.println('elements array:'); for (int i="0;" < originalarray.length; system.out.print(originalarray[i] + ' '); system.out.println('

elements clonearray.length; system.out.print(clonearray[i] pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/18/java-array-clone-3.webp" alt="Java Array Clone"> <h3>Using copyOf() Method</h3> <p>Just like clone() and arraycopy() methods, we can also use copyOf() method of the Arrays class available in the <strong>java.util</strong> package. The copyOf () method has the following syntax:</p> <pre> public static int[] copyOf(int[] arr, int len) </pre> <p>Here, <strong>arr</strong> defines the original array, and <strong>len</strong> is the length of the array to get copied.</p> <p>Let&apos;s implement the code to clone an array by using arraycopy() method of Arrays class:</p> <p> <strong>CloneArrayExample4.java</strong> </p> <pre> //import required classes and packages if any import java.util.Scanner; import java.util.Arrays; //create class CloneArrayExample4 to clone an array using arraycopy() method of Arrays class public class CloneArrayExample4 { // main() method start public static void main(String[] args) { // declare originalArray and cloneArray variables int originalArray[]; int cloneArray[]; int size; // create Scanner class object to take input from user Scanner sc = new Scanner(System.in); System.out.println(&apos;Enter the size of the array.&apos;); size = sc.nextInt(); // initialize both the array with the given size originalArray = new int[size]; cloneArray = new int[size]; System.out.println(&apos;Enter elements of the original array:&apos;); //take input from user to fill originalArray for(int i = 0; i <size; i++) { originalarray[i]="sc.nextInt();" } close scanner class object sc.close(); use copyof() method to clone originalarray clonearray="Arrays.copyOf(originalArray," size); display elements of the original array system.out.println('elements array:'); for (int i="0;" < originalarray.length; system.out.print(originalarray[i] + ' '); system.out.println('

elements clonearray.length; system.out.print(clonearray[i] pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/18/java-array-clone-4.webp" alt="Java Array Clone"> <h3>Using copyOfRange() Method</h3> <p>Lust like copyOf() method, Arrays class provide copyOfRange() method to clone an array. The copyOfRange() method is used to copy the elements of the specified range of the original array into clone array. The syntax of the copyOfRange() method is as follows:</p> <pre> public static int[] copyOfRange(int[] original, int from, int to) </pre> <p>Here, <strong>the original</strong> defines the original array, <strong>from</strong> defines initial index and <strong>to</strong> defines the final index of the element.</p> <p>Let&apos;s implement the code to clone an array by using arraycopyRange() method of Arrays class:</p> <p> <strong>CloneArrayExample5.java</strong> </p> <pre> //import required classes and packages if any package javaTpoint.JavaExample; import java.util.Scanner; import java.util.Arrays; //create class CloneArrayExample5 to clone an array using copyOfRange() method of Arrays class public class CloneArrayExample5 { // main() method start public static void main(String[] args) { // declare originalArray and cloneArray variables int originalArray[]; int cloneArray[]; int size; // create Scanner class object to take input from user Scanner sc = new Scanner(System.in); System.out.println(&apos;Enter the size of the array.&apos;); size = sc.nextInt(); // initialize both the array with the given size originalArray = new int[size]; cloneArray = new int[size]; System.out.println(&apos;Enter elements of the original array:&apos;); //take input from user to fill originalArray for(int i = 0; i <size; i++) { originalarray[i]="sc.nextInt();" } close scanner class object sc.close(); use copyofrange() method to clone originalarray clonearray="Arrays.copyOfRange(originalArray," 0, size); display elements of the original array system.out.println('elements array:'); for (int i="0;" < originalarray.length; system.out.print(originalarray[i] + ' '); system.out.println('

elements clonearray.length; system.out.print(clonearray[i] pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/18/java-array-clone-5.webp" alt="Java Array Clone"> <p>All the above-discussed ways are used for cloning an array. We recommend you to use the clone() method of the Object class to clone the array. </p> <hr></size;></pre></size;></pre></size;></pre></size;></pre></size;>

Să implementăm codul pentru a clona o matrice utilizând metoda clone() a obiectului:

CloneArrayExample2.java

șir jsonobject
 //import required classes and packages if any import java.util.Scanner; //create class CloneArrayExample2 to clone an array using clone() method public class CloneArrayExample2 { // main() method start public static void main(String[] args) { // declare originalArray and cloneArray variables int originalArray[]; int cloneArray[]; int size; // create Scanner class object to take input from user Scanner sc = new Scanner(System.in); System.out.println(&apos;Enter the size of the array.&apos;); size = sc.nextInt(); // initialize both the array with the given size originalArray = new int[size]; cloneArray = new int[size]; System.out.println(&apos;Enter elements of the original array:&apos;); //take input from user to fill originalArray for(int i = 0; i <size; i++) { originalarray[i]="sc.nextInt();" } close scanner class object sc.close(); use clone() method of to clone originalarray clonearray="originalArray.clone();" display elements the original array system.out.println(\'elements array:\'); for (int i="0;" < originalarray.length; system.out.print(originalarray[i] + \' \'); cloned system.out.println(\'

elements clonearray.length; system.out.print(clonearray[i] pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/18/java-array-clone-2.webp" alt="Java Array Clone"> <h3>Using arraycopy() Method</h3> <p>Just like the clone() method, we can also use the arraycopy() method of the System class available in the <strong>java.lang</strong> package. The arraycopy() method has the following syntax:</p> <pre> public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) </pre> <p>Here, <strong>src</strong> defines the source array, <strong>srcPos</strong> defines the index from where copying should be started, <strong>dest</strong> defines the array in which elements will be copied, <strong>destPos</strong> defines the index from which the copied elements are placed in the clone array, and <strong>length</strong> is the size of the subarray to be copied.</p> <p>Let&apos;s implement the code to clone an array by using the System.arraycopy() method:</p> <p> <strong>CloneArrayExample3.java</strong> </p> <pre> //import required classes and packages if any import java.util.Scanner; //create class CloneArrayExample3 to clone an array using System.arraycopy() method public class CloneArrayExample3 { // main() method start public static void main(String[] args) { // declare originalArray and cloneArray variables int originalArray[]; int cloneArray[]; int size; // create Scanner class object to take input from user Scanner sc = new Scanner(System.in); System.out.println(&apos;Enter the size of the array.&apos;); size = sc.nextInt(); // initialize both the array with the given size originalArray = new int[size]; cloneArray = new int[size]; System.out.println(&apos;Enter elements of the original array:&apos;); //take input from user to fill originalArray for(int i = 0; i <size; i++) { originalarray[i]="sc.nextInt();" } close scanner class object sc.close(); use system.arraycopy() method to clone originalarray system.arraycopy(originalarray, 0, clonearray, size); display elements of the original array system.out.println(\'elements array:\'); for (int i="0;" < originalarray.length; system.out.print(originalarray[i] + \' \'); system.out.println(\'

elements clonearray.length; system.out.print(clonearray[i] pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/18/java-array-clone-3.webp" alt="Java Array Clone"> <h3>Using copyOf() Method</h3> <p>Just like clone() and arraycopy() methods, we can also use copyOf() method of the Arrays class available in the <strong>java.util</strong> package. The copyOf () method has the following syntax:</p> <pre> public static int[] copyOf(int[] arr, int len) </pre> <p>Here, <strong>arr</strong> defines the original array, and <strong>len</strong> is the length of the array to get copied.</p> <p>Let&apos;s implement the code to clone an array by using arraycopy() method of Arrays class:</p> <p> <strong>CloneArrayExample4.java</strong> </p> <pre> //import required classes and packages if any import java.util.Scanner; import java.util.Arrays; //create class CloneArrayExample4 to clone an array using arraycopy() method of Arrays class public class CloneArrayExample4 { // main() method start public static void main(String[] args) { // declare originalArray and cloneArray variables int originalArray[]; int cloneArray[]; int size; // create Scanner class object to take input from user Scanner sc = new Scanner(System.in); System.out.println(&apos;Enter the size of the array.&apos;); size = sc.nextInt(); // initialize both the array with the given size originalArray = new int[size]; cloneArray = new int[size]; System.out.println(&apos;Enter elements of the original array:&apos;); //take input from user to fill originalArray for(int i = 0; i <size; i++) { originalarray[i]="sc.nextInt();" } close scanner class object sc.close(); use copyof() method to clone originalarray clonearray="Arrays.copyOf(originalArray," size); display elements of the original array system.out.println(\'elements array:\'); for (int i="0;" < originalarray.length; system.out.print(originalarray[i] + \' \'); system.out.println(\'

elements clonearray.length; system.out.print(clonearray[i] pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/18/java-array-clone-4.webp" alt="Java Array Clone"> <h3>Using copyOfRange() Method</h3> <p>Lust like copyOf() method, Arrays class provide copyOfRange() method to clone an array. The copyOfRange() method is used to copy the elements of the specified range of the original array into clone array. The syntax of the copyOfRange() method is as follows:</p> <pre> public static int[] copyOfRange(int[] original, int from, int to) </pre> <p>Here, <strong>the original</strong> defines the original array, <strong>from</strong> defines initial index and <strong>to</strong> defines the final index of the element.</p> <p>Let&apos;s implement the code to clone an array by using arraycopyRange() method of Arrays class:</p> <p> <strong>CloneArrayExample5.java</strong> </p> <pre> //import required classes and packages if any package javaTpoint.JavaExample; import java.util.Scanner; import java.util.Arrays; //create class CloneArrayExample5 to clone an array using copyOfRange() method of Arrays class public class CloneArrayExample5 { // main() method start public static void main(String[] args) { // declare originalArray and cloneArray variables int originalArray[]; int cloneArray[]; int size; // create Scanner class object to take input from user Scanner sc = new Scanner(System.in); System.out.println(&apos;Enter the size of the array.&apos;); size = sc.nextInt(); // initialize both the array with the given size originalArray = new int[size]; cloneArray = new int[size]; System.out.println(&apos;Enter elements of the original array:&apos;); //take input from user to fill originalArray for(int i = 0; i <size; i++) { originalarray[i]="sc.nextInt();" } close scanner class object sc.close(); use copyofrange() method to clone originalarray clonearray="Arrays.copyOfRange(originalArray," 0, size); display elements of the original array system.out.println(\'elements array:\'); for (int i="0;" < originalarray.length; system.out.print(originalarray[i] + \' \'); system.out.println(\'

elements clonearray.length; system.out.print(clonearray[i] pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/18/java-array-clone-5.webp" alt="Java Array Clone"> <p>All the above-discussed ways are used for cloning an array. We recommend you to use the clone() method of the Object class to clone the array. </p> <hr></size;></pre></size;></pre></size;></pre></size;>

Aici, src definește matricea sursă, srcPos definește indexul de unde ar trebui să înceapă copierea, start definește matricea în care vor fi copiate elementele, destPos definește indexul din care sunt plasate elementele copiate în matricea clonelor și lungime este dimensiunea subbaryului de copiat.

Să implementăm codul pentru a clona o matrice utilizând metoda System.arraycopy():

CloneArrayExample3.java

 //import required classes and packages if any import java.util.Scanner; //create class CloneArrayExample3 to clone an array using System.arraycopy() method public class CloneArrayExample3 { // main() method start public static void main(String[] args) { // declare originalArray and cloneArray variables int originalArray[]; int cloneArray[]; int size; // create Scanner class object to take input from user Scanner sc = new Scanner(System.in); System.out.println(&apos;Enter the size of the array.&apos;); size = sc.nextInt(); // initialize both the array with the given size originalArray = new int[size]; cloneArray = new int[size]; System.out.println(&apos;Enter elements of the original array:&apos;); //take input from user to fill originalArray for(int i = 0; i <size; i++) { originalarray[i]="sc.nextInt();" } close scanner class object sc.close(); use system.arraycopy() method to clone originalarray system.arraycopy(originalarray, 0, clonearray, size); display elements of the original array system.out.println(\'elements array:\'); for (int i="0;" < originalarray.length; system.out.print(originalarray[i] + \' \'); system.out.println(\'

elements clonearray.length; system.out.print(clonearray[i] pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/18/java-array-clone-3.webp" alt="Java Array Clone"> <h3>Using copyOf() Method</h3> <p>Just like clone() and arraycopy() methods, we can also use copyOf() method of the Arrays class available in the <strong>java.util</strong> package. The copyOf () method has the following syntax:</p> <pre> public static int[] copyOf(int[] arr, int len) </pre> <p>Here, <strong>arr</strong> defines the original array, and <strong>len</strong> is the length of the array to get copied.</p> <p>Let&apos;s implement the code to clone an array by using arraycopy() method of Arrays class:</p> <p> <strong>CloneArrayExample4.java</strong> </p> <pre> //import required classes and packages if any import java.util.Scanner; import java.util.Arrays; //create class CloneArrayExample4 to clone an array using arraycopy() method of Arrays class public class CloneArrayExample4 { // main() method start public static void main(String[] args) { // declare originalArray and cloneArray variables int originalArray[]; int cloneArray[]; int size; // create Scanner class object to take input from user Scanner sc = new Scanner(System.in); System.out.println(&apos;Enter the size of the array.&apos;); size = sc.nextInt(); // initialize both the array with the given size originalArray = new int[size]; cloneArray = new int[size]; System.out.println(&apos;Enter elements of the original array:&apos;); //take input from user to fill originalArray for(int i = 0; i <size; i++) { originalarray[i]="sc.nextInt();" } close scanner class object sc.close(); use copyof() method to clone originalarray clonearray="Arrays.copyOf(originalArray," size); display elements of the original array system.out.println(\'elements array:\'); for (int i="0;" < originalarray.length; system.out.print(originalarray[i] + \' \'); system.out.println(\'

elements clonearray.length; system.out.print(clonearray[i] pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/18/java-array-clone-4.webp" alt="Java Array Clone"> <h3>Using copyOfRange() Method</h3> <p>Lust like copyOf() method, Arrays class provide copyOfRange() method to clone an array. The copyOfRange() method is used to copy the elements of the specified range of the original array into clone array. The syntax of the copyOfRange() method is as follows:</p> <pre> public static int[] copyOfRange(int[] original, int from, int to) </pre> <p>Here, <strong>the original</strong> defines the original array, <strong>from</strong> defines initial index and <strong>to</strong> defines the final index of the element.</p> <p>Let&apos;s implement the code to clone an array by using arraycopyRange() method of Arrays class:</p> <p> <strong>CloneArrayExample5.java</strong> </p> <pre> //import required classes and packages if any package javaTpoint.JavaExample; import java.util.Scanner; import java.util.Arrays; //create class CloneArrayExample5 to clone an array using copyOfRange() method of Arrays class public class CloneArrayExample5 { // main() method start public static void main(String[] args) { // declare originalArray and cloneArray variables int originalArray[]; int cloneArray[]; int size; // create Scanner class object to take input from user Scanner sc = new Scanner(System.in); System.out.println(&apos;Enter the size of the array.&apos;); size = sc.nextInt(); // initialize both the array with the given size originalArray = new int[size]; cloneArray = new int[size]; System.out.println(&apos;Enter elements of the original array:&apos;); //take input from user to fill originalArray for(int i = 0; i <size; i++) { originalarray[i]="sc.nextInt();" } close scanner class object sc.close(); use copyofrange() method to clone originalarray clonearray="Arrays.copyOfRange(originalArray," 0, size); display elements of the original array system.out.println(\'elements array:\'); for (int i="0;" < originalarray.length; system.out.print(originalarray[i] + \' \'); system.out.println(\'

elements clonearray.length; system.out.print(clonearray[i] pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/18/java-array-clone-5.webp" alt="Java Array Clone"> <p>All the above-discussed ways are used for cloning an array. We recommend you to use the clone() method of the Object class to clone the array. </p> <hr></size;></pre></size;></pre></size;>

Aici, arr definește matricea originală și numai este lungimea matricei de copiat.

Să implementăm codul pentru a clona o matrice utilizând metoda arraycopy() din clasa Arrays:

CloneArrayExample4.java

 //import required classes and packages if any import java.util.Scanner; import java.util.Arrays; //create class CloneArrayExample4 to clone an array using arraycopy() method of Arrays class public class CloneArrayExample4 { // main() method start public static void main(String[] args) { // declare originalArray and cloneArray variables int originalArray[]; int cloneArray[]; int size; // create Scanner class object to take input from user Scanner sc = new Scanner(System.in); System.out.println(&apos;Enter the size of the array.&apos;); size = sc.nextInt(); // initialize both the array with the given size originalArray = new int[size]; cloneArray = new int[size]; System.out.println(&apos;Enter elements of the original array:&apos;); //take input from user to fill originalArray for(int i = 0; i <size; i++) { originalarray[i]="sc.nextInt();" } close scanner class object sc.close(); use copyof() method to clone originalarray clonearray="Arrays.copyOf(originalArray," size); display elements of the original array system.out.println(\'elements array:\'); for (int i="0;" < originalarray.length; system.out.print(originalarray[i] + \' \'); system.out.println(\'

elements clonearray.length; system.out.print(clonearray[i] pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/18/java-array-clone-4.webp" alt="Java Array Clone"> <h3>Using copyOfRange() Method</h3> <p>Lust like copyOf() method, Arrays class provide copyOfRange() method to clone an array. The copyOfRange() method is used to copy the elements of the specified range of the original array into clone array. The syntax of the copyOfRange() method is as follows:</p> <pre> public static int[] copyOfRange(int[] original, int from, int to) </pre> <p>Here, <strong>the original</strong> defines the original array, <strong>from</strong> defines initial index and <strong>to</strong> defines the final index of the element.</p> <p>Let&apos;s implement the code to clone an array by using arraycopyRange() method of Arrays class:</p> <p> <strong>CloneArrayExample5.java</strong> </p> <pre> //import required classes and packages if any package javaTpoint.JavaExample; import java.util.Scanner; import java.util.Arrays; //create class CloneArrayExample5 to clone an array using copyOfRange() method of Arrays class public class CloneArrayExample5 { // main() method start public static void main(String[] args) { // declare originalArray and cloneArray variables int originalArray[]; int cloneArray[]; int size; // create Scanner class object to take input from user Scanner sc = new Scanner(System.in); System.out.println(&apos;Enter the size of the array.&apos;); size = sc.nextInt(); // initialize both the array with the given size originalArray = new int[size]; cloneArray = new int[size]; System.out.println(&apos;Enter elements of the original array:&apos;); //take input from user to fill originalArray for(int i = 0; i <size; i++) { originalarray[i]="sc.nextInt();" } close scanner class object sc.close(); use copyofrange() method to clone originalarray clonearray="Arrays.copyOfRange(originalArray," 0, size); display elements of the original array system.out.println(\'elements array:\'); for (int i="0;" < originalarray.length; system.out.print(originalarray[i] + \' \'); system.out.println(\'

elements clonearray.length; system.out.print(clonearray[i] pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/18/java-array-clone-5.webp" alt="Java Array Clone"> <p>All the above-discussed ways are used for cloning an array. We recommend you to use the clone() method of the Object class to clone the array. </p> <hr></size;></pre></size;>

Aici, originalul definește matricea originală, din definește indicele inițial și la definește indexul final al elementului.

Să implementăm codul pentru a clona o matrice utilizând metoda arraycopyRange() din clasa Arrays:

shilpa Shetty vârsta

CloneArrayExample5.java

 //import required classes and packages if any package javaTpoint.JavaExample; import java.util.Scanner; import java.util.Arrays; //create class CloneArrayExample5 to clone an array using copyOfRange() method of Arrays class public class CloneArrayExample5 { // main() method start public static void main(String[] args) { // declare originalArray and cloneArray variables int originalArray[]; int cloneArray[]; int size; // create Scanner class object to take input from user Scanner sc = new Scanner(System.in); System.out.println(&apos;Enter the size of the array.&apos;); size = sc.nextInt(); // initialize both the array with the given size originalArray = new int[size]; cloneArray = new int[size]; System.out.println(&apos;Enter elements of the original array:&apos;); //take input from user to fill originalArray for(int i = 0; i <size; i++) { originalarray[i]="sc.nextInt();" } close scanner class object sc.close(); use copyofrange() method to clone originalarray clonearray="Arrays.copyOfRange(originalArray," 0, size); display elements of the original array system.out.println(\'elements array:\'); for (int i="0;" < originalarray.length; system.out.print(originalarray[i] + \' \'); system.out.println(\'

elements clonearray.length; system.out.print(clonearray[i] pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/18/java-array-clone-5.webp" alt="Java Array Clone"> <p>All the above-discussed ways are used for cloning an array. We recommend you to use the clone() method of the Object class to clone the array. </p> <hr></size;>