logo

Java Array Clone

Mange gange er vi nødt til at klone et array for at tage backup af de originale elementer i det. Vi har nogle specielle strenge og numre som palindromnummer, palindromstreng og Armstrong-nummer, og for at kontrollere deres specialitet skal vi klone arrayet. For at kontrollere, om en streng for eksempel er et palindrom eller ej, konverterer vi først en streng til et tegnarray og kloner char-arrayet som et temp. Nu vender vi elementerne i char-arrayet om og sammenligner det med den temp, der indeholder den originale streng.

Med enkle ord skal vi klone arrayet, når vi vil sikkerhedskopiere de originale data. I Java har vi fire måder at klone et array på, som er som følger:

Ved at kopiere array-elementer

Det er den naive måde at klone et array på. I denne metode itererer vi det originale array og sætter hvert element i arrayet i et andet array. Vi kloner arrayet ved at kopiere elementer på følgende måde:

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

Lad os implementere koden for at klone et array ved at bruge Objects clone() metode:

CloneArrayExample2.java

kruskal algoritme
 //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;>

Her, src definerer kildearrayet, srcPos definerer indekset, hvorfra kopiering skal startes, Start definerer arrayet, hvori elementer vil blive kopieret, destPos definerer indekset, hvorfra de kopierede elementer placeres i klonarrayet, og længde er størrelsen på den undergruppe, der skal kopieres.

Lad os implementere koden til at klone et array ved at bruge System.arraycopy() metoden:

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

Her, arr definerer det originale array, og kun er længden af ​​det array, der skal kopieres.

Lad os implementere koden for at klone et array ved at bruge arraycopy() metoden i Arrays-klassen:

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

Her, den oprindelige definerer det originale array, fra definerer indledende indeks og til definerer elementets endelige indeks.

Lad os implementere koden til at klone et array ved at bruge arraycopyRange() metoden i Arrays-klassen:

streng til dato konverter

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