{"id":36685,"date":"2024-11-26T14:22:19","date_gmt":"2024-11-26T06:22:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/36685\/"},"modified":"2024-11-26T14:22:19","modified_gmt":"2024-11-26T06:22:19","slug":"%e6%95%b0%e7%bb%84java%e6%80%8e%e4%b9%88%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/36685\/","title":{"rendered":"\u6570\u7ec4java\u600e\u4e48\u4f7f\u7528"},"content":{"rendered":"<blockquote><p>\n  java \u6570\u7ec4\u662f\u4e00\u79cd\u5b58\u50a8\u76f8\u540c\u7c7b\u578b\u5143\u7d20\u7684\u6709\u5e8f\u96c6\u5408\uff0c\u53ef\u901a\u8fc7\u4ece 0 \u5f00\u59cb\u7684\u7d22\u5f15\u8bbf\u95ee\u5143\u7d20\u3002\u5b83\u53ef\u901a\u8fc7\u58f0\u660e new \u5173\u952e\u5b57\u548c\u6307\u5b9a\u957f\u5ea6\u6765\u521b\u5efa\uff1aint[] myarray = new int[5];\u3002\u5e38\u89c1\u64cd\u4f5c\u5305\u62ec\u4f7f\u7528 for \u5faa\u73af\u6216 for-each \u5faa\u73af\u904d\u5386\uff0c\u6b64\u5916\u8fd8\u652f\u6301\u957f\u5ea6\u83b7\u53d6\uff0c\u590d\u5236\uff0c\u6392\u5e8f\u548c\u641c\u7d22\u3002\u591a\u7ef4\u6570\u7ec4\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5957\u6570\u7ec4\u5b9e\u73b0\uff0c\u4f46\u6570\u7ec4\u5927\u5c0f\u5728\u521b\u5efa\u540e\u4e0d\u53ef\u66f4\u6539\uff0c\u8bbf\u95ee\u8d85\u51fa\u8303\u56f4\u7684\u7d22\u5f15\u4f1a\u5bfc\u81f4\u5f02\u5e38\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/12\/2024111213515545917.jpg\" class=\"aligncenter\" title=\"\u6570\u7ec4java\u600e\u4e48\u4f7f\u7528\u63d2\u56fe\" alt=\"\u6570\u7ec4java\u600e\u4e48\u4f7f\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u6570\u7ec4\uff1a\u4f7f\u7528\u6307\u5357<\/strong><\/p>\n<p><strong>\u4ec0\u4e48\u662f\u6570\u7ec4\uff1f<\/strong><br \/>\u6570\u7ec4\u662f\u4e00\u4e2a\u7528\u4e8e\u5b58\u50a8\u76f8\u540c\u7c7b\u578b\u5143\u7d20\u7684\u6709\u5e8f\u96c6\u5408\u3002\u5143\u7d20\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\uff0c\u4ece 0 \u5f00\u59cb\u3002<\/p>\n<p><strong>\u5982\u4f55\u58f0\u660e\u6570\u7ec4\uff1f<\/strong><\/p>\n<pre>\/\/ \u58f0\u660e\u4e00\u4e2a int \u7c7b\u578b\u7684\u6570\u7ec4\uff0c\u957f\u5ea6\u4e3a 5\nint[] myArray = new int[5];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5982\u4f55\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\uff1f<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>\/\/ \u8bbf\u95ee\u5e76\u4fee\u6539\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\nmyArray[0] = 10;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5982\u4f55\u904d\u5386\u6570\u7ec4\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u5f0f\u904d\u5386\u6570\u7ec4\uff1a<\/p>\n<ul>\n<li>\n<p><strong>for \u5faa\u73af\uff1a<\/strong><\/p>\n<pre>for (int i = 0; i &lt; myArray.length; i++) {\n  \/\/ \u5904\u7406 myArray[i]\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p><strong>for-each \u5faa\u73af\uff1a<\/strong><\/p>\n<pre>for (int element : myArray) {\n  \/\/ \u5904\u7406 element\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p><strong>\u6570\u7ec4\u7684\u5e38\u89c1\u64cd\u4f5c<\/strong><\/p>\n<ul>\n<li> <strong>\u957f\u5ea6\uff1a<\/strong>myArray.length \u8fd4\u56de\u6570\u7ec4\u7684\u957f\u5ea6\u3002<\/li>\n<li> <strong>\u590d\u5236\uff1a<\/strong>Arrays.copyOf(myArray, newLength) \u8fd4\u56de\u6570\u7ec4\u7684\u526f\u672c\uff0c\u957f\u5ea6\u4e3a newLength\u3002<\/li>\n<li> <strong>\u6392\u5e8f\uff1a<\/strong>Arrays.sort(myArray) \u5bf9\u6570\u7ec4\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f\u3002<\/li>\n<li> <strong>\u641c\u7d22\uff1a<\/strong>Arrays.binarySearch(myArray, element) \u5728\u6570\u7ec4\u4e2d\u4e8c\u5206\u67e5\u627e\u7ed9\u5b9a\u5143\u7d20\u3002<\/li>\n<\/ul>\n<p><strong>\u591a\u7ef4\u6570\u7ec4<\/strong><br \/>Java \u8fd8\u652f\u6301\u591a\u7ef4\u6570\u7ec4\uff0c\u901a\u8fc7\u5d4c\u5957\u6570\u7ec4\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u58f0\u660e\u4e00\u4e2a 2D \u6570\u7ec4\uff1a<\/p>\n<pre>int[][] my2DArray = new int[3][4];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>\u6570\u7ec4\u5927\u5c0f\u5728\u521b\u5efa\u540e\u4e0d\u80fd\u6539\u53d8\u3002<\/li>\n<li>\u8bbf\u95ee\u8d85\u51fa\u8303\u56f4\u7684\u7d22\u5f15\u4f1a\u5bfc\u81f4 ArrayIndexOutOfBoundsException\u3002<\/li>\n<li>\u6570\u7ec4\u5143\u7d20\u9ed8\u8ba4\u4e3a 0\uff08\u5bf9\u4e8e\u6570\u5b57\u7c7b\u578b\uff09\u3001null\uff08\u5bf9\u4e8e\u5f15\u7528\u7c7b\u578b\uff09\u6216\u5176\u7c7b\u578b\u7279\u5b9a\u7684\u9ed8\u8ba4\u503c\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6570\u7ec4java\u600e\u4e48\u4f7f\u7528\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>java \u6570\u7ec4\u662f\u4e00\u79cd\u5b58\u50a8\u76f8\u540c\u7c7b\u578b\u5143\u7d20\u7684\u6709\u5e8f\u96c6\u5408\uff0c\u53ef\u901a\u8fc7\u4ece 0 \u5f00\u59cb\u7684\u7d22\u5f15\u8bbf\u95ee\u5143\u7d20\u3002\u5b83\u53ef\u901a\u8fc7\u58f0\u660e new \u5173\u952e\u5b57\u548c\u6307\u5b9a\u957f\u5ea6\u6765\u521b\u5efa\uff1aint[] myarray = new int[5];\u3002\u5e38\u89c1\u64cd\u4f5c\u5305\u62ec\u4f7f\u7528 for \u5faa\u73af\u6216 for-each \u5faa\u73af\u904d\u5386\uff0c\u6b64\u5916\u8fd8\u652f\u6301\u957f\u5ea6\u83b7\u53d6\uff0c\u590d\u5236\uff0c\u6392\u5e8f\u548c\u641c\u7d22\u3002\u591a\u7ef4\u6570\u7ec4\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5957\u6570\u7ec4\u5b9e\u73b0\uff0c\u4f46\u6570\u7ec4\u5927\u5c0f\u5728\u521b\u5efa\u540e\u4e0d\u53ef\u66f4\u6539\uff0c\u8bbf\u95ee\u8d85\u51fa\u8303\u56f4\u7684\u7d22\u5f15\u4f1a\u5bfc\u81f4\u5f02\u5e38\u3002 Java \u6570\u7ec4\uff1a\u4f7f\u7528\u6307\u5357 \u4ec0\u4e48\u662f\u6570\u7ec4\uff1f\u6570\u7ec4\u662f\u4e00\u4e2a\u7528\u4e8e\u5b58\u50a8\u76f8\u540c\u7c7b\u578b\u5143\u7d20\u7684\u6709\u5e8f\u96c6\u5408\u3002\u5143\u7d20\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\uff0c\u4ece 0 \u5f00\u59cb\u3002 \u5982\u4f55\u58f0\u660e\u6570\u7ec4\uff1f \/\/ \u58f0\u660e\u4e00\u4e2a int \u7c7b\u578b\u7684\u6570\u7ec4\uff0c\u957f\u5ea6\u4e3a 5 int[] myArray = new int[5]; \u767b\u5f55\u540e\u590d\u5236 \u5982\u4f55\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\uff1f \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \/\/ \u8bbf\u95ee\u5e76\u4fee\u6539\u6570\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20 myArray[0] = 10; \u767b\u5f55\u540e\u590d\u5236 \u5982\u4f55\u904d\u5386\u6570\u7ec4\uff1f\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u5f0f\u904d\u5386\u6570\u7ec4\uff1a for \u5faa\u73af\uff1a for (int i = 0; i &lt; myArray.length; i++) { \/\/ \u5904\u7406 myArray[i] [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-36685","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36685","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=36685"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36685\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=36685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=36685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=36685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}