{"id":37394,"date":"2023-12-11T14:41:17","date_gmt":"2023-12-11T06:41:17","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=37394"},"modified":"2023-12-11T14:41:17","modified_gmt":"2023-12-11T06:41:17","slug":"randomaccess%e6%8e%a5%e5%8f%a3%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/11\/randomaccess%e6%8e%a5%e5%8f%a3%ef%bc%9f\/","title":{"rendered":"RandomAccess\u63a5\u53e3\uff1f"},"content":{"rendered":"<p>&#8220;`&#8221;                    \u67e5\u770b\u6e90\u7801\u6211\u4eec\u53d1\u73b0\u5b9e\u9645\u4e0a <code>RandomAccess<\/code> \u63a5\u53e3\u4e2d\u4ec0\u4e48\u90fd\u6ca1\u6709\u5b9a\u4e49\u3002\u6240\u4ee5\uff0c\u5728\u6211\u770b\u6765 <code>RandomAccess<\/code> \u63a5\u53e3\u4e0d\u8fc7\u662f\u4e00\u4e2a\u6807\u8bc6\u7f62\u4e86\u3002\u6807\u8bc6\u4ec0\u4e48\uff1f \u6807\u8bc6\u5b9e\u73b0\u8fd9\u4e2a\u63a5\u53e3\u7684\u7c7b\u5177\u6709\u968f\u673a\u8bbf\u95ee\u529f\u80fd\u3002<\/p>\n<p>\u5728 &lt;code&gt;binarySearch\uff08&lt;\/code&gt;\uff09\u65b9\u6cd5\u4e2d\uff0c\u5b83\u8981\u5224\u65ad\u4f20\u5165\u7684list \u662f\u5426 &lt;code&gt;RamdomAccess&lt;\/code&gt; \u7684\u5b9e\u4f8b\uff0c\u5982\u679c\u662f\uff0c\u8c03\u7528&lt;code&gt;indexedBinarySearch\uff08\uff09&lt;\/code&gt;\u65b9\u6cd5\uff0c\u5982\u679c\u4e0d\u662f\uff0c\u90a3\u4e48\u8c03\u7528&lt;code&gt;iteratorBinarySearch\uff08\uff09&lt;\/code&gt;\u65b9\u6cd5&lt;\/p&gt;<\/p>\n<p>&lt;pre&gt;&lt;code&gt;java    public static &lt;T&gt;    int binarySearch(List&lt;? extends Comparable&lt;? super T&gt;&gt; list, T key) {        if (list instanceof RandomAccess || list.size()&lt;BINARYSEARCH_THRESHOLD)            return Collections.indexedBinarySearch(list, key);        else            return Collections.iteratorBinarySearch(list, key);    }&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>&lt;code&gt;ArrayList&lt;\/code&gt; \u5b9e\u73b0\u4e86 &lt;code&gt;RandomAccess&lt;\/code&gt; \u63a5\u53e3\uff0c \u800c &lt;code&gt;LinkedList&lt;\/code&gt; \u6ca1\u6709\u5b9e\u73b0\u3002\u4e3a\u4ec0\u4e48\u5462\uff1f\u6211\u89c9\u5f97\u8fd8\u662f\u548c\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u6709\u5173\uff01&lt;code&gt;ArrayList&lt;\/code&gt; \u5e95\u5c42\u662f\u6570\u7ec4\uff0c\u800c &lt;code&gt;LinkedList&lt;\/code&gt; \u5e95\u5c42\u662f\u94fe\u8868\u3002\u6570\u7ec4\u5929\u7136\u652f\u6301\u968f\u673a\u8bbf\u95ee\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O\uff081\uff09\uff0c\u6240\u4ee5\u79f0\u4e3a\u5feb\u901f\u968f\u673a\u8bbf\u95ee\u3002\u94fe\u8868\u9700\u8981\u904d\u5386\u5230\u7279\u5b9a\u4f4d\u7f6e\u624d\u80fd\u8bbf\u95ee\u7279\u5b9a\u4f4d\u7f6e\u7684\u5143\u7d20\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O\uff08n\uff09\uff0c\u6240\u4ee5\u4e0d\u652f\u6301\u5feb\u901f\u968f\u673a\u8bbf\u95ee\u3002\uff0c&lt;code&gt;ArrayList&lt;\/code&gt; \u5b9e\u73b0\u4e86 &lt;code&gt;RandomAccess&lt;\/code&gt; \u63a5\u53e3\uff0c\u5c31\u8868\u660e\u4e86\u4ed6\u5177\u6709\u5feb\u901f\u968f\u673a\u8bbf\u95ee\u529f\u80fd\u3002 &lt;code&gt;RandomAccess&lt;\/code&gt; \u63a5\u53e3\u53ea\u662f\u6807\u8bc6\uff0c\u5e76\u4e0d\u662f\u8bf4 &lt;code&gt;ArrayList&lt;\/code&gt; \u5b9e\u73b0 &lt;code&gt;RandomAccess&lt;\/code&gt; \u63a5\u53e3\u624d\u5177\u6709\u5feb\u901f\u968f\u673a\u8bbf\u95ee\u529f\u80fd\u7684\uff01<\/p>\n<p>&lt;strong&gt;\u4e0b\u9762\u518d\u603b\u7ed3\u4e00\u4e0b list \u7684\u904d\u5386\u65b9\u5f0f\u9009\u62e9\uff1a&lt;\/strong&gt;<\/p>\n<p>&lt;ul&gt;&lt;li&gt;\u5b9e\u73b0\u4e86 &lt;code&gt;RandomAccess&lt;\/code&gt; \u63a5\u53e3\u7684list\uff0c\u4f18\u5148\u9009\u62e9\u666e\u901a for \u5faa\u73af \uff0c\u5176\u6b21 foreach,&lt;\/li&gt;&lt;li&gt;\u672a\u5b9e\u73b0 &lt;code&gt;RandomAccess&lt;\/code&gt;\u63a5\u53e3\u7684list\uff0c\u4f18\u5148\u9009\u62e9iterator\u904d\u5386\uff08foreach\u904d\u5386\u5e95\u5c42\u4e5f\u662f\u901a\u8fc7iterator\u5b9e\u73b0\u7684,\uff09\uff0c\u5927size\u7684\u6570\u636e\uff0c\u5343\u4e07\u4e0d\u8981\u4f7f\u7528\u666e\u901afor\u5faa\u73af&lt;\/li&gt;&lt;\/ul&gt;<\/p>\n<p>&lt;pre&gt;&lt;code&gt;            &quot;&#8220;`<br \/>\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`&#8221; \u67e5\u770b\u6e90\u7801\u6211\u4eec\u53d1\u73b0\u5b9e\u9645\u4e0a RandomAccess \u63a5\u53e3\u4e2d\u4ec0\u4e48\u90fd\u6ca1\u6709\u5b9a\u4e49\u3002\u6240\u4ee5\uff0c [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-37394","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/37394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=37394"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/37394\/revisions"}],"predecessor-version":[{"id":37395,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/37394\/revisions\/37395"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=37394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=37394"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=37394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}