{"id":37782,"date":"2024-11-26T14:37:53","date_gmt":"2024-11-26T06:37:53","guid":{"rendered":"https:\/\/fwq.ai\/blog\/37782\/"},"modified":"2024-11-26T14:37:53","modified_gmt":"2024-11-26T06:37:53","slug":"%e6%95%b0%e7%bb%84%e4%b8%8b%e6%a0%87%e8%b6%8a%e7%95%8c%e6%80%8e%e4%b9%88%e8%a7%a3%e5%86%b3java","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/37782\/","title":{"rendered":"\u6570\u7ec4\u4e0b\u6807\u8d8a\u754c\u600e\u4e48\u89e3\u51b3java"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u6570\u7ec4\u4e0b\u6807\u8d8a\u754c\u7684\u89e3\u51b3\u65b9\u6848\u5305\u62ec\uff1a\u8fb9\u754c\u68c0\u67e5\uff1a\u5728\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u524d\u8fdb\u884c\u8fb9\u754c\u68c0\u67e5\uff0c\u8d85\u51fa\u8303\u56f4\u5219\u629b\u51fa\u5f02\u5e38\u3002arrays.copyofrange()\uff1a\u521b\u5efa\u6307\u5b9a\u8303\u56f4\u7684\u6570\u7ec4\u526f\u672c\uff0c\u8d85\u51fa\u8303\u56f4\u81ea\u52a8\u622a\u65ad\u3002guava \u5e93\uff1a\u4f7f\u7528 ints.toarray() \u65b9\u6cd5\uff0c\u539f\u5b50\u65b9\u5f0f\u8f6c\u6362 inttointhashmap\uff0c\u9a8c\u8bc1\u6240\u6709\u952e\u662f\u5426\u5728\u6307\u5b9a\u8303\u56f4\u5185\u3002lombok \u6ce8\u89e3\uff1a\u4f7f\u7528 @indexed \u6ce8\u89e3\uff0c\u7f16\u8bd1\u65f6\u81ea\u52a8\u6267\u884c\u8fb9\u754c\u68c0\u67e5\uff0c\u751f\u6210\u9644\u52a0\u7684\u5b89\u5168\u8bbf\u95ee\u65b9\u6cd5\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111310275460078.jpg\" class=\"aligncenter\" title=\"\u6570\u7ec4\u4e0b\u6807\u8d8a\u754c\u600e\u4e48\u89e3\u51b3java\u63d2\u56fe\" alt=\"\u6570\u7ec4\u4e0b\u6807\u8d8a\u754c\u600e\u4e48\u89e3\u51b3java\u63d2\u56fe\" \/><\/p>\n<p><strong>\u6570\u7ec4\u4e0b\u6807\u8d8a\u754c\u5728 Java \u4e2d\u7684\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u6570\u7ec4\u4e0b\u6807\u8d8a\u754c\u9519\u8bef\u53d1\u751f\u5728\u6570\u7ec4\u8bbf\u95ee\u65f6\u8d85\u51fa\u5176\u6709\u6548\u8303\u56f4\u3002\u5bf9\u4e8e\u5177\u6709\u4ece 0 \u5230 N-1 \u4e0b\u6807\u7684\u6570\u7ec4\uff0c\u5982\u679c\u5c1d\u8bd5\u8bbf\u95ee\u4e0b\u6807\u4e3a N \u6216\u8d1f\u6570\u7684\u4e0b\u6807\uff0c\u5c31\u4f1a\u53d1\u751f\u6b64\u9519\u8bef\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p><strong>1. \u8fb9\u754c\u68c0\u67e5<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u5728\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u4e4b\u524d\u8fdb\u884c\u8fb9\u754c\u68c0\u67e5\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>int[] arr = {1, 2, 3};\n\nif (index &gt;= arr.length || index &lt; 0) {\n  throw new ArrayIndexOutOfBoundsException();\n}\n\n\/\/ \u5b89\u5168\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\nSystem.out.println(arr[index]);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 Arrays.copyOfRange()<\/strong><\/p>\n<p>Java API \u63d0\u4f9b\u4e86 Arrays.copyOfRange() \u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u7684\u6570\u7ec4\u526f\u672c\u3002\u5982\u679c\u8bbf\u95ee\u7684\u4e0b\u6807\u8d85\u51fa\u8303\u56f4\uff0c\u5b83\u4f1a\u81ea\u52a8\u622a\u65ad\u5e76\u8fd4\u56de\u526f\u672c\u3002<\/p>\n<pre>int[] safeArr = Arrays.copyOfRange(arr, 0, index + 1);\n\n\/\/ \u5b89\u5168\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\nSystem.out.println(safeArr[0]);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 Guava \u5e93<\/strong><\/p>\n<p>Guava \u5e93\u63d0\u4f9b\u4e86 Ints.toArray() \u65b9\u6cd5\uff0c\u5b83\u4ee5\u539f\u5b50\u65b9\u5f0f\u5c06 IntToIntHashMap \u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u5728\u8f6c\u6362\u671f\u95f4\uff0c\u5b83\u5c06\u9a8c\u8bc1\u6240\u6709\u952e\u662f\u5426\u90fd\u5728\u6307\u5b9a\u8303\u56f4\u5185\u3002<\/p>\n<pre>import com.google.common.collect.Ints;\n\nInts.toArray(map.keys().toArray());<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528 Lombok \u6ce8\u89e3<\/strong><\/p>\n<p>Lombok \u5e93\u63d0\u4f9b\u4e86 @Indexed \u6ce8\u89e3\uff0c\u5b83\u53ef\u4ee5\u5728\u7f16\u8bd1\u65f6\u81ea\u52a8\u6267\u884c\u8fb9\u754c\u68c0\u67e5\u3002\u88ab\u6b64\u6ce8\u89e3\u6807\u8bb0\u7684\u6570\u7ec4\u5c06\u751f\u6210\u9644\u52a0\u7684\u65b9\u6cd5\u6765\u5b89\u5168\u8bbf\u95ee\u5143\u7d20\u3002<\/p>\n<pre>@Indexed(it.units.it.cs.unipd.utility.Unit.SECONDS)\nprivate List&lt;Integer&gt; durations;\n\npublic int getDuration(int index) {\n  return durations.get(index); \/\/ \u5b89\u5168\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u89e3\u51b3\u65b9\u6848\uff0c\u53ef\u4ee5\u9632\u6b62\u6570\u7ec4\u4e0b\u6807\u8d8a\u754c\u9519\u8bef\u5e76\u786e\u4fdd\u6570\u7ec4\u8bbf\u95ee\u7684\u5b89\u5168\u6027\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6570\u7ec4\u4e0b\u6807\u8d8a\u754c\u600e\u4e48\u89e3\u51b3java\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 \u4e2d\u6570\u7ec4\u4e0b\u6807\u8d8a\u754c\u7684\u89e3\u51b3\u65b9\u6848\u5305\u62ec\uff1a\u8fb9\u754c\u68c0\u67e5\uff1a\u5728\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u524d\u8fdb\u884c\u8fb9\u754c\u68c0\u67e5\uff0c\u8d85\u51fa\u8303\u56f4\u5219\u629b\u51fa\u5f02\u5e38\u3002arrays.copyofrange()\uff1a\u521b\u5efa\u6307\u5b9a\u8303\u56f4\u7684\u6570\u7ec4\u526f\u672c\uff0c\u8d85\u51fa\u8303\u56f4\u81ea\u52a8\u622a\u65ad\u3002guava \u5e93\uff1a\u4f7f\u7528 ints.toarray() \u65b9\u6cd5\uff0c\u539f\u5b50\u65b9\u5f0f\u8f6c\u6362 inttointhashmap\uff0c\u9a8c\u8bc1\u6240\u6709\u952e\u662f\u5426\u5728\u6307\u5b9a\u8303\u56f4\u5185\u3002lombok \u6ce8\u89e3\uff1a\u4f7f\u7528 @indexed \u6ce8\u89e3\uff0c\u7f16\u8bd1\u65f6\u81ea\u52a8\u6267\u884c\u8fb9\u754c\u68c0\u67e5\uff0c\u751f\u6210\u9644\u52a0\u7684\u5b89\u5168\u8bbf\u95ee\u65b9\u6cd5\u3002 \u6570\u7ec4\u4e0b\u6807\u8d8a\u754c\u5728 Java \u4e2d\u7684\u89e3\u51b3\u65b9\u6848 \u6570\u7ec4\u4e0b\u6807\u8d8a\u754c\u9519\u8bef\u53d1\u751f\u5728\u6570\u7ec4\u8bbf\u95ee\u65f6\u8d85\u51fa\u5176\u6709\u6548\u8303\u56f4\u3002\u5bf9\u4e8e\u5177\u6709\u4ece 0 \u5230 N-1 \u4e0b\u6807\u7684\u6570\u7ec4\uff0c\u5982\u679c\u5c1d\u8bd5\u8bbf\u95ee\u4e0b\u6807\u4e3a N \u6216\u8d1f\u6570\u7684\u4e0b\u6807\uff0c\u5c31\u4f1a\u53d1\u751f\u6b64\u9519\u8bef\u3002 \u89e3\u51b3\u65b9\u6848 1. \u8fb9\u754c\u68c0\u67e5 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u5728\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u4e4b\u524d\u8fdb\u884c\u8fb9\u754c\u68c0\u67e5\u3002\u4ee3\u7801\u5982\u4e0b\uff1a int[] arr = {1, 2, 3}; if (index &gt;= arr.length || index &lt; 0) { throw new ArrayIndexOutOfBoundsException(); } \/\/ \u5b89\u5168\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20 System.out.println(arr[index]); \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 Arrays.copyOfRange() Java API \u63d0\u4f9b\u4e86 Arrays.copyOfRange() \u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u7684\u6570\u7ec4\u526f\u672c\u3002\u5982\u679c\u8bbf\u95ee\u7684\u4e0b\u6807\u8d85\u51fa\u8303\u56f4\uff0c\u5b83\u4f1a\u81ea\u52a8\u622a\u65ad\u5e76\u8fd4\u56de\u526f\u672c\u3002 [&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-37782","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37782","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=37782"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/37782\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=37782"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=37782"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=37782"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}