{"id":66442,"date":"2025-05-03T15:28:38","date_gmt":"2025-05-03T07:28:38","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66442\/"},"modified":"2025-05-03T15:28:38","modified_gmt":"2025-05-03T07:28:38","slug":"java%e7%9a%84indexof%e6%80%8e%e4%b9%88%e9%85%8d%e5%90%88%e6%95%b0%e7%bb%84%e4%bd%bf%e7%94%a8-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66442\/","title":{"rendered":"java\u7684indexof\u600e\u4e48\u914d\u5408\u6570\u7ec4\u4f7f\u7528"},"content":{"rendered":"<blockquote><p>\n  \u8981\u5c06 java \u7684 indexof() \u65b9\u6cd5\u4e0e\u6570\u7ec4\u7ed3\u5408\u4f7f\u7528\uff0c\u53ef\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u58f0\u660e\u6570\u7ec4\uff0c\u4f7f\u7528 indexof() \u67e5\u627e\u7279\u5b9a\u5143\u7d20\u7d22\u5f15\uff0c\u5e76\u6839\u636e\u8fd4\u56de\u7684\u7d22\u5f15\u503c\uff08&gt;=0 \u8868\u793a\u627e\u5230\uff0c-1 \u8868\u793a\u672a\u627e\u5230\uff09\u5904\u7406\u7ed3\u679c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/16\/2024111603512586610.jpg\" class=\"aligncenter\" title=\"java\u7684indexof\u600e\u4e48\u914d\u5408\u6570\u7ec4\u4f7f\u7528\u63d2\u56fe\" alt=\"java\u7684indexof\u600e\u4e48\u914d\u5408\u6570\u7ec4\u4f7f\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06 Java \u7684 indexOf() \u65b9\u6cd5\u4e0e\u6570\u7ec4\u7ed3\u5408\u4f7f\u7528<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><br \/>indexOf() \u65b9\u6cd5\u53ef\u7528\u4e8e\u786e\u5b9a\u6570\u7ec4\u4e2d\u7279\u5b9a\u5143\u7d20\u7684\u4f4d\u7f6e\u3002\u5b83\u8fd4\u56de\u8be5\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\uff0c\u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de -1\u3002<\/p>\n<p><strong>\u4f7f\u7528indexOf() \u65b9\u6cd5<\/strong><br \/>\u8981\u5c06 indexOf() \u65b9\u6cd5\u4e0e\u6570\u7ec4\u7ed3\u5408\u4f7f\u7528\uff0c\u8bf7\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li> <strong>\u58f0\u660e\u548c\u521d\u59cb\u5316\u6570\u7ec4\uff1a<\/strong>\u58f0\u660e\u5e76\u521d\u59cb\u5316\u4e00\u4e2a\u5305\u542b\u5143\u7d20\u7684\u6570\u7ec4\u3002<\/li>\n<li> <strong>\u4f7f\u7528 indexOf() \u67e5\u627e\u7d22\u5f15\uff1a<\/strong>\u4f7f\u7528 indexOf() \u65b9\u6cd5\u67e5\u627e\u7279\u5b9a\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\u3002\u8be5\u8bed\u6cd5\u4e3a\uff1a<\/li>\n<\/ol>\n<pre>int index = Arrays.indexOf(array, element);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\n<p><strong>\u5904\u7406\u7ed3\u679c\uff1a<\/strong>\u6839\u636e index \u7684\u503c\u5904\u7406\u7ed3\u679c\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li>\u5982\u679c index &gt;= 0\uff0c\u5219\u627e\u5230\u4e86\u5143\u7d20\u5e76\u8fd4\u56de\u5176\u7d22\u5f15\u3002<\/li>\n<li>\u5982\u679c index == -1\uff0c\u5219\u5143\u7d20\u672a\u627e\u5230\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b<\/strong><br \/>\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 indexOf() \u65b9\u6cd5\u67e5\u627e\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u7d22\u5f15\uff1a<\/p>\n<pre>\/\/ \u58f0\u660e\u548c\u521d\u59cb\u5316\u6570\u7ec4\nint[] numbers = {1, 2, 3, 4, 5};\n\n\/\/ \u67e5\u627e\u5143\u7d20 3 \u7684\u7d22\u5f15\nint index = Arrays.indexOf(numbers, 3);\n\n\/\/ \u5904\u7406\u7ed3\u679c\nif (index &gt;= 0) {\n    System.out.println(\"\u5143\u7d20 3 \u7684\u7d22\u5f15\u4e3a\uff1a\" + index);\n} else {\n    System.out.println(\"\u5143\u7d20 3 \u4e0d\u5728\u6570\u7ec4\u4e2d\u3002\");\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>\u5143\u7d20 3 \u7684\u7d22\u5f15\u4e3a\uff1a2<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>indexOf() \u65b9\u6cd5\u67e5\u627e\u7684\u662f\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\u3002\u5982\u679c\u6570\u7ec4\u4e2d\u6709\u591a\u4e2a\u76f8\u540c\u7684\u5143\u7d20\uff0c\u5b83\u53ea\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u7d22\u5f15\u3002<\/li>\n<li>indexOf() \u65b9\u6cd5\u662f\u533a\u5206\u5927\u5c0f\u5199\u7684\u3002\u5982\u679c\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u662f\u5b57\u7b26\u4e32\uff0c\u5219\u9700\u8981\u4f7f\u7528\u76f8\u540c\u7684\u5927\u5c0f\u5199\u8fdb\u884c\u641c\u7d22\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u7684indexof\u600e\u4e48\u914d\u5408\u6570\u7ec4\u4f7f\u7528\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>\u8981\u5c06 java \u7684 indexof() \u65b9\u6cd5\u4e0e\u6570\u7ec4\u7ed3\u5408\u4f7f\u7528\uff0c\u53ef\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u58f0\u660e\u6570\u7ec4\uff0c\u4f7f\u7528 indexof() \u67e5\u627e\u7279\u5b9a\u5143\u7d20\u7d22\u5f15\uff0c\u5e76\u6839\u636e\u8fd4\u56de\u7684\u7d22\u5f15\u503c\uff08&gt;=0 \u8868\u793a\u627e\u5230\uff0c-1 \u8868\u793a\u672a\u627e\u5230\uff09\u5904\u7406\u7ed3\u679c\u3002 \u5982\u4f55\u5c06 Java \u7684 indexOf() \u65b9\u6cd5\u4e0e\u6570\u7ec4\u7ed3\u5408\u4f7f\u7528 \u7b80\u4ecbindexOf() \u65b9\u6cd5\u53ef\u7528\u4e8e\u786e\u5b9a\u6570\u7ec4\u4e2d\u7279\u5b9a\u5143\u7d20\u7684\u4f4d\u7f6e\u3002\u5b83\u8fd4\u56de\u8be5\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\uff0c\u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de -1\u3002 \u4f7f\u7528indexOf() \u65b9\u6cd5\u8981\u5c06 indexOf() \u65b9\u6cd5\u4e0e\u6570\u7ec4\u7ed3\u5408\u4f7f\u7528\uff0c\u8bf7\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a \u58f0\u660e\u548c\u521d\u59cb\u5316\u6570\u7ec4\uff1a\u58f0\u660e\u5e76\u521d\u59cb\u5316\u4e00\u4e2a\u5305\u542b\u5143\u7d20\u7684\u6570\u7ec4\u3002 \u4f7f\u7528 indexOf() \u67e5\u627e\u7d22\u5f15\uff1a\u4f7f\u7528 indexOf() \u65b9\u6cd5\u67e5\u627e\u7279\u5b9a\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7684\u7d22\u5f15\u3002\u8be5\u8bed\u6cd5\u4e3a\uff1a int index = Arrays.indexOf(array, element); \u767b\u5f55\u540e\u590d\u5236 \u5904\u7406\u7ed3\u679c\uff1a\u6839\u636e index \u7684\u503c\u5904\u7406\u7ed3\u679c\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u5982\u679c index &gt;= 0\uff0c\u5219\u627e\u5230\u4e86\u5143\u7d20\u5e76\u8fd4\u56de\u5176\u7d22\u5f15\u3002 \u5982\u679c index == -1\uff0c\u5219\u5143\u7d20\u672a\u627e\u5230\u3002 \u793a\u4f8b\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 indexOf() \u65b9\u6cd5\u67e5\u627e\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u7d22\u5f15\uff1a \/\/ \u58f0\u660e\u548c\u521d\u59cb\u5316\u6570\u7ec4 int[] numbers = {1, 2, [&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-66442","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66442","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=66442"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66442\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}