{"id":66225,"date":"2025-05-03T15:22:02","date_gmt":"2025-05-03T07:22:02","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66225\/"},"modified":"2025-05-03T15:22:02","modified_gmt":"2025-05-03T07:22:02","slug":"java%e6%80%8e%e4%b9%88%e8%be%93%e5%87%ba%e5%90%ab%e6%9c%89%e6%9f%90%e6%95%b0%e7%9a%84%e6%95%b0%e7%bb%84-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66225\/","title":{"rendered":"java\u600e\u4e48\u8f93\u51fa\u542b\u6709\u67d0\u6570\u7684\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u4f7f\u7528 arrays.tostring() \u65b9\u6cd5\u8f93\u51fa\u542b\u6709\u7279\u5b9a\u6570\u5b57\u7684\u6570\u7ec4\uff1a1. \u5b9a\u4e49\u5e76\u8f93\u5165\u6570\u7ec4\u30022. \u67e5\u627e\u7279\u5b9a\u6570\u5b57\u662f\u5426\u5b58\u5728\u30023. \u6839\u636e\u7ed3\u679c\u8f93\u51fa\u6570\u7ec4\uff1a\u5b58\u5728\u5219\u8f93\u51fa\u5b50\u6570\u7ec4\uff0c\u4e0d\u5b58\u5728\u5219\u8f93\u51fa\u7a7a\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/15\/2024111504243677790.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u8f93\u51fa\u542b\u6709\u67d0\u6570\u7684\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u600e\u4e48\u8f93\u51fa\u542b\u6709\u67d0\u6570\u7684\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 Java \u8f93\u51fa\u542b\u6709\u67d0\u6570\u7684\u6570\u7ec4<\/strong><\/p>\n<p><strong>\u7b54\u6848\uff1a<\/strong><br \/>\u4f7f\u7528 Arrays.toString() \u65b9\u6cd5\uff0c\u5148\u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u7279\u5b9a\u6570\u5b57\u662f\u5426\u5b58\u5728\uff0c\u518d\u6839\u636e\u7ed3\u679c\u8f93\u51fa\u6570\u7ec4\u3002<\/p>\n<p><strong>\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ol>\n<li>\n<p><strong>\u5b9a\u4e49\u6570\u7ec4\u5e76\u8f93\u5165\u503c\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>   int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p><strong>\u67e5\u627e\u7279\u5b9a\u6570\u5b57\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528 Arrays.binarySearch() \u65b9\u6cd5\u67e5\u627e\u6570\u7ec4\u4e2d\u662f\u5426\u5b58\u5728\u7279\u5b9a\u6570\u5b57\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>   int target = 5;\n   int index = Arrays.binarySearch(arr, target);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5982\u679c index &gt;= 0\uff0c\u5219\u6570\u7ec4\u4e2d\u5305\u542b\u6b64\u6570\u5b57\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u6839\u636e\u7ed3\u679c\u8f93\u51fa\u6570\u7ec4\uff1a<\/strong><\/p>\n<ul>\n<li>\n<p>\u5982\u679c\u627e\u5230\u6570\u5b57\uff0c\u8f93\u51fa\u5305\u542b\u8be5\u6570\u5b57\u7684\u5b50\u6570\u7ec4\uff1a<\/p>\n<pre>   System.out.println(Arrays.toString(Arrays.copyOfRange(arr, 0, index + 1)));<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u5982\u679c\u672a\u627e\u5230\u6570\u5b57\uff0c\u8f93\u51fa\u7a7a\u6570\u7ec4\uff1a<\/p>\n<pre>   System.out.println(Arrays.toString(new int[0]));<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u8f93\u51fa\u542b\u6709\u67d0\u6570\u7684\u6570\u7ec4\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 arrays.tostring() \u65b9\u6cd5\u8f93\u51fa\u542b\u6709\u7279\u5b9a\u6570\u5b57\u7684\u6570\u7ec4\uff1a1. \u5b9a\u4e49\u5e76\u8f93\u5165\u6570\u7ec4\u30022. \u67e5\u627e\u7279\u5b9a\u6570\u5b57\u662f\u5426\u5b58\u5728\u30023. \u6839\u636e\u7ed3\u679c\u8f93\u51fa\u6570\u7ec4\uff1a\u5b58\u5728\u5219\u8f93\u51fa\u5b50\u6570\u7ec4\uff0c\u4e0d\u5b58\u5728\u5219\u8f93\u51fa\u7a7a\u6570\u7ec4\u3002 \u5982\u4f55\u4f7f\u7528 Java \u8f93\u51fa\u542b\u6709\u67d0\u6570\u7684\u6570\u7ec4 \u7b54\u6848\uff1a\u4f7f\u7528 Arrays.toString() \u65b9\u6cd5\uff0c\u5148\u5728\u6570\u7ec4\u4e2d\u67e5\u627e\u7279\u5b9a\u6570\u5b57\u662f\u5426\u5b58\u5728\uff0c\u518d\u6839\u636e\u7ed3\u679c\u8f93\u51fa\u6570\u7ec4\u3002 \u8be6\u7ec6\u6b65\u9aa4\uff1a \u5b9a\u4e49\u6570\u7ec4\u5e76\u8f93\u5165\u503c\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; \u767b\u5f55\u540e\u590d\u5236 \u67e5\u627e\u7279\u5b9a\u6570\u5b57\uff1a \u4f7f\u7528 Arrays.binarySearch() \u65b9\u6cd5\u67e5\u627e\u6570\u7ec4\u4e2d\u662f\u5426\u5b58\u5728\u7279\u5b9a\u6570\u5b57\u3002\u4f8b\u5982\uff1a int target = 5; int index = Arrays.binarySearch(arr, target); \u767b\u5f55\u540e\u590d\u5236 \u5982\u679c index &gt;= 0\uff0c\u5219\u6570\u7ec4\u4e2d\u5305\u542b\u6b64\u6570\u5b57\u3002 \u6839\u636e\u7ed3\u679c\u8f93\u51fa\u6570\u7ec4\uff1a \u5982\u679c\u627e\u5230\u6570\u5b57\uff0c\u8f93\u51fa\u5305\u542b\u8be5\u6570\u5b57\u7684\u5b50\u6570\u7ec4\uff1a System.out.println(Arrays.toString(Arrays.copyOfRange(arr, 0, index + 1))); \u767b\u5f55\u540e\u590d\u5236 [&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-66225","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66225","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=66225"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66225\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}