{"id":13366,"date":"2020-03-16T03:37:15","date_gmt":"2020-03-16T10:37:15","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=13366"},"modified":"2023-12-01T02:31:46","modified_gmt":"2023-12-01T10:31:46","slug":"java-copy-array","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/java-copy-array\/","title":{"rendered":"4 Ways to Copy an Array in Java"},"content":{"rendered":"\n<p>When you\u2019re working with arrays in Java, you may decide that you want to create a copy of an array. For instance, if you\u2019re running a coffee shop and want to create a seasonal menu, you may want to create a copy of your original menu on which the new menu can be based.<br><\/p>\n\n\n\n<p>In Java, there are a number of ways in which you can copy an array. This tutorial will explore four common methods to copy arrays and discuss how they work line-by-line. After reading this tutorial, you\u2019ll be a master at copying arrays in Java.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Java Arrays<\/h2>\n\n\n\n<p>In Java, an array is a container that holds values that hold one single type. For example, an array could be used to store a list of books, or a list of scores players have earned in a game of darts.<br><\/p>\n\n\n\n<p>Arrays are useful when you want to work with many similar values because you can store them all in one collection. This allows you to condense your code and also run the same methods on similar values at one time.<br><\/p>\n\n\n\n<p>Suppose we want to create an array that stores the coffees sold at our coffeeshop. We could do so using this code:<br><\/p>\n\n\n\n<p><code>String[] coffees = {\u201cEspresso\u201d, \u201cMocha\u201d, \u201cLatte\u201d, \u201cCappuccino\u201d, \u201cPour Over\u201d, \u201cFlat White\u201d};<br><\/code><\/p>\n\n\n\n<p>In this example, we declare an array called <code>coffees<\/code> which stores String values. Our array contains six values.<br><\/p>\n\n\n\n<p>Each item in an array is assigned an index number, starting from 0, which can be used to reference items individually in an array.<br><\/p>\n\n\n\n<p>Now that we have explored the basics of Java arrays, we can discuss methods you can use to copy the contents of your array.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Copy Array Using Assignment Operator<\/h2>\n\n\n\n<p>One of the most common clone methods used to copy an array is to use the assignment operator.&nbsp;<br><\/p>\n\n\n\n<p>The assignment operator is used to assign a value to an array. By using the assignment operator, we can assign the contents of an existing array to a new variable, which will create a copy of our existing array.<br><\/p>\n\n\n\n<p>Let\u2019s return to the coffeeshop. Suppose we want to create a copy of the <code>coffees<\/code> array upon which we will base our summer coffee menu. We could use this code to create a copy of the array:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>class CopyAssignment {\n\tpublic static void main(String[] args) {\n\t\tString[] coffees = {\"Espresso\", \"Mocha\", \"Latte\", \"Cappuccino\", \"Pour Over\", \"Flat White\"};\n\t\tString[] summer_coffees = coffees;\n\t\tfor (String c: summer_coffees) {\n\t\t\tSystem.out.print(c + \",\");\n\t\t}\n\t}\n}\n<\/pre><\/div>\n\n\n\n<p>Our code returns:<br><\/p>\n\n\n\n<p><code>Espresso, Mocha, Latte, Cappuccino, Pour Over, Flat White,<br><\/code><\/p>\n\n\n\n<p>Let\u2019s break down our code. On the first line of code inside our CopyAssignment class, we declare an array called <code>coffees<\/code> which stores our standard coffee menu.<br><\/p>\n\n\n\n<p>On the next line, we use the assignment operator to assign the value of <code>coffees<\/code> to a new array called <code>summer_coffees<\/code>. Then we create a \u201cfor-each\u201d loop that iterates through every item in the <code>summer_coffees<\/code> array and prints it out to the console.<br><\/p>\n\n\n\n<p>There is one drawback to using this method: if you change elements of one array, the other array will also be changed. So if we changed the value of <code>Latte<\/code> to <code>Summer Latte<\/code> in our <code>summer_coffees<\/code> list, our <code>coffees<\/code> list would also be changed.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Loop to Copy Arrays<\/h2>\n\n\n\n<p>The first approach we discussed to copy an array &#8212; using the assignment operator &#8212; creates what is called a <code>shallow copy.<\/code> This is the case because we have assigned an existing array object to a new one, which means that when we change any object, they will both be changed &#8212; the two objects are linked.<br><\/p>\n\n\n\n<p>However, we often need to create a deep copy. Deep copies copy the values from an existing object and create a new array object. When you create a deep copy, you can change your new array without affecting the original one.<br><\/p>\n\n\n\n<p>One approach that can be used to create a deep copy is to create a <code>for<\/code> loop which iterates through the contents of an array and creates a new array.<br><\/p>\n\n\n\n<p>Suppose we want to create a deep copy of our <code>coffees<\/code> array called <code>summer_coffees.<\/code> This needs to be a deep copy because we plan on changing the contents of the <code>summer_coffees<\/code> array to reflect the new coffees we will offer in the summer months.&nbsp;<br><\/p>\n\n\n\n<p>Here\u2019s the code we would use to create a deep copy using a <code>for<\/code> loop:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>import java.util.Arrays;\nclass LoopCopy {\n\tpublic static void main(String[] args) {\n\t\tString[] coffees = {\"Espresso\", \"Mocha\", \"Latte\", \"Cappuccino\", \"Pour Over\", \"Flat White\"};\n\t\tString[] summer_coffees = new String[6];\n\t\tfor (int i = 0; i &lt; coffees.length; ++i) {\n\t\t\tsummer_coffees[i] = coffees[i];\n\t\t}\n\t\tSystem.out.println(Arrays.toString(summer_coffees));\n\t}\n}\n<\/pre><\/div>\n\n\n\n<p>When we run our code, the output is as follows:<br><\/p>\n\n\n\n<p><code>[Espresso, Mocha, Latte, Cappuccino, Pour Over, Flat White]<br><\/code><\/p>\n\n\n\n<p>As you can see, our code has created a copy of our original array. Let\u2019s explain how it works step-by-step:<br><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>We import <code>java.util.Arrays<\/code> which includes the toString() method, we\u2019ll use to print our array to the console at the end of the example.<\/li>\n<\/ol>\n\n\n\n<ol class=\"wp-block-list\">\n<li>We declare an array called <code>coffees<\/code> which stores the list of coffees on our standard menu.<\/li>\n\n\n\n<li>We initialize an array called <code>summer_coffees<\/code> which will store six values.<\/li>\n\n\n\n<li>We use a for loop to iterate through every item in the <code>coffees<\/code> list.<\/li>\n\n\n\n<li>Each time the for loop runs, the item at index value <code>i<\/code> in summer_coffees will be assigned the item at the index value <code>i<\/code>in coffees.<\/li>\n\n\n\n<li>We use Arrays.toString() to convert <code>summer_coffees<\/code> to a string, then print out the new array with our copied elements to the console.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Using copyOfRange() Java Methods<\/h2>\n\n\n\n<p>The Java copyOfRange() method is used to copy Arrays.copyOfRange() is part of the java.util.Arrays class. Here\u2019s the syntax for the copyOfRange() method:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>import java.util.arrays;\nDataType[] newArray = Arrays.copyOfRange(oldArray, indexPos, length);\n<\/pre><\/div>\n\n\n\n<p>Let\u2019s break down the syntax for the copyOfRange() method:<br><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>DataType<\/strong> is the type of data the new array will store.<\/li>\n\n\n\n<li><strong>newArray<\/strong> is the name of the new array.<\/li>\n\n\n\n<li><strong>oldArray<\/strong> is the array whose values you want to copy to the <code>newArray<\/code>.<\/li>\n\n\n\n<li><strong>indexPos<\/strong> is the position at which the copy operation should begin in the <code>oldArray<\/code>.<\/li>\n\n\n\n<li><strong>length<\/strong> is the number of values that should be copied from <code>oldArray<\/code> to <code>newArray<\/code>.<\/li>\n<\/ol>\n\n\n\n<p>Let\u2019s walk through an example to demonstrate the copyOfRange() method in action. Say we want to create a copy of our <code>coffees<\/code> array from earlier. We could do so using this code:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>import java.util.Arrays;\nclass CopyUsingRange {\n\tpublic static void main(String[] args) {\n\t\tString[] coffees = {\"Espresso\", \"Mocha\", \"Latte\", \"Cappuccino\", \"Pour Over\", \"Flat White\"};\n\t\tString[] summer_coffees = Arrays.copyOfRange(coffees, 0, coffees.length);\n\t\tSystem.out.println(\"Summer coffees: \" + Arrays.toString(summer_coffees));\n\t}\n}\n<\/pre><\/div>\n\n\n\n<p>Our code returns:<br><\/p>\n\n\n\n<p><code>Summer coffees: [Espresso, Mocha, Latte, Cappuccino, Pour Over, Flat White]<br><\/code><\/p>\n\n\n\n<p>Let\u2019s break down our code:<br><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>We import the <code>java.util.Arrays<\/code> library which stores the copyOfRange() and toString() methods that we\u2019ll use in our example.<\/li>\n\n\n\n<li>We declare an array called <code>coffees<\/code> which stores the coffees on our standard menu.<\/li>\n\n\n\n<li>We declare an array called <code>summer_coffees<\/code> and use the copyOfRange() method to create a copy of the<code> coffees<\/code> array. The parameters we specify are as follows:\n<ol class=\"wp-block-list\">\n<li><strong>coffees<\/strong> is the name of the array we want to copy.<\/li>\n\n\n\n<li><strong>0<\/strong> specifies we want to copy values starting at index position 0 from the <code>coffees<\/code> array.<\/li>\n\n\n\n<li><strong>coffees.length <\/strong>specifies we want to copy every value in the list.<\/li>\n<\/ol>\n<\/li>\n\n\n\n<li>We print out \u201cSummer coffees: \u201c followed by the resulting array called <code>summer_coffees<\/code> to the console.<\/li>\n<\/ol>\n\n\n\n<p>Now we\u2019ve created a copy of our \u201c<code>coffees<\/code> list called <code>summer_coffees<\/code>.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using arraycopy() Java Methods<\/h2>\n\n\n\n<p>The arraycopy() method is used to copy data from one array to another array. The arraycopy() method is part of the System class and includes a number of options which allow you to customize the copy you create of an existing array.<br><\/p>\n\n\n\n<p>Here\u2019s the syntax of the arraycopy() method:<br><\/p>\n\n\n\n<p><code>System.arraycopy(sourceArray, startingPos, newArray, newArrayStartingPos, length);<br><\/code><\/p>\n\n\n\n<p>Let\u2019s break down this method. copyarray() accepts five parameters:<br><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>sourceArray<\/strong> is the name of the array you want to copy.<\/li>\n\n\n\n<li><strong>startingPos<\/strong> is the index position from which you want to start copying values in the <code>source_array<\/code>.<\/li>\n\n\n\n<li><strong>newArray<\/strong> is the name of the new array where values will be copies.<\/li>\n\n\n\n<li><strong>newArrayStartingPos<\/strong> is the index position at which the copied values should be added.<\/li>\n\n\n\n<li><strong>length<\/strong> is the number of elements you want to copy to the <code>new_array<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>Let\u2019s return to the coffeeshop. Suppose we wanted to copy every value in our <code>coffees<\/code> array to a new array called <code>summer_coffees<\/code>. We could do so using this code:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>import java.util.Arrays;\nclass ArrayCopyMethod {\n\tpublic static void main(String[] args) {\n\t\tString[] coffees = {\"Espresso\", \"Mocha\", \"Latte\", \"Cappuccino\", \"Pour Over\", \"Flat White\"};\n\t\tString[] summer_coffees = new String[6];\n\t\tSystem.arraycopy(coffees, 0, summer_coffees, 0, coffees.length);\n\t\tSystem.out.println(\"Summer coffees: \" + Arrays.toString(summer_coffees));\n\t}\n}\n<\/pre><\/div>\n\n\n\n<p>Our code returns:<br><\/p>\n\n\n\n<p><code>Summer coffees: [Espresso, Mocha, Latte, Cappuccino, Pour Over, Flat White]<br><\/code><\/p>\n\n\n\n<p>Let\u2019s break down our code step-by-step:<br><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>We import the <code>java.util.Arrays<\/code> package at the start of our program. This includes the toString() method we\u2019ll use to print the array copy we create at the end of our program.<\/li>\n\n\n\n<li>We declare an array called <code>coffees<\/code> which stores the coffees on our standard menu.<\/li>\n\n\n\n<li>We initialize an array called <code>summer_coffees<\/code> which will hold 6 values.<\/li>\n\n\n\n<li>We use arraycopy() to create a copy of our <code>coffees<\/code> array. Here are the parameters we specify:\n<ol class=\"wp-block-list\">\n<li><strong>coffees<\/strong> is the array we want to copy.<\/li>\n\n\n\n<li><strong>0<\/strong> is the position at which we want our copy to start in the <code>coffees<\/code> array.<\/li>\n\n\n\n<li><strong>summer_coffees<\/strong> is the array in which we want our copied values to be added.<\/li>\n\n\n\n<li><strong>0<\/strong> is the position at which we want the copied values to start being added in the <code>summer_coffees<\/code> array.<\/li>\n\n\n\n<li><strong>coffees.length<\/strong> is the number of array elements we want to copy. In this case, using <code>coffees.length<\/code> allows us to copy every element from the <code>coffees<\/code> list.<\/li>\n<\/ol>\n<\/li>\n\n\n\n<li>We print out a message stating \u201cSummer coffees: \u201c, followed by the list of summer coffees we have created.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Copying an array is a common operation when you\u2019re working with lists. This tutorial explored four ways in which you can copy an array in Java.<br><\/p>\n\n\n\n<p>First, we discussed how to create a shallow copy using the assignment operator, then we proceeded to explain how to create a deep copy using a <code>for<\/code> loop. Then we explored how to use the copyOfRange() method to create a copy of an array, and how the arraycopy() system method is used to copy an array.<br><\/p>\n\n\n\n<p>Now you\u2019re ready to start copying arrays in Java like a professional!<\/p>\n","protected":false},"excerpt":{"rendered":"When you\u2019re working with arrays in Java, you may decide that you want to create a copy of an array. For instance, if you\u2019re running a coffee shop and want to create a seasonal menu, you may want to create a copy of your original menu on which the new menu can be based. In&hellip;","protected":false},"author":240,"featured_media":13367,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[17289],"tags":[],"class_list":{"0":"post-13366","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-java"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Java","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":"","is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.4 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>4 Ways to Copy an Array in Java: The Complete Guide | Career Karma<\/title>\n<meta name=\"description\" content=\"Copying an array is a common operation when working with Java arrays. Learn four approaches to make deep and shallow copies in this article.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/java-copy-array\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"4 Ways to Copy an Array in Java\" \/>\n<meta property=\"og:description\" content=\"Copying an array is a common operation when working with Java arrays. Learn four approaches to make deep and shallow copies in this article.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/java-copy-array\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-03-16T10:37:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T10:31:46+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/adults-business-computer-conference-room-1181329.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"801\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-copy-array\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-copy-array\\\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"4 Ways to Copy an Array in Java\",\"datePublished\":\"2020-03-16T10:37:15+00:00\",\"dateModified\":\"2023-12-01T10:31:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-copy-array\\\/\"},\"wordCount\":1491,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-copy-array\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/adults-business-computer-conference-room-1181329.jpg\",\"articleSection\":[\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-copy-array\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-copy-array\\\/\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-copy-array\\\/\",\"name\":\"4 Ways to Copy an Array in Java: The Complete Guide | Career Karma\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-copy-array\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-copy-array\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/adults-business-computer-conference-room-1181329.jpg\",\"datePublished\":\"2020-03-16T10:37:15+00:00\",\"dateModified\":\"2023-12-01T10:31:46+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"Copying an array is a common operation when working with Java arrays. Learn four approaches to make deep and shallow copies in this article.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-copy-array\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-copy-array\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-copy-array\\\/#primaryimage\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/adults-business-computer-conference-room-1181329.jpg\",\"contentUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/adults-business-computer-conference-room-1181329.jpg\",\"width\":1200,\"height\":801},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-copy-array\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"4 Ways to Copy an Array in Java\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/james-gallagher-150x150.jpg\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/author\\\/jamesgallagher\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"4 Ways to Copy an Array in Java: The Complete Guide | Career Karma","description":"Copying an array is a common operation when working with Java arrays. Learn four approaches to make deep and shallow copies in this article.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/careerkarma.com\/blog\/java-copy-array\/","og_locale":"en_US","og_type":"article","og_title":"4 Ways to Copy an Array in Java","og_description":"Copying an array is a common operation when working with Java arrays. Learn four approaches to make deep and shallow copies in this article.","og_url":"https:\/\/careerkarma.com\/blog\/java-copy-array\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-03-16T10:37:15+00:00","article_modified_time":"2023-12-01T10:31:46+00:00","og_image":[{"width":1200,"height":801,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/adults-business-computer-conference-room-1181329.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/java-copy-array\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/java-copy-array\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"4 Ways to Copy an Array in Java","datePublished":"2020-03-16T10:37:15+00:00","dateModified":"2023-12-01T10:31:46+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/java-copy-array\/"},"wordCount":1491,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/java-copy-array\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/adults-business-computer-conference-room-1181329.jpg","articleSection":["Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/java-copy-array\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/java-copy-array\/","url":"https:\/\/careerkarma.com\/blog\/java-copy-array\/","name":"4 Ways to Copy an Array in Java: The Complete Guide | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/java-copy-array\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/java-copy-array\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/adults-business-computer-conference-room-1181329.jpg","datePublished":"2020-03-16T10:37:15+00:00","dateModified":"2023-12-01T10:31:46+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"Copying an array is a common operation when working with Java arrays. Learn four approaches to make deep and shallow copies in this article.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/java-copy-array\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/java-copy-array\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/java-copy-array\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/adults-business-computer-conference-room-1181329.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/adults-business-computer-conference-room-1181329.jpg","width":1200,"height":801},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/java-copy-array\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/careerkarma.com\/blog\/java\/"},{"@type":"ListItem","position":3,"name":"4 Ways to Copy an Array in Java"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/13366","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=13366"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/13366\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/13367"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=13366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=13366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=13366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}