logo

Clone dell'array Java

Molte volte è necessario clonare un array per eseguire il backup dei suoi elementi originali. Abbiamo alcune stringhe e numeri speciali come il numero palindromo, la stringa palindromo e il numero Armstrong e per verificare la loro specialità dobbiamo clonare l'array. Ad esempio, per verificare se una stringa è palindroma o meno, convertiamo prima una stringa in un array di caratteri e cloniamo l'array di caratteri come temp. Ora invertiamo gli elementi dell'array char e lo confrontiamo con il temp che contiene la stringa originale.

In parole semplici, dobbiamo clonare l'array quando vogliamo eseguire il backup dei dati originali. In Java, abbiamo quattro modi per clonare un array che sono i seguenti:

Copiando gli elementi dell'array

È il modo ingenuo di clonare un array. In questo metodo, iteriamo l'array originale e inseriamo ogni elemento dell'array in un altro array. Cloniamo l'array copiando gli elementi nel modo seguente:

CloneArrayEsempio1.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;>

Implementiamo il codice per clonare un array utilizzando il metodo clone() di Object:

CloneArrayExample2.java

python stampa a 2 cifre decimali
 //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;>

Qui, src definisce l'array di origine, srcPos definisce l'indice da cui iniziare la copia, inizio definisce l'array in cui verranno copiati gli elementi, destPos definisce l'indice da cui vengono posizionati gli elementi copiati nell'array clone e lunghezza è la dimensione del sottoarray da copiare.

Implementiamo il codice per clonare un array utilizzando il metodo System.arraycopy():

CloneArrayExample3.java

protocollo UDP
 //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;>

Qui, arr definisce l'array originale e soltanto è la lunghezza dell'array da copiare.

Implementiamo il codice per clonare un array utilizzando il metodo arraycopy() della classe 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;>

Qui, l'originale definisce l'array originale, da definisce l'indice iniziale e A definisce l'indice finale dell'elemento.

Implementiamo il codice per clonare un array utilizzando il metodo arraycopyRange() della classe Arrays:

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;>