{"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\/zh\/\u535a\u5ba2\/java\/array_copy_clone","title":{"rendered":"Java \u4e2d\u7684\u57fa\u672c\u6570\u7ec4\u64cd\u4f5c\u6458\u8981\u3002"},"content":{"rendered":"<h2>\u58f0\u660e\u6570\u7ec4\u540e\u7acb\u5373\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\u3002<\/h2>\n<p>\u6570\u7ec4\u53ef\u4ee5\u5728\u58f0\u660e\u7684\u540c\u65f6\u521d\u59cb\u5316\uff0c\u65b9\u6cd5\u662f\u5199\u5165<\/p>\n<h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<pre class=\"sample_src\">\r\nstatic void arraySample() {\r\n    String[] s = {\"\u82f9\u679c\"\u3001\"\u67d1\u6a58\"\u3001\"\u9999\u8549\"}\uff1b\r\n}\r\nSystem.out.println(s[1])\uff1b\r\n<\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre class=\"console\">\r\n\u672a\u5b8c\u6210\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>\u83b7\u53d6\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u4e2a\u6570 -\u3002 <strong>\u957f\u5ea6<\/strong><\/h2>\n<p>\u5982\u679c\u60f3\u77e5\u9053\u6570\u7ec4\u4e2d\u6709\u591a\u5c11\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528 length\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0clength \u662f\u4e00\u4e2a\u5b57\u6bb5\uff0c\u800c\u4e0d\u662f\u4e00\u79cd\u65b9\u6cd5\u3002<\/p>\n<h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<pre class=\"sample_src\">\r\nstatic void lengthSample() {\r\n    String[] s1 = {\"\u82f9\u679c\"\u3001\"\u67d1\u6a58\"\u3001\"\u8461\u8404\"}\uff1b\r\n    String[] s2 = {}\uff1b\r\n    System.out.println(s1.length)\uff1b\r\n    System.out.println(s2.length)\uff1b\r\n}\r\n<\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre class=\"console\">\r\n3\r\n0\r\n<\/pre>\n<h2>\u590d\u5236\u6570\u7ec4 <strong>\u590d\u5236<\/strong><\/h2>\n<p>\u5982\u679c\u4f60\u60f3\u521b\u5efa\u4e00\u4e2a\u4e0e\u6570\u7ec4\u5b8c\u5168\u76f8\u540c\u7684\u6570\u7ec4\uff0c\u514b\u9686\u5f88\u6709\u7528\u3002<\/p>\n<h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<pre class=\"sample_src\">\r\nstatic void cloneSample() {\r\n    String[] s1 = {\"\u82f9\u679c\"\u3001\"\u67d1\u6a58\"\u3001\"\u8461\u8404\"}\uff1b\r\n    String[] s2 = s1.clone()\uff1b\r\n    System.out.println(s1[0] + ' ' + s1[1] + ' ' + s1[2])\uff1b\r\n    System.out.println(s2[0] + ' ' + s2[1] + ' ' + s2[2])\uff1b\r\n}\r\n<\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre class=\"console\">\r\n\u82f9\u679c \u6a58\u5b50 \u8461\u8404\r\n\u82f9\u679c \u6a58\u5b50 \u8461\u8404\r\n<\/pre>\n<h2>\u590d\u5236\u6570\u7ec4 <strong>System.arraycopy<\/strong><\/h2>\n<p>Java \u4e2d\u6570\u7ec4\u7684\u521d\u59cb\u5316\u5927\u5c0f\u662f\u56fa\u5b9a\u7684\u3002\u5982\u679c\u60f3\u5728\u521b\u5efa\u6570\u7ec4\u540e\u589e\u52a0\u5176\u5927\u5c0f\uff0c\u5219\u9700\u8981\u4f7f\u7528 System.arraycopy\u3002<\/p>\n<p>arraycopy \u7528\u4e8e\u590d\u5236\u6570\u7ec4\uff0c\u4e0e clone \u7c7b\u4f3c\uff0c\u4f46\u6bd4 clone \u5141\u8bb8\u66f4\u591a\u7684\u5fae\u8c03\u3002<\/p>\n<pre class=\"point\">\r\narraycopy(Object src, int srcPos, Object dest, int destPos, int length)\r\n src...\u590d\u5236\u5bf9\u8c61\u7684\u6570\u7ec4\r\n srcPos...\u8981\u590d\u5236\u7684\u6e90\u6570\u7ec4\uff08src\uff09\u7684\u8d77\u59cb\u4f4d\u7f6e\r\n dest...\u76ee\u6807\u6570\u7ec4\r\n destPos...\u8981\u590d\u5236\u7684\u76ee\u6807\u6570\u7ec4\uff08dest\uff09\u7684\u8d77\u59cb\u4f4d\u7f6e\r\n length...\u8981\u590d\u5236\u7684\u5143\u7d20\u4e2a\u6570\r\n<\/pre>\n<h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<pre class=\"sample_src\">\r\nstatic void arraycopySample() {\r\n    String[] s1 = {\"\u82f9\u679c\"\u3001\"\u67d1\u6a58\"\u3001\"\u8461\u8404\"}\uff1b\r\n    String[] s2 = new String[5]\uff1b\r\n    System.arraycopy(s1, 0, s2, 0, 3)\uff1b\r\n    s2[3] = \"\u9999\u8549\"\uff1b\r\n    s2[4] = \"\u6a31\u6843\"\uff1b\r\n    System.out.println(s1[0] + \" \" + s1[1] + \" \" + s1[2])\uff1b\r\n    System.out.println(s2[0] + \" \" + s2[1] + \" \" + s2[2] + \" \" + s2[3] + \" \" + s2[4])\uff1b\r\n}\r\n<\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre class=\"console\">\r\n\u82f9\u679c \u6a58\u5b50 \u8461\u8404\r\n\u82f9\u679c \u6a58\u5b50 \u8461\u8404 \u9999\u8549 \u6a31\u6843\r\n<\/pre>\n<p>\u4f7f\u7528 arraycopy \u8fde\u63a5\u6570\u7ec4\u4e5f\u5f88\u65b9\u4fbf\u3002<\/p>\n<h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<pre class=\"sample_src\">\r\nstatic void joinArraySample() {\r\n    String[] s1 = {\"\u82f9\u679c\"\u3001\"\u67d1\u6a58\"\u3001\"\u8461\u8404\"}\uff1b\r\n    String[] s2 = {\"\u9999\u8549\"\u3001\"\u6a31\u6843\"\u3001\"Momo\"}\uff1b\r\n    int rLen = s1.length + s2.length\uff1b\r\n    String[] rStr = new String[rLen]; \/\/\u521d\u59cb\u5316 s1 \u548c s2 \u957f\u5ea6\u7684\u6570\u7ec4\r\n    System.arraycopy(s1, 0, rStr, 0, s1.length)\uff1b\r\n    System.arraycopy(s2, 0, rStr, s1.length, s2.length); \/\/\u8bbe\u7f6e\u76ee\u6807\u6570\u7ec4\u7684\u8d77\u59cb\u4f4d\u7f6e\u662f\u5173\u952e\r\n    System.out.println(rStr[0] + \" \" + rStr[1] + \" \" + rStr[2] + \" \" + rStr[3] + \" \" + rStr[4] + \" \" + rStr[5])\uff1b\r\n}\r\n<\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre class=\"console\">\r\n\u82f9\u679c \u6a58\u5b50 \u8461\u8404 \u9999\u8549 \u6a31\u6843 \u83ab\u83ab\r\n<\/pre>\n<h2>\u521b\u5efa\u591a\u7ef4\u6570\u7ec4<\/h2>\n<p>\u901a\u8fc7\u5728\u6570\u7ec4\u4e2d\u521b\u5efa\u6570\u7ec4\uff0c\u53ef\u4ee5\u521b\u5efa\u591a\u7ef4\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u8fd9\u5728\u5b58\u50a8\u8868\u683c\u6570\u636e\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<pre class=\"sample_src\">\r\nstatic void twoDArraySample() {\r\n    int[] a1 = {38, 84, 98}\uff1b\r\n    int[] a2 = {32, 11, 56}\uff1b\r\n    int[] a3 = {82, 77, 8}\uff1b\r\n    int[][] twoDArray = {a1, a2, a3}\uff1b\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>\u8f93\u51fa\u7ed3\u679c\uff1a<\/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>\u5728\u58f0\u660e\u7684\u540c\u65f6\u521d\u59cb\u5316\u6570\u7ec4 \u60a8\u53ef\u4ee5\u5728\u58f0\u660e\u7684\u540c\u65f6\u521d\u59cb\u5316\u6570\u7ec4\uff0c\u5982\u4e0b\u6240\u793a\u3002\u793a\u4f8b\u4ee3\u7801 sta [...]<\/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\/zh\/wp-json\/wp\/v2\/posts\/4681"}],"collection":[{"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/comments?post=4681"}],"version-history":[{"count":6,"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/posts\/4681\/revisions"}],"predecessor-version":[{"id":4688,"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/posts\/4681\/revisions\/4688"}],"wp:attachment":[{"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/media?parent=4681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/categories?post=4681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chat-messenger.com\/zh\/wp-json\/wp\/v2\/tags?post=4681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}