{"id":66734,"date":"2025-05-03T13:40:03","date_gmt":"2025-05-03T05:40:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66734\/"},"modified":"2025-05-03T13:40:03","modified_gmt":"2025-05-03T05:40:03","slug":"java%e6%80%8e%e4%b9%88%e5%8f%96%e4%b8%a4%e4%b8%aa%e6%95%b0%e7%bb%84%e7%9a%84%e4%b8%8b%e6%a0%87-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66734\/","title":{"rendered":"java\u600e\u4e48\u53d6\u4e24\u4e2a\u6570\u7ec4\u7684\u4e0b\u6807"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u67e5\u627e\u4e24\u4e2a\u6570\u7ec4\u4e2d\u76f8\u7b49\u5143\u7d20\u7684\u4e0b\u6807\uff0c\u53ef\u91c7\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u4f7f\u7528\u4e24\u4e2a for \u5faa\u73af\uff1a\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u5d4c\u5957\u5faa\u73af\u6bd4\u8f83\u5143\u7d20\u3002\u4f7f\u7528 java 8 stream\uff1a\u4f7f\u7528 stream api \u8fc7\u6ee4\u548c\u67e5\u627e\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u3002\u4f7f\u7528 hashmap\uff1a\u5bf9\u4e8e\u5143\u7d20\u8f83\u591a\u7684\u6570\u7ec4\uff0c\u4f7f\u7528 hashmap \u5b58\u50a8\u4e00\u4e2a\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u7136\u540e\u904d\u5386\u53e6\u4e00\u4e2a\u6570\u7ec4\u67e5\u627e\u76f8\u7b49\u5143\u7d20\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111620492325113.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u53d6\u4e24\u4e2a\u6570\u7ec4\u7684\u4e0b\u6807\u63d2\u56fe\" alt=\"java\u600e\u4e48\u53d6\u4e24\u4e2a\u6570\u7ec4\u7684\u4e0b\u6807\u63d2\u56fe\" \/><\/p>\n<h2>Java \u4e2d\u53d6\u4e24\u4e2a\u6570\u7ec4\u7684\u4e0b\u6807<\/h2>\n<h3>\u4f7f\u7528\u4e24\u4e2a for \u5faa\u73af<\/h3>\n<p>\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u4e24\u4e2a for \u5faa\u73af\u8fed\u4ee3\u6570\u7ec4\uff0c\u5e76\u5728\u627e\u5230\u76f8\u7b49\u5143\u7d20\u65f6\u8bb0\u5f55\u4e0b\u6807\uff1a<\/p>\n<pre>int[] arr1 = {1, 2, 3, 4, 5};\nint[] arr2 = {2, 4, 6, 8, 10};\nint index1 = -1;\nint index2 = -1;\n\nfor (int i = 0; i &lt; arr1.length; i++) {\n    for (int j = 0; j &lt; arr2.length; j++) {\n        if (arr1[i] == arr2[j]) {\n            index1 = i;\n            index2 = j;\n            break; \/\/ \u627e\u5230\u540e\u5373\u53ef\u8df3\u51fa\u5faa\u73af\n        }\n    }\n    if (index1 != -1) { \/\/ \u5728\u7b2c\u4e00\u4e2a\u6570\u7ec4\u4e2d\u627e\u5230\u5bf9\u5e94\u5143\u7d20\n        break; \/\/ \u8df3\u51fa\u5916\u5c42\u5faa\u73af\uff0c\u907f\u514d\u91cd\u590d\u67e5\u627e\n    }\n}\n\n\/\/ \u6253\u5370\u4e0b\u6807\nSystem.out.println(\"arr1 \u7684\u4e0b\u6807\uff1a\" + index1);\nSystem.out.println(\"arr2 \u7684\u4e0b\u6807\uff1a\" + index2);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3>\u4f7f\u7528 Java 8 Stream<\/h3>\n<p>Java 8 \u53ca\u66f4\u9ad8\u7248\u672c\u53ef\u4ee5\u4f7f\u7528 Stream API \u6765\u7b80\u5316\u67e5\u627e\u8fc7\u7a0b:<\/p>\n<pre>int index1 = IntStream.range(0, arr1.length)\n        .filter(i -&gt; arr2[i] == arr1[i])\n        .findFirst()\n        .orElse(-1);\n\nint index2 = index1 == -1 ? -1 : arr2[index1];<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3>\u4f7f\u7528 HashMap<\/h3>\n<p>\u5982\u679c\u4e24\u4e2a\u6570\u7ec4\u7684\u5143\u7d20\u8f83\u591a\uff0c\u53ef\u4ee5\u4f7f\u7528 HashMap \u6765\u52a0\u901f\u67e5\u627e:<\/p>\n<pre>Map&lt;Integer, Integer&gt; map = new HashMap&lt;&gt;();\nfor (int i = 0; i &lt; arr2.length; i++) {\n    map.put(arr2[i], i);\n}\n\nfor (int i = 0; i &lt; arr1.length; i++) {\n    if (map.containsKey(arr1[i])) {\n        index1 = i;\n        index2 = map.get(arr1[i]);\n        break;\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<ul>\n<li>\u5982\u679c\u6570\u7ec4\u4e2d\u6ca1\u6709\u76f8\u7b49\u5143\u7d20\uff0c\u5219 index1 \u548c index2 \u5c06\u4e3a -1\u3002<\/li>\n<li>\u4e24\u4e2a\u6570\u7ec4\u7684\u957f\u5ea6\u5fc5\u987b\u76f8\u7b49\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u53d6\u4e24\u4e2a\u6570\u7ec4\u7684\u4e0b\u6807\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>java \u4e2d\u67e5\u627e\u4e24\u4e2a\u6570\u7ec4\u4e2d\u76f8\u7b49\u5143\u7d20\u7684\u4e0b\u6807\uff0c\u53ef\u91c7\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u4f7f\u7528\u4e24\u4e2a for \u5faa\u73af\uff1a\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u5d4c\u5957\u5faa\u73af\u6bd4\u8f83\u5143\u7d20\u3002\u4f7f\u7528 java 8 stream\uff1a\u4f7f\u7528 stream api \u8fc7\u6ee4\u548c\u67e5\u627e\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u3002\u4f7f\u7528 hashmap\uff1a\u5bf9\u4e8e\u5143\u7d20\u8f83\u591a\u7684\u6570\u7ec4\uff0c\u4f7f\u7528 hashmap \u5b58\u50a8\u4e00\u4e2a\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u7136\u540e\u904d\u5386\u53e6\u4e00\u4e2a\u6570\u7ec4\u67e5\u627e\u76f8\u7b49\u5143\u7d20\u3002 Java \u4e2d\u53d6\u4e24\u4e2a\u6570\u7ec4\u7684\u4e0b\u6807 \u4f7f\u7528\u4e24\u4e2a for \u5faa\u73af \u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u4e24\u4e2a for \u5faa\u73af\u8fed\u4ee3\u6570\u7ec4\uff0c\u5e76\u5728\u627e\u5230\u76f8\u7b49\u5143\u7d20\u65f6\u8bb0\u5f55\u4e0b\u6807\uff1a int[] arr1 = {1, 2, 3, 4, 5}; int[] arr2 = {2, 4, 6, 8, 10}; int index1 = -1; int index2 = -1; for (int i = 0; i &lt; arr1.length; i++) { [&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-66734","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66734","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=66734"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66734\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}