logo

Java Array Clone

Mange ganger må vi klone en matrise for å sikkerhetskopiere de originale elementene i den. Vi har noen spesielle strenger og tall som palindromnummer, palindromstreng og Armstrong-nummer, og for å sjekke spesialiteten deres, må vi klone matrisen. For eksempel, for å sjekke om en streng er et palindrom eller ikke, konverterer vi først en streng til en tegnarray og kloner char-matrisen som en temp. Nå reverserer vi elementene i char-matrisen og sammenligner den med temperaturen som inneholder den opprinnelige strengen.

Med enkle ord må vi klone matrisen når vi vil sikkerhetskopiere de originale dataene. I Java har vi fire måter å klone en matrise på, som er som følger:

Ved å kopiere matriseelementer

Det er den naive måten å klone en array på. I denne metoden itererer vi den opprinnelige matrisen og legger hvert element i matrisen inn i en annen matrise. Vi kloner matrisen ved å kopiere elementer på følgende måte:

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

La oss implementere koden for å klone en matrise ved å bruke Objects clone()-metode:

CloneArrayExample2.java

java sorterer en liste
 //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 kildematrisen, srcPos definerer indeksen hvor kopieringen skal startes fra, start definerer matrisen som elementer skal kopieres i, destPos definerer indeksen som de kopierte elementene plasseres fra i klonematrisen, og lengde er størrelsen på undergruppen som skal kopieres.

La oss implementere koden for å klone en matrise ved å bruke 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 den opprinnelige matrisen, og bare er lengden på matrisen som skal kopieres.

La oss implementere koden for å klone en matrise ved å bruke arraycopy()-metoden til 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 opprinnelige definerer den opprinnelige matrisen, fra definerer innledende indeks og til definerer den endelige indeksen til elementet.

La oss implementere koden for å klone en matrise ved å bruke arraycopyRange()-metoden til Arrays-klassen:

hvis annet loop i java

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