{"id":5284,"date":"2021-03-23T16:52:57","date_gmt":"2021-03-23T16:52:57","guid":{"rendered":"https:\/\/ded9.com\/?p=5284"},"modified":"2025-12-21T11:10:14","modified_gmt":"2025-12-21T11:10:14","slug":"how-to-copy-java-arrays-in-very-simple-language","status":"publish","type":"post","link":"https:\/\/ded9.com\/de\/how-to-copy-java-arrays-in-very-simple-language\/","title":{"rendered":"How to Copy Java Arrays in Very Simple Language"},"content":{"rendered":"<p><span style=\"font-size: 12pt;\">In this tutorial, you will learn about different methods that you can use to copy arrays (both one-dimensional and two-dimensional) in <a href=\"https:\/\/ded9.com\/what-is-object-orientation-in-java-and-how-to-use-it\/\">Java<\/a>.<\/span><span id=\"more-50114\"><\/span><\/p>\n<p>There are several techniques you can use to copy arrays in Java.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-257944 size-full\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/Java_Arrays_SingleORMultiDimensional.png\" alt=\"There are several techniques you can use to copy arrays in Java.\" width=\"2032\" height=\"1198\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/Java_Arrays_SingleORMultiDimensional.png 2032w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/Java_Arrays_SingleORMultiDimensional-300x177.png 300w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/Java_Arrays_SingleORMultiDimensional-1024x604.png 1024w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/Java_Arrays_SingleORMultiDimensional-768x453.png 768w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/Java_Arrays_SingleORMultiDimensional-1536x906.png 1536w\" sizes=\"(max-width: 2032px) 100vw, 2032px\" \/><\/p>\n<h2>Copy arrays using the assignment operator<\/h2>\n<p>Let&#8217;s take an example,<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class CopyArray {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">int [] numbers = {1, 2, 3, 4, 5, 6};<\/li>\n<li dir=\"ltr\">int [] positiveNumbers = numbers;\u00a0\/\/ copying arrays<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">for (int number: positiveNumbers) {<\/li>\n<li dir=\"ltr\">System.out.print (number + \u201c,\u201c);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>Output<\/p>\n<blockquote>\n<p dir=\"ltr\">1, 2, 3, 4, 5, 6<\/p>\n<\/blockquote>\n<p>Although this method seems quite efficient for copying arrays, there is a problem with it.<\/p>\n<p>If you change the elements of one array in the example above, the elements of the other array will also change.<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">class AssignmentOperator {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">int [] numbers = {1, 2, 3, 4, 5, 6};<\/li>\n<li dir=\"ltr\">int [] positiveNumbers = numbers;\u00a0\/\/ copying arrays<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">numbers [0] = -1;<\/li>\n<li dir=\"ltr\">for (int number: positiveNumbers) {<\/li>\n<li dir=\"ltr\">System.out.print (number + \u201c,\u201c);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>Output<\/p>\n<blockquote>\n<p dir=\"ltr\">-1, 2, 3, 4, 5, 6<\/p>\n<\/blockquote>\n<p>When the first element of the numbers array changes to -1, the first element of the positiveNumbers array also changes to -1 because both arrays point to the same array object.<\/p>\n<p>This is called surface copy.<\/p>\n<p>Most of the time, however, we need a deep copy rather than a shallow copy. A deep copy copies the values of the new array object&#8217;s properties.<\/p>\n<h2><span style=\"font-size: 18pt;\">2- Using the loop structure to copy arrays<\/span><\/h2>\n<p>Let&#8217;s take an example:<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">import java.util.Arrays;<\/li>\n<li dir=\"ltr\">class ArraysCopy {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">int [] source = {1, 2, 3, 4, 5, 6};<\/li>\n<li dir=\"ltr\">int [] destination = new int [6];<\/li>\n<li dir=\"ltr\">for (int i = 0; i &lt;source.length; ++ i) {<\/li>\n<li dir=\"ltr\">destination [i] = source [i];<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">\/\/ converting array to string<\/li>\n<li dir=\"ltr\">System.out.println (Arrays.toString (destination));<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>Output<\/p>\n<blockquote>\n<p dir=\"ltr\">[1, 2, 3, 4, 5, 6]<\/p>\n<\/blockquote>\n<p>Here, the loop iterates over the elements of the source array. In each iteration, the corresponding component of the source array is copied to the destination array.<\/p>\n<p>The source and destination arrays do not share the same reference (i.e., they are not a deep copy). This means that if the elements of one array (source or destination) change, the elements of the other array will remain unchanged.<\/p>\n<p>The toString () method converts arrays to strings (for output only).<\/p>\n<p>There is a better way (better than loops) to copy arrays in Java using arraycopy () and copyOfRange ().<\/p>\n<h2><span style=\"font-size: 18pt;\">3. Copy arrays with arraycopy ()<\/span><\/h2>\n<p>The System class contains an arraycopy () method that allows you to copy data from one array to another.<\/p>\n<p>The arraycopy () method is efficient and flexible. This method enables you to copy a specific portion of the source array to the destination array<strong>.<\/strong><\/p>\n<blockquote>\n<p dir=\"ltr\">public static void arraycopy (Object src, int srcPos,<\/p>\n<p dir=\"ltr\">Object dest, int destPos, int length)<\/p>\n<\/blockquote>\n<p>here,<\/p>\n<ul>\n<li>src &#8211; The variety you want to copy<\/li>\n<li>srcPos &#8211; Start position (index) in the rc array<\/li>\n<li>dest &#8211; The elements of the src array are copied to this array<\/li>\n<li>destPos &#8211; Start position (index) in the destination array<\/li>\n<li>length &#8211; The number of elements to copy<\/li>\n<\/ul>\n<p>Let&#8217;s take an example:<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">\/\/ To use Arrays.toString () method<\/li>\n<li dir=\"ltr\">import java.util.Arrays;<\/li>\n<li dir=\"ltr\">class ArraysCopy {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\">int [] n1 = {2, 3, 12, 4, 12, -2};<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">int [] n3 = new int [5];<\/li>\n<li dir=\"ltr\">\/\/ Creating n2 array of having length of n1 array<\/li>\n<li dir=\"ltr\">int [] n2 = new int [n1.length];<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">\/\/ copying entire n1 array to n2<\/li>\n<li dir=\"ltr\">System.arraycopy (n1, 0, n2, 0, n1.length);<\/li>\n<li dir=\"ltr\">System.out.println (\u201cn2 =\u201d + Arrays.toString (n2));<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">\/\/ copying elements from index 2 on n1 array<\/li>\n<li dir=\"ltr\">\/\/ copying element to index 1 of n3 array<\/li>\n<li dir=\"ltr\">\/\/ 2 elements will be copied<\/li>\n<li dir=\"ltr\">System.arraycopy (n1, 2, n3, 1, 2);<\/li>\n<li dir=\"ltr\">System.out.println (\u201cn3 =\u201d + Arrays.toString (n3));<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>Output<\/p>\n<blockquote>\n<p dir=\"ltr\">n2 = [2, 3, 12, 4, 12, -2]<\/p>\n<p dir=\"ltr\">n3 = [0, 12, 4, 0, 0]<\/p>\n<\/blockquote>\n<p>Note that the default initial value of the elements of an array is the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Integer\" target=\"_blank\" rel=\"noopener\">integer<\/a> 0.<\/p>\n<h2><span style=\"font-size: 18pt;\">4- Copy arrays with the copyOfRange () method<\/span><\/h2>\n<p>Additionally, you can use the copyOfRange () method defined in the Java\u2014uti\u2014arrays class to copy arrays. You do not need to create a destination array before using this method.<\/p>\n<p>Here&#8217;s how to do it.<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">\/\/ To use toString () and copyOfRange () method<\/li>\n<li dir=\"ltr\">import java.util.Arrays;<\/li>\n<li dir=\"ltr\">class ArraysCopy {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">int [] source = {2, 3, 12, 4, 12, -2};<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">\/\/ copying entire source array to destination<\/li>\n<li dir=\"ltr\">int [] destination1 = Arrays.copyOfRange (source, 0, source.length);<\/li>\n<li dir=\"ltr\">System.out.println (\u201cdestination1 =\u201d + Arrays.toString (destination1));<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">\/\/ copying from index 2 to 5 (5 is not included)<\/li>\n<li dir=\"ltr\">int [] destination2 = Arrays.copyOfRange (source, 2, 5);<\/li>\n<li dir=\"ltr\">System.out.println (\u201cdestination2 =\u201d + Arrays.toString (destination2));<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>Output<\/p>\n<blockquote>\n<p dir=\"ltr\">destination1 = [2, 3, 12, 4, 12, -2]<\/p>\n<p dir=\"ltr\">destination2 = [12, 4, 12]<\/p>\n<\/blockquote>\n<h1 style=\"text-align: center;\"><span style=\"font-size: 18pt;\"><img decoding=\"async\" class=\"alignnone wp-image-257947 size-full\" src=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/nestedloops.png\" alt=\"Copy arrays with the copyOfRange () method\" width=\"555\" height=\"301\" srcset=\"https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/nestedloops.png 555w, https:\/\/ded9.com\/wp-content\/uploads\/2021\/03\/nestedloops-300x163.png 300w\" sizes=\"(max-width: 555px) 100vw, 555px\" \/><\/span><\/h1>\n<h2 style=\"text-align: left;\"><span style=\"font-size: 18pt;\">Copy two-dimensional arrays using loops.<\/span><\/h2>\n<p>There is an example of copying irregular two-dimensional arrays using loops:<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">import java.util.Arrays;<\/li>\n<li dir=\"ltr\">class ArraysCopy {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">int [] [] source = source<\/li>\n<li dir=\"ltr\">{1, 2, 3, 4},<\/li>\n<li dir=\"ltr\">{5, 6},<\/li>\n<li dir=\"ltr\">\u06f0 0, 2, 42, -4, 5.<\/li>\n<li dir=\"ltr\">};<\/li>\n<li dir=\"ltr\">int [] [] destination = new int [source.length] [];<\/li>\n<li dir=\"ltr\">for (int i = 0; i &lt;destination.length; ++ i) {<\/li>\n<li dir=\"ltr\">\/\/ allocating space for each row of destination array<\/li>\n<li dir=\"ltr\">destination [i] = new int [source [i] .length];<\/li>\n<li dir=\"ltr\">for (int j = 0; j &lt;destination [i] .length; ++ j) {<\/li>\n<li dir=\"ltr\">destination [i] [j] = source [i] [j];<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">\/\/ displaying destination array<\/li>\n<li dir=\"ltr\">System.out.println (Arrays.deepToString (destination));<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<p>Output<\/p>\n<blockquote>\n<p dir=\"ltr\">[[1, 2, 3, 4], [5, 6], [0, 2, 42, -4, 5]]<\/p>\n<\/blockquote>\n<p>We used the Arrays.deepToString() method in the code above. deepToString() provides a clearer view of a multidimensional array, such as a 2D array.<\/p>\n<p>You can replace the inner loop of the above code with System.arraycopy () or Arrays. copyOf () like a one-dimensional array.<\/p>\n<p>Here is an example of doing the same with arraycopy().<\/p>\n<blockquote>\n<ol>\n<li dir=\"ltr\">import java.util.Arrays;<\/li>\n<li dir=\"ltr\">class AssignmentOperator {<\/li>\n<li dir=\"ltr\">public static void main (String [] args) {<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">int [] [] source = source<\/li>\n<li dir=\"ltr\">{1, 2, 3, 4},<\/li>\n<li dir=\"ltr\">{5, 6},<\/li>\n<li dir=\"ltr\">\u06f0 0, 2, 42, -4, 5.<\/li>\n<li dir=\"ltr\">};<\/li>\n<li dir=\"ltr\">int [] [] destination = new int [source.length] [];<\/li>\n<li dir=\"ltr\">for (int i = 0; i &lt;source.length; ++ i) {<\/li>\n<li dir=\"ltr\">\/\/ allocating space for each row of destination array<\/li>\n<li dir=\"ltr\">destination [i] = new int [source [i] .length];<\/li>\n<li dir=\"ltr\">System.arraycopy (source [i], 0, destination [i], 0, destination [i] .length);<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\"><\/li>\n<li dir=\"ltr\">\/\/ displaying destination array<\/li>\n<li dir=\"ltr\">System.out.println (Arrays.deepToString (destination));<\/li>\n<li dir=\"ltr\">}<\/li>\n<li dir=\"ltr\">}<\/li>\n<\/ol>\n<\/blockquote>\n<h2>FAQ<\/h2>\n<div id=\"rank-math-rich-snippet-wrapper\"><div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-1\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What are common ways to copy arrays in Java?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>You can copy arrays using loops, System.arraycopy, Arrays.copyOf, or cloning.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-2\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Why would you need to copy an array?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>To preserve the original data while working with a duplicate.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-3\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">How do you copy an array with Arrays.copyOf?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Use Arrays.copyOf(original, newLength) to create a new array with copied elements.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn about different methods that you can use to copy arrays (both one-dimensional and two-dimensional) in Java. There are several techniques you can use to copy arrays in Java. Copy arrays using the assignment operator Let&#8217;s take an example, class CopyArray { public static void main (String [] args) { [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":5285,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11513],"tags":[840],"class_list":["post-5284","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-2","tag-java"],"acf":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/5284","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/comments?post=5284"}],"version-history":[{"count":5,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/5284\/revisions"}],"predecessor-version":[{"id":266369,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/posts\/5284\/revisions\/266369"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/media\/5285"}],"wp:attachment":[{"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/media?parent=5284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/categories?post=5284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ded9.com\/de\/wp-json\/wp\/v2\/tags?post=5284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}