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 Overflow 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.
IIT 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.
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
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 ...
00:56
How can you duplicate an array? - Cracking the Java Coding Interview ...
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.
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 ·
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.
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.
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
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)); } }
Quora
quora.com › What-makes-Javas-System-arraycopy-fast-Yes-Ive-read-that-its-a-native-method-but-what-makes-such-native-method-faster-than-using-a-for-loop-to-duplicate-an-array-Does-the-System-arraycopy-not-also-do-a-for-loop
What makes Java's System.arraycopy fast. Yes I've read that it's a native method but what makes such 'native method' faster than using a for-loop to duplicate an array? Does the System.arraycopy not also do a for-loop? - Quora
Answer (1 of 5): Native code is…native. It’s faster. It’s raw C or C++ code compiled down to the machine’s native instructions. This is faster than Java bytecode interpreted in a JVM. Also, Java code performs bounds-checking so that you get a nice little ArrayIndexOutOfBounds error ...