{"id":251722,"date":"2022-09-11T09:44:58","date_gmt":"2022-09-11T01:44:58","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=251722"},"modified":"2022-09-05T04:45:44","modified_gmt":"2022-09-04T20:45:44","slug":"for-loop-foreach-fast","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/for-loop-foreach-fast.html","title":{"rendered":"For\u5faa\u73af\u548cForeach\u54ea\u4e2a\u66f4\u5feb?\u8fc7\u6765\u770b"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5bf9\u4e8eJava\u4e2d\u7684For\u5faa\u73af\u548cForeach\uff0c\u54ea\u4e2a\u66f4\u5feb\uff1f\u901a\u8fc7\u672c\u6587\uff0c\u60a8\u53ef\u4ee5\u4e86\u89e3\u4e00\u4e9b\u96c6\u5408\u904d\u5386\u6280\u5de7\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

Java\u904d\u5386\u96c6\u5408\u6709\u4e24\u79cd\u65b9\u6cd5\u3002\u4e00\u4e2a\u662f\u6700\u57fa\u672c\u7684for\u5faa\u73af\uff0c\u53e6\u4e00\u4e2a\u662fjdk5\u5f15\u5165\u7684for each\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u904d\u5386\u6570\u7ec4\u548c\u96c6\u5408\u3002\u4f46\u662f\u4f60\u6709\u6ca1\u6709\u60f3\u8fc7\u8fd9\u4e24\u79cd\u65b9\u6cd5\uff1f\u54ea\u4e00\u4e2a\u904d\u5386\u96c6\u5408\u66f4\u6709\u6548\uff1f
\n\"\"<\/p>\n

for-each\u5b9e\u73b0\u65b9\u6cd5<\/strong><\/div>\n

For-each\u4e0d\u662f\u4e00\u79cd\u65b0\u8bed\u6cd5\uff0c\u800c\u662fJava\u7684\u8bed\u6cd5\u7cd6\u3002\u5728\u7f16\u8bd1\u65f6\uff0c\u7f16\u8bd1\u5668\u5c06\u6b64\u4ee3\u7801\u8f6c\u6362\u4e3a\u8fed\u4ee3\u5668\u5b9e\u73b0\uff0c\u5e76\u5c06\u5176\u7f16\u8bd1\u4e3a\u5b57\u8282\u7801\u3002<\/p>\n

\u8bed\u6cd5\u7cd6\uff1a<\/strong><\/span><\/div>\n

\u8bed\u6cd5\u7cd6\uff08Syntactic sugar\uff09\uff0c\u4e5f\u8bd1\u4e3a\u7cd6\u8863\u8bed\u6cd5\uff0c\u662f\u7531\u82f1\u56fd\u8ba1\u7b97\u673a\u79d1\u5b66\u5bb6\u5f7c\u5f97\u00b7\u7ea6\u7ff0\u00b7\u5170\u8fbe\uff08Peter J. Landin\uff09\u53d1\u660e\u7684\u4e00\u4e2a\u672f\u8bed\uff0c\u6307\u8ba1\u7b97\u673a\u8bed\u8a00\u4e2d\u6dfb\u52a0\u7684\u67d0\u79cd\u8bed\u6cd5\uff0c\u8fd9\u79cd\u8bed\u6cd5\u5bf9\u8bed\u8a00\u7684\u529f\u80fd\u5e76\u6ca1\u6709\u5f71\u54cd\uff0c\u4f46\u662f\u66f4\u65b9\u4fbf\u7a0b\u5e8f\u5458\u4f7f\u7528\u3002\u901a\u5e38\u6765\u8bf4\u4f7f\u7528\u8bed\u6cd5\u7cd6\u80fd\u591f\u589e\u52a0\u7a0b\u5e8f\u7684\u53ef\u8bfb\u6027\uff0c\u4ece\u800c\u51cf\u5c11\u7a0b\u5e8f\u4ee3\u7801\u51fa\u9519\u7684\u673a\u4f1a\u3002<\/p>\n

\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6267\u884c\u547d\u4ee4javap-verbose-Testforeach\u53cd\u7f16\u8bd1\u4ee5\u4e0b\u7f16\u8bd1\u4ee3\u7801\uff1a<\/p>\n

\r\npublic class TestForeach {\r\n    List integers;\r\n    public void testForeach(){\r\n        for(Integer i : integers){\r\n\r\n        }\r\n    }\r\n}\r\n<\/integer><\/pre>\n

\u83b7\u5f97\u7684\u8be6\u7ec6\u5b57\u8282\u7801\u5982\u4e0b\uff1a<\/p>\n

\r\npublic void testForeach();\r\n    descriptor: ()V\r\n    flags: ACC_PUBLIC\r\n    Code:\r\n      stack=1, locals=3, args_size=1\r\n         0: aload_0\r\n         1: getfield      #2                  \/\/ Field integers:Ljava\/util\/List;\r\n         4: invokeinterface #3,  1            \/\/ InterfaceMethod java\/util\/List.iterator:()Ljava\/util\/Iterator;\r\n         9: astore_1\r\n        10: aload_1\r\n        11: invokeinterface #4,  1            \/\/ InterfaceMethod java\/util\/Iterator.hasNext:()Z\r\n        16: ifeq          32\r\n        19: aload_1\r\n        20: invokeinterface #5,  1            \/\/ InterfaceMethod java\/util\/Iterator.next:()Ljava\/lang\/Object;\r\n        25: checkcast     #6                  \/\/ class java\/lang\/Integer\r\n        28: astore_2\r\n        29: goto          10\r\n        32: return\r\n      LineNumberTable:\r\n        line 11: 0\r\n        line 13: 29\r\n        line 14: 32\r\n      LocalVariableTable:\r\n        Start  Length  Slot  Name   Signature\r\n           29       0     2     i   Ljava\/lang\/Integer;\r\n            0      33     0  this   Ltest\/TestForeach;\r\n}\r\n<\/pre>\n

\u6b64\u5b57\u8282\u7801\u7684\u4e00\u822c\u542b\u4e49\u662f\u4f7f\u7528getfileld\u547d\u4ee4\u6765\u83b7\u53d6integers\u53d8\u91cf\u5e76\u4e14\u8c03\u7528List.iterator\u6765\u83b7\u53d6\u8fed\u4ee3\u5668\u5b9e\u4f8b\u548c\u8c03\u7528iterator.hasNext\u3002\u5982\u679c\u8fd4\u56detrue\uff0c\u8c03\u7528iterator.next\u65b9\u6cd5\u3002<\/p>\n

\u8bf7\u770b\uff0c\u8fd9\u662f\u8fed\u4ee3\u5668\u904d\u5386\u96c6\u5408\u7684\u5b9e\u73b0\u903b\u8f91\u3002<\/p>\n

\u57fa\u51c6\u6d4b\u8bd5<\/strong><\/div>\n

\u73b0\u5728\u8ba9\u6211\u4eec\u4f7f\u7528for\u5faa\u73af\u65b9\u6cd5\u548cfor-each\u65b9\u6cd5\u8fdb\u884c\u6d4b\u8bd5\u3002<\/p>\n

\r\npublic class ForLoopTest {\r\n\r\n    public static void main(String[] args) {\r\n        List arrayList = new ArrayList<>();\r\n        for (int i = 0; i < 10000000; i++) {\r\n            arrayList.add(i);\r\n        }\r\n\r\n        long arrayListStartTime = System.currentTimeMillis();\r\n        for (int i = 0; i < arrayList.size(); i++) {\r\n            arrayList.get(i);\r\n        }\r\n\r\n        long arrayListCost =System.currentTimeMillis()-arrayListStartTime;\r\n        System.out.println(\"ArrayList for loop traversal cost: \"+ arrayListCost);\r\n\r\n        long arrayListForeachStartTime = System.currentTimeMillis();\r\n        for (Integer integer : arrayList) {\r\n\r\n        }\r\n\r\n        long arrayListForeachCost =System.currentTimeMillis()-arrayListForeachStartTime;\r\n        System.out.println(\"ArrayList foreach traversal cost: \"+ arrayListForeachCost);\r\n<\/integer><\/pre>\n

\u8fd9\u662f\u6d4b\u8bd5\u7ed3\u679c:<\/p>\n

\"\"<\/p>\n

\u5982\u4f60\u6240\u89c1\uff0c\u7ed3\u679c\u662f\u663e\u800c\u6613\u89c1\u7684\u3002\u5bf9\u4e8eArrayList\uff0c\u4f7f\u7528For\u5faa\u73af\u65b9\u6cd5\u7684\u6027\u80fd\u4f18\u4e8eFor each\u65b9\u6cd5\u3002<\/p>\n

\u6211\u4eec\u53ef\u4ee5\u8bf4for\u5faa\u73af\u6bd4for-each\u597d\u5417\uff1f<\/p>\n

\u7b54\u6848\u662f\u5426\u5b9a\u7684\u3002\u5728\u4e0b\u4e00\u4e2a\u57fa\u51c6\u6d4b\u8bd5\u4e2d\uff0c\u6211\u4eec\u5c06ArrayList\u66f4\u6539\u4e3aLinkedList\u3002 \u540c\u6837\uff0c\u8fd9\u91cc\u662f\u6d4b\u8bd5\u7ed3\u679c\u3002<\/p>\n

\"\"<\/p>\n

\u539f\u56e0\u5206\u6790<\/strong><\/div>\n

\u4e00\u4e9b\u521d\u5b66\u8005\u53ef\u80fd\u60f3\u77e5\u9053\u4e3a\u4ec0\u4e48ArrayList\u4f7f\u7528for\u5faa\u73af\u65b9\u6cd5\u904d\u5386\u5f97\u66f4\u5feb\uff0c\u800cLinkedList\u5219\u66f4\u6162\uff0c\u901f\u5ea6\u4e5f\u975e\u5e38\u6162\uff1f<\/p>\n

\u8fd9\u7531ArrayList\u548cLinkedList\u6570\u636e\u7ed3\u6784\u51b3\u5b9a\u3002 ArrayList\u5e95\u5c42\u4f7f\u7528\u6570\u7ec4\u5b58\u50a8\u5143\u7d20\u3002\u6570\u7ec4\u662f\u8fde\u7eed\u7684\u5185\u5b58\u7a7a\u95f4\u3002\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u83b7\u5f97\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO\uff081\uff09\uff0c\u56e0\u6b64\u901f\u5ea6\u5f88\u5feb\u3002<\/p>\n

LinkedList\u7684\u5e95\u5c42\u662f\u4e00\u4e2a\u53cc\u5411\u94fe\u8868\u3002\u4f7f\u7528for\u5faa\u73af\u5b9e\u73b0\u904d\u5386\uff0c\u6bcf\u6b21\u90fd\u9700\u8981\u4ece\u94fe\u8868\u7684\u5934\u8282\u70b9\u5f00\u59cb\u3002\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO\uff08n*n\uff09\u3002<\/p>\n

\u7ed3\u8bba<\/strong><\/div>\n

1.\u4f7f\u7528ArrayList\u65f6\uff0cfor\u5faa\u73af\u65b9\u6cd5\u66f4\u5feb\uff0c\u56e0\u4e3afor-each\u7531\u8fed\u4ee3\u5668\u5b9e\u73b0\uff0c\u5e76\u4e14\u9700\u8981\u6267\u884c\u5e76\u53d1\u4fee\u6539\u9a8c\u8bc1\u3002
\n2.\u4f7f\u7528LinkedList\u65f6\uff0cfor-each\u6bd4for\u5faa\u73af\u5feb\u5f97\u591a\uff0c\u56e0\u4e3aLinkedList\u662f\u901a\u8fc7\u4f7f\u7528\u53cc\u5411\u94fe\u8868\u5b9e\u73b0\u7684\u3002\u6bcf\u4e2a\u5bfb\u5740\u90fd\u9700\u8981\u4ece\u5934\u8282\u70b9\u5f00\u59cb\u3002\u5982\u679c\u6211\u4eec\u9700\u8981\u904d\u5386LinkedList\uff0c\u6211\u4eec\u9700\u8981\u907f\u514d\u4f7f\u7528for\u5faa\u73af\u3002
\n3.\u4f7f\u7528\u8fed\u4ee3\u5668\u6a21\u5f0f\uff0cfor-each\u4e0d\u9700\u8981\u5173\u5fc3\u96c6\u5408\u7684\u5177\u4f53\u5b9e\u73b0\u3002\u5982\u679c\u9700\u8981\u66ff\u6362\u96c6\u5408\uff0c\u65e0\u9700\u4fee\u6539\u4ee3\u7801\u5373\u53ef\u8f7b\u677e\u66ff\u6362\u3002\u200b<\/p>\n","protected":false},"excerpt":{"rendered":"

Java\u904d\u5386\u96c6\u5408\u6709\u4e24\u79cd\u65b9\u6cd5\u3002\u4e00\u4e2a\u662f\u6700\u57fa\u672c\u7684for\u5faa\u73af\uff0c\u53e6\u4e00\u4e2a\u662fjdk5\u5f15\u5165\u7684for each\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u6211 […]<\/p>\n","protected":false},"author":1898,"featured_media":251761,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[908],"class_list":["post-251722","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-for"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/251722","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=251722"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/251722\/revisions"}],"predecessor-version":[{"id":251762,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/251722\/revisions\/251762"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/251761"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=251722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=251722"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=251722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}