{"id":4681,"date":"2019-10-30T19:27:32","date_gmt":"2019-10-30T10:27:32","guid":{"rendered":"https:\/\/chat-messenger.com\/?p=4681"},"modified":"2019-10-30T19:38:47","modified_gmt":"2019-10-30T10:38:47","slug":"array_copy_clone","status":"publish","type":"post","link":"https:\/\/chat-messenger.com\/en\/blog\/java\/array_copy_clone","title":{"rendered":"Summary of basic array operations in Java"},"content":{"rendered":"<h2>Initialize array upon declaration<\/h2>\n<p>The array can be initialized at the same time as the declaration by writing<\/p>\n<h4>sample code<\/h4>\n<pre class=\"sample_src\">\r\nstatic void arraySample() {\r\n    String[] s = {\"apple\", \"mandarin\", \"banana\"}; }\r\n}\r\nSystem.out.println(s[1]);\r\n<\/pre>\n<p>Output Results:<\/p>\n<pre class=\"console\">\r\nunfinished\r\n<\/pre>\n<p><script type=\"text\/javascript\" src=\"https:\/\/chat-messenger.com\/js\/common.js?dd12sssas2223\" charset=\"UTF-8\"><\/script><br \/>\n<script type=\"text\/javascript\"> writePR(); <\/script><\/p>\n<h2>Get the number of elements in an array - -. <strong>length<\/strong><\/h2>\n<p>If you want to know how many elements are in an array, use length, with the slight caveat that length is a field, not a method.<\/p>\n<h4>sample code<\/h4>\n<pre class=\"sample_src\">\r\nstatic void lengthSample() {\r\n    String[] s1 = {\"apple\", \"mandarin\", \"grape\"};\r\n    String[] s2 = {};\r\n    System.out.println(s1.length);\r\n    System.out.println(s2.length);\r\n}\r\n<\/pre>\n<p>Output Results:<\/p>\n<pre class=\"console\">\r\n3\r\n0\r\n<\/pre>\n<h2>Copying arrays - <strong>clone<\/strong><\/h2>\n<p>If you want to create an array that is identical to another array, clone is useful.<\/p>\n<h4>sample code<\/h4>\n<pre class=\"sample_src\">\r\nstatic void cloneSample() {\r\n    String[] s1 = {\"apple\", \"mandarin\", \"grape\"};\r\n    String[] s2 = s1.clone();\r\n    System.out.println(s1[0] + ' ' + s1[1] + ' ' + s1[2]);\r\n    System.out.println(s2[0] + ' ' + s2[1] + ' ' + s2[2]);\r\n}\r\n<\/pre>\n<p>Output Results:<\/p>\n<pre class=\"console\">\r\nApples Oranges Grapes\r\nApples, mandarins, grapes\r\n<\/pre>\n<h2>Copying arrays - <strong>System.arraycopy<\/strong><\/h2>\n<p>Arrays in Java are fixed at their initialization size. If you want to increase the size of an array once it has been created, you must use System.arraycopy.<\/p>\n<p>arraycopy, like clone, is used to copy arrays, but it can be more finely tuned than clone.<\/p>\n<pre class=\"point\">\r\narraycopy(Object src, int srcPos, Object dest, int destPos, int length)\r\n src... Array from which to copy\r\n srcPos... Starting position of the source array (src) to be copied\r\n dest... Destination array\r\n destPos... Starting position of the destination array (dest) to be copied\r\n length... Number of elements to copy\r\n<\/pre>\n<h4>sample code<\/h4>\n<pre class=\"sample_src\">\r\nstatic void arraycopySample() {\r\n    String[] s1 = {\"apple\", \"mandarin\", \"grape\"};\r\n    String[] s2 = new String[5];\r\n    System.arraycopy(s1, 0, s2, 0, 3);\r\n    s2[3] = \"banana\";\r\n    s2[4] = \"cherry\";\r\n    System.out.println(s1[0] + \" \" + s1[1] + \" \" + s1[2]);\r\n    System.out.println(s2[0] + \" \" + s2[1] + \" \" + s2[2] + \" \" + s2[3] + \" \" + s2[4]);\r\n}\r\n<\/pre>\n<p>Output Results:<\/p>\n<pre class=\"console\">\r\nApples Oranges Grapes\r\nApples Oranges Grapes Bananas Cherries\r\n<\/pre>\n<p>It is also convenient to use arraycopy to join arrays.<\/p>\n<h4>sample code<\/h4>\n<pre class=\"sample_src\">\r\nstatic void joinArraySample() {\r\n    String[] s1 = {\"apple\", \"mandarin\", \"grape\"};\r\n    String[] s2 = {\"banana\", \"cherry\", \"momo\"};\r\n    int rLen = s1.length + s2.length;\r\n    String[] rStr = new String[rLen]; \/\/initialize array for length of both s1 and s2\r\n    System.arraycopy(s1, 0, rStr, 0, s1.length);\r\n    System.arraycopy(s2, 0, rStr, s1.length, s2.length); \/\/Setting the starting position of the destination array is the key point\r\n    System.out.println(rStr[0] + \" \" + rStr[1] + \" \" + rStr[2] + \" \" + rStr[3] + \" \" + rStr[4] + \" \" + rStr[5]);\r\n}\r\n<\/pre>\n<p>Output Results:<\/p>\n<pre class=\"console\">\r\nApples Oranges Grapes Grapes Bananas Cherries Momo\r\n<\/pre>\n<h2>Create multidimensional arrays<\/h2>\n<p>You can create multidimensional arrays by creating arrays within arrays. This is useful for storing tabular data.<\/p>\n<h4>sample code<\/h4>\n<pre class=\"sample_src\">\r\nstatic void twoDArraySample() {\r\n    int[] a1 = {38, 84, 98};\r\n    int[] a2 = {32, 11, 56};\r\n    int[] a3 = {82, 77, 8};\r\n    int[][] twoDArray = {a1, a2, a3};\r\n    for (int[] a : twoDArray) {\r\n        for (int score : a) {\r\n            System.out.print(score + \" \"); }\r\n        }\r\n        System.out.println(\"\"); }\r\n    }\r\n}\r\n<\/pre>\n<p>Output Results:<\/p>\n<pre class=\"console\">\r\n38 84 98 \r\n32 11 56 \r\n82 77 8 \r\n<\/pre>","protected":false},"excerpt":{"rendered":"<p>Initializing an array at the same time as declaration You can initialize an array at the same time as declaration by writing as follows. Sample code sta [\u2026]<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"swell_btn_cv_data":""},"categories":[19],"tags":[],"_links":{"self":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/4681"}],"collection":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/comments?post=4681"}],"version-history":[{"count":6,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/4681\/revisions"}],"predecessor-version":[{"id":4688,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/4681\/revisions\/4688"}],"wp:attachment":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/media?parent=4681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/categories?post=4681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/tags?post=4681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}