logo

Java Array Clone

Много пъти трябва да клонираме масив, за да архивираме оригиналните елементи от него. Имаме някои специални низове и числа като палиндромно число, палиндромен низ и число на Армстронг и за да проверим тяхната специалност, трябва да клонираме масива. Например, за да проверим дали даден низ е палиндром или не, първо преобразуваме низ в масив от знаци и клонираме масива char като temp. Сега обръщаме елементите на масива char и го сравняваме с temp, който съдържа оригиналния низ.

С прости думи, трябва да клонираме масива, когато искаме да архивираме оригиналните данни. В Java имаме четири начина за клониране на масив, които са както следва:

Чрез копиране на елементи от масив

Това е наивният начин за клониране на масив. В този метод ние итерираме оригиналния масив и поставяме всеки елемент от масива в друг масив. Ние клонираме масива, като копираме елементи по следния начин:

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

Нека внедрим кода за клониране на масив с помощта на метода clone() на Object:

CloneArrayExample2.java

java string to int
 //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;>

Тук, src дефинира изходния масив, srcPos дефинира индекса, от който трябва да започне копирането, започнете дефинира масива, в който елементите ще бъдат копирани, destPos дефинира индекса, от който копираните елементи се поставят в масива за клонинг, и дължина е размерът на подмасива, който трябва да се копира.

Нека внедрим кода за клониране на масив с помощта на метода System.arraycopy():

CloneArrayExample3.java

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

Тук, обр дефинира оригиналния масив и само е дължината на масива, който трябва да бъде копиран.

Нека внедрим кода за клониране на масив с помощта на метода arraycopy() на класа 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;>

Тук, оригинала дефинира оригиналния масив, от определя начален индекс и да се дефинира крайния индекс на елемента.

Нека внедрим кода за клониране на масив, като използваме метода arraycopyRange() на класа 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;>