{"id":36601,"date":"2024-11-26T10:34:10","date_gmt":"2024-11-26T02:34:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/36601\/"},"modified":"2024-11-26T10:34:10","modified_gmt":"2024-11-26T02:34:10","slug":"java%e6%95%b0%e7%bb%84%e4%b8%ad%e6%9c%89%e5%93%aa%e4%ba%9b%e9%9d%a2%e8%af%95%e9%a2%98","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/36601\/","title":{"rendered":"java\u6570\u7ec4\u4e2d\u6709\u54ea\u4e9b\u9762\u8bd5\u9898"},"content":{"rendered":"<blockquote><p>\n  java \u6570\u7ec4\u63d0\u4f9b\u4e86\u5b58\u50a8\u540c\u7c7b\u578b\u5143\u7d20\u7684\u6709\u5e8f\u96c6\u5408\u3002\u53ef\u4ee5\u58f0\u660e\u4e00\u4e2a\u56fa\u5b9a\u5bb9\u91cf\u7684\u6570\u7ec4\u5e76\u4f7f\u7528\u6570\u7ec4\u5b57\u9762\u91cf\u6216 new \u8bed\u6cd5\u521d\u59cb\u5316\u5b83\u3002\u4e0e arraylist \u76f8\u6bd4\uff0c\u6570\u7ec4\u7684\u5bb9\u91cf\u56fa\u5b9a\uff0c\u53ea\u80fd\u5b58\u50a8\u76f8\u540c\u5143\u7d20\uff0c\u4f46\u5728\u5904\u7406\u5927\u91cf\u539f\u59cb\u7c7b\u578b\u65f6\u6027\u80fd\u66f4\u4f73\u3002\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6216 foreach \u5faa\u73af\u3002\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\u6216\u7ebf\u6027\u641c\u7d22\u53ef\u4ee5\u67e5\u627e\u5143\u7d20\u7684\u7d22\u5f15\u3002\u53ef\u4ee5\u4f7f\u7528 arrays.copyof \u6216 system.arraycopy \u590d\u5236\u6570\u7ec4\u3002\u901a\u8fc7 arrays.sort \u8fdb\u884c\u6392\u5e8f\uff0c\u800c\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u53ef\u4ee5\u901a\u8fc7 arrays.stream \u7684 max \u548c m\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/12\/2024111211004381493.jpg\" class=\"aligncenter\" title=\"java\u6570\u7ec4\u4e2d\u6709\u54ea\u4e9b\u9762\u8bd5\u9898\u63d2\u56fe\" alt=\"java\u6570\u7ec4\u4e2d\u6709\u54ea\u4e9b\u9762\u8bd5\u9898\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u6570\u7ec4\u9762\u8bd5\u9898<\/strong><\/p>\n<p><strong>1. \u5982\u4f55\u5728 Java \u4e2d\u58f0\u660e\u548c\u521d\u59cb\u5316\u6570\u7ec4\uff1f<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\u58f0\u660e\u4e00\u4e2a\u6570\u7ec4\uff1aint[] arr = new int[size];<\/li>\n<li>\u4f7f\u7528\u6570\u7ec4\u5b57\u9762\u91cf\u521d\u59cb\u5316\u6570\u7ec4\uff1aint[] arr = {1, 2, 3};<\/li>\n<\/ul>\n<p><strong>2. \u6570\u7ec4\u548c ArrayList \u4e4b\u95f4\u7684\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n<ul>\n<li> <strong>\u5bb9\u91cf\uff1a<\/strong>\u6570\u7ec4\u5177\u6709\u56fa\u5b9a\u5bb9\u91cf\uff0c\u800c ArrayList \u53ef\u4ee5\u52a8\u6001\u589e\u957f\u3002<\/li>\n<li> <strong>\u5143\u7d20\u7c7b\u578b\uff1a<\/strong>\u6570\u7ec4\u53ea\u80fd\u5b58\u50a8\u76f8\u540c\u7c7b\u578b\u7684\u5143\u7d20\uff0c\u800c ArrayList \u53ef\u4ee5\u5b58\u50a8\u5404\u79cd\u5bf9\u8c61\u3002<\/li>\n<li> <strong>\u6027\u80fd\uff1a<\/strong>\u5728\u5904\u7406\u5927\u91cf\u539f\u59cb\u7c7b\u578b\u503c\u65f6\uff0c\u6570\u7ec4\u7684\u6027\u80fd\u4f18\u4e8e ArrayList\u3002<\/li>\n<\/ul>\n<p><strong>3. \u5982\u4f55\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff1f<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li>\u4f7f\u7528\u7d22\u5f15\uff1aarr[index]<\/li>\n<li>\u4f7f\u7528 foreach \u5faa\u73af\u8bbf\u95ee\u6bcf\u4e2a\u5143\u7d20\uff1afor (int x : arr)<\/li>\n<\/ul>\n<p><strong>4. \u5982\u4f55\u67e5\u627e\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u7d22\u5f15\uff1f<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 Arrays.binarySearch(arr, element) \u8fdb\u884c\u4e8c\u5206\u67e5\u627e\uff0c\u524d\u63d0\u662f\u6570\u7ec4\u5df2\u6392\u5e8f\u3002<\/li>\n<li>\u4f7f\u7528\u7ebf\u200b\u200b\u6027\u641c\u7d22\uff1afor (int i = 0; i <\/li>\n<\/ul>\n<p><strong>5. \u5982\u4f55\u590d\u5236\u6570\u7ec4\uff1f<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 Arrays.copyOf(arr, newLength) \u590d\u5236\u4e00\u4e2a\u6307\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\u3002<\/li>\n<li>\u4f7f\u7528 System.arraycopy(arr, 0, newArr, 0, arr.length) \u590d\u5236\u6574\u4e2a\u6570\u7ec4\u3002<\/li>\n<\/ul>\n<p><strong>6. \u5982\u4f55\u5bf9\u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff1f<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 Arrays.sort(arr) \u5bf9\u57fa\u672c\u7c7b\u578b\u8fdb\u884c\u6392\u5e8f\u3002<\/li>\n<li>\u4f7f\u7528 Arrays.sort(arr, Comparator) \u5bf9\u5bf9\u8c61\u8fdb\u884c\u6392\u5e8f\uff0c\u524d\u63d0\u662f\u5b83\u4eec\u5b9e\u73b0\u4e86 Comparable \u63a5\u53e3\u3002<\/li>\n<\/ul>\n<p><strong>7. \u5982\u4f55\u67e5\u627e\u6570\u7ec4\u4e2d\u7684\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\uff1f<\/strong><\/p>\n<ul>\n<li> <strong>\u6700\u5927\u503c\uff1a<\/strong>Arrays.stream(arr).max().getAsInt()<\/li>\n<li> <strong>\u6700\u5c0f\u503c\uff1a<\/strong>Arrays.stream(arr).min().getAsInt()<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u6570\u7ec4\u4e2d\u6709\u54ea\u4e9b\u9762\u8bd5\u9898\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\u63d0\u4f9b\u4e86\u5b58\u50a8\u540c\u7c7b\u578b\u5143\u7d20\u7684\u6709\u5e8f\u96c6\u5408\u3002\u53ef\u4ee5\u58f0\u660e\u4e00\u4e2a\u56fa\u5b9a\u5bb9\u91cf\u7684\u6570\u7ec4\u5e76\u4f7f\u7528\u6570\u7ec4\u5b57\u9762\u91cf\u6216 new \u8bed\u6cd5\u521d\u59cb\u5316\u5b83\u3002\u4e0e arraylist \u76f8\u6bd4\uff0c\u6570\u7ec4\u7684\u5bb9\u91cf\u56fa\u5b9a\uff0c\u53ea\u80fd\u5b58\u50a8\u76f8\u540c\u5143\u7d20\uff0c\u4f46\u5728\u5904\u7406\u5927\u91cf\u539f\u59cb\u7c7b\u578b\u65f6\u6027\u80fd\u66f4\u4f73\u3002\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6216 foreach \u5faa\u73af\u3002\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\u6216\u7ebf\u6027\u641c\u7d22\u53ef\u4ee5\u67e5\u627e\u5143\u7d20\u7684\u7d22\u5f15\u3002\u53ef\u4ee5\u4f7f\u7528 arrays.copyof \u6216 system.arraycopy \u590d\u5236\u6570\u7ec4\u3002\u901a\u8fc7 arrays.sort \u8fdb\u884c\u6392\u5e8f\uff0c\u800c\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u53ef\u4ee5\u901a\u8fc7 arrays.stream \u7684 max \u548c m Java \u6570\u7ec4\u9762\u8bd5\u9898 1. \u5982\u4f55\u5728 Java \u4e2d\u58f0\u660e\u548c\u521d\u59cb\u5316\u6570\u7ec4\uff1f \u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\u58f0\u660e\u4e00\u4e2a\u6570\u7ec4\uff1aint[] arr = new int[size]; \u4f7f\u7528\u6570\u7ec4\u5b57\u9762\u91cf\u521d\u59cb\u5316\u6570\u7ec4\uff1aint[] arr = {1, 2, 3}; 2. \u6570\u7ec4\u548c ArrayList \u4e4b\u95f4\u7684\u662f\u4ec0\u4e48\uff1f \u5bb9\u91cf\uff1a\u6570\u7ec4\u5177\u6709\u56fa\u5b9a\u5bb9\u91cf\uff0c\u800c ArrayList \u53ef\u4ee5\u52a8\u6001\u589e\u957f\u3002 \u5143\u7d20\u7c7b\u578b\uff1a\u6570\u7ec4\u53ea\u80fd\u5b58\u50a8\u76f8\u540c\u7c7b\u578b\u7684\u5143\u7d20\uff0c\u800c ArrayList \u53ef\u4ee5\u5b58\u50a8\u5404\u79cd\u5bf9\u8c61\u3002 \u6027\u80fd\uff1a\u5728\u5904\u7406\u5927\u91cf\u539f\u59cb\u7c7b\u578b\u503c\u65f6\uff0c\u6570\u7ec4\u7684\u6027\u80fd\u4f18\u4e8e ArrayList\u3002 3. \u5982\u4f55\u8bbf\u95ee\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff1f \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u4f7f\u7528\u7d22\u5f15\uff1aarr[index] \u4f7f\u7528 foreach \u5faa\u73af\u8bbf\u95ee\u6bcf\u4e2a\u5143\u7d20\uff1afor [&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-36601","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36601","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=36601"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36601\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=36601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=36601"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=36601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}