You can try using System.arraycopy()
int[] src = new int[]{1,2,3,4,5};
int[] dest = new int[5];
System.arraycopy( src, 0, dest, 0, src.length );
But, probably better to use array.clone() in most cases:
int[] src = ...
int[] dest = src.clone();
Answer from Bala R on Stack OverflowIIT Kanpur
iitk.ac.in › esc101 › 05Aug › tutorial › java › data › copyingarrays.html
Copying Arrays
Recall that array indices start at 0, so that the copy begins at the array element 'c'. The arraycopy method call puts the copied elements into the destination array beginning at the first element (element 0) in the destination array copyTo.
Top answer 1 of 11
649
You can try using System.arraycopy()
int[] src = new int[]{1,2,3,4,5};
int[] dest = new int[5];
System.arraycopy( src, 0, dest, 0, src.length );
But, probably better to use array.clone() in most cases:
int[] src = ...
int[] dest = src.clone();
2 of 11
250
you can use
int[] a = new int[]{1,2,3,4,5};
int[] b = a.clone();
as well.
Videos
Arrays & methods & the right (and wrong) way to copy an array; ...
Arrays Copy | Ejercicios Java
10:40
JAVA: Copiar y clonar Arrays ☕ DAM - DAW - YouTube
00:56
How can you duplicate an array? - Cracking the Java Coding Interview ...
04:15
How to Copy an Array in Java By Example - Learn Java Programming ...
11:12
Java Program Copying Array Elements|Copy Elements of One Array ...
Emory
cs.emory.edu › ~cheung › Courses › 170 › Syllabus › 09 › copy-array.html
Copying an array and changing the size of an array
Complete Java example: Output of this program: Conclusion: array b is a · independent and a true copy of the · array b. Example Program: (Demo above code)                                                 · Prog file: click here ·
Programiz
programiz.com › java-programming › copy-arrays
Java Copy Arrays (Using System arraycopy(), Looping construct and so on)
In Java, the System class contains a method named arraycopy() to copy arrays.
DataCamp
datacamp.com › doc › java › copyof
Java Array copyOf()
In this example, Arrays.copyOf() is used to create an exact copy of originalArray. The new array copiedArray contains all elements from the original array. import java.util.Arrays; public class CopyOfLargerExample { public static void main(String[] args) { int[] originalArray = {1, 2, 3}; int[] extendedArray = Arrays.copyOf(originalArray, 5); System.out.println("Extended Array: " + Arrays.toString(extendedArray)); } }
Scaler
scaler.com › home › topics › array copy in java
Array Copy in Java - Scaler Topics
December 27, 2023 - Copying one element at a time while iterating through every element of the original array provided. Using this method ensures that any changes to b won't affect the initial array a, as seen in the example below follows this example we, are going to check how to copy an array in java using for loop
Tutorialspoint
tutorialspoint.com › java › lang › system_arraycopy.htm
Java System arraycopy() Method
The Java System arraycopy() method copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. A subsequence of array components are copied from the source array
DEV Community
dev.to › luthfisauqi17 › multiple-ways-to-copy-array-in-java-effectively-4lpj
Multiple Ways to Copy Array in Java Effectively - DEV Community
September 17, 2022 - As Output 4 shows, array b will not change if there is an update to any of the values in array a. Congratulations, you have successfully copied the value of array a to array b! But we will not stop here, because it turns out that Java has made it easy to copy array values to other arrays without having to use the iterative method.
TutorialsPoint
tutorialspoint.com › array-copy-in-java
Array Copy in Java
September 13, 2023 - public class Tester { public static void main(String args[]) { //Scenario 1: Copy array using assignment int[] a = {1, 2, 3}; int[] b = a; //test side effect b[0]++; System.out.println("Scenario 1: "); System.out.print("Array a: "); printArray(a); System.out.print("Array b: "); printArray(b); //Scenario 2: Copy array by iterating int[] c = {1, 2, 3}; int[] d = new int[c.length]; for (int i = 0; i < d.length; i++) { d[i] = c[i]; } //test side effect d[0]++; System.out.println("Scenario 2: "); System.out.print("Array c: "); printArray(c); System.out.print("Array d: "); printArray(d); //Scenario
Baeldung
baeldung.com › home › java › java array › performance of system.arraycopy() vs. arrays.copyof()
Performance of System.arraycopy() vs. Arrays.copyOf() | Baeldung
January 8, 2024 - When estimating the performance of System.arraycopy(), we need to keep in mind that it is a native method. Native methods are implemented in platform-dependent code (typically C) and accessed through JNI calls. Because native methods are already compiled for a specific architecture, we can’t precisely estimate the runtime complexity. Moreover, their complexities can differ between platforms. We can be sure that the worst-case scenario is O(N). However, the processor can copy contiguous blocks of memory one block at a time (memcpy() in C), so actual results can be better.
Tutorjoes
tutorjoes.in › Java_example_programs › copy_the_elements_of_one_array_into_another_array_in_java
Write a program to copy the elements of one array into another array
import java.util.Scanner; class Array_Copy { public static void main(String[] args) { Scanner input =new Scanner(System.in); System.out.print("Enter the Array Limit :"); int l =input.nextInt(); int [] a =new int[l]; int [] c =new int[l]; int sum = 0; for(int i=0;i<l;i++) { System.out.printf("Element of a[%d] :",i); a[i]=input.nextInt(); } for(int i=0;i<l;i++) { c[i] = a[i]; } System.out.print("\nOriginal Array Elements..."); for(int i=0;i<l;i++) { System.out.printf("\na[%d] = %d",i,a[i]); } System.out.print("\n\nCopy Array Elements one to Another Array..."); for(int i=0;i<l;i++) { System.out.printf("\nc[%d] = %d",i,c[i]); } } }
Career Karma
careerkarma.com › blog › java › 4 ways to copy an array in java
4 Ways to Copy an Array in Java: The Complete Guide | Career Karma
December 1, 2023 - This needs to be a deep copy because we plan on changing the contents of the summer_coffees array to reflect the new coffees we will offer in the summer months. Here’s the code we would use to create a deep copy using a for loop: import java.util.Arrays; class LoopCopy { public static void main(String[] args) { String[] coffees = {"Espresso", "Mocha", "Latte", "Cappuccino", "Pour Over", "Flat White"}; String[] summer_coffees = new String[6]; for (int i = 0; i < coffees.length; ++i) { summer_coffees[i] = coffees[i]; } System.out.println(Arrays.toString(summer_coffees)); } }
Software Testing Help
softwaretestinghelp.com › home › java › java copy array: how to copy / clone an array in java
Java Copy Array: How To Copy / Clone An Array In Java
April 1, 2025 - Here we have modified the previous program to include for loop and inside for loop, we assign each element of intArray to the corresponding element of copyArray. This way, the elements are actually copied. So when one array is modified, the changes do not reflect in another array. Java’s System class has a method called “ArrayCOpy” that allows you to copy elements of one array to another array.