{"id":4629,"date":"2020-01-10T18:41:31","date_gmt":"2020-01-10T10:41:31","guid":{"rendered":"https:\/\/www.xttblog.com\/?p=4629"},"modified":"2020-01-10T18:41:31","modified_gmt":"2020-01-10T10:41:31","slug":"%e8%af%a6%e8%a7%a3-java-arraylist%e4%bd%bf%e7%94%a8add%e3%80%81set%e6%8a%9b%e5%87%baindexoutofboundsexception%e5%bc%82%e5%b8%b8%e7%9a%84%e9%97%ae%e9%a2%98%ef%bc%81","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=4629","title":{"rendered":"\u8be6\u89e3 Java ArrayList\u4f7f\u7528add\u3001set\u629b\u51faIndexOutOfBoundsException\u5f02\u5e38\u7684\u95ee\u9898\uff01"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u6700\u8fd1\u4e24\u5929\uff0c\u6709\u540c\u4e8b\u53d1\u73b0\u4e86\u201c\u65b0\u5927\u9646\u201d\u3002\u8bf4\u662f\u53d1\u73b0\u4e86 JDK \u7684 bug\uff0c\u5176\u5b9e\u662f\u6ca1\u7406\u89e3 JDK \u7684\u8bbe\u8ba1\u601d\u8def\uff01<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u95ee\u9898\u7684\u8d77\u56e0\u662f\u56e0\u4e3a\uff0c\u8fd9\u4f4d\u540c\u4e8b\u5728\u4f7f\u7528 ArrayList \u65f6\uff0c\u629b\u51fa\u4e86 IndexOutOfBoundsException \u5f02\u5e38\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List xttblog = new ArrayList&lt;Integer&gt;();\nxttblog.add(2, 8);\n\n\/\/ \u6216\u8005\nxttblog = new ArrayList&lt;Integer&gt;(10);\nxttblog.add(2, 8);\n\n\/\/ \u6216\u8005\nxttblog = new ArrayList&lt;Integer&gt;(10);\nxttblog.set(2, 8);\n\n\/\/ \u6216\u8005\nxttblog = new ArrayList&lt;Integer&gt;();\nxttblog.add(1);\nxttblog.add(2);\nxttblog.clear();\nxttblog.set(2,6);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6bd4\u5982\u4e0a\u9762\u7684\u51e0\u79cd\u7528\u6cd5\uff0c\u90fd\u4f1a\u5bfc\u81f4\u8fd9\u6bb5\u4ee3\u7801\u629b\u51fa IndexOutOfBoundsException \u5f02\u5e38\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/mw690\/8fa5dcfcgy1garnhtdob2j20rs0hstbf.jpg\" alt=\"\" \/><figcaption>IndexOutOfBoundsException<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u7a76\u5176\u539f\u56e0\u662f\u56e0\u4e3a\uff0cset(int index, E element) \u65b9\u6cd5\u548c add(int index, E element) \u65b9\u6cd5\u4e2d\u90fd\u4f1a\u68c0\u6d4b\u6570\u7ec4\u7684\u8fb9\u754c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u662f\uff0c\u4f60\u7ec6\u7ec6\u7684\u770b\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u6211\u4eec add \u6216\u8005 set \u7684\u65f6\u5019\uff0c\u90fd\u6ca1\u6709\u8d85\u8fc7 ArrayList \u7684 size\uff0c\u4e3a\u4ec0\u4e48\u8fd8\u4f1a\u62a5\u8d8a\u754c\u95ee\u9898\u5462\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u662f\u56e0\u4e3a\uff0cArrayList \u7684\u5e95\u5c42\u672c\u8d28\u4e0a\u8fd8\u662f\u4e00\u4e2a\u6570\u7ec4\u3002ArrayList \u672c\u8eab\u662f\u8981\u6c42\u81ea\u5df1\u7684\u6570\u7ec4\u8981\u4fdd\u6301\u8fde\u7eed\u6027\u3002\u6bcf\u5f53\u5220\u9664\u4e00\u4e2a\u5143\u7d20\uff0c\u6211\u4eec\u90fd\u4f1a\u79fb\u52a8\u6570\u7ec4\u7684\u3002\u56e0\u4e3a size \u5927\u5c0f\u53d8\u4e86\uff0c\u6240\u4ee5\u5fc5\u987b\u8981\u79fb\u52a8\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u800c\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u56e0\u4e3a\u524d\u9762\u7684\u5143\u7d20\u90fd\u6ca1\u6709\u63d2\u5165\u6570\u636e\u3002size \u4e3a 0\uff0c\u6b64\u65f6\u4f60\u8981\u8df3\u8fc7\u524d\u9762\u7684 size \u90e8\u5206\u63d2\u5165\u5143\u7d20\uff0c\u5c31\u4f1a\u53d1\u751f\u8d8a\u754c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u73b0\u5728\u5047\u8bbe\uff0c\u4e0d\u62a5\u8d8a\u754c\u7684\u5f02\u5e38\u3002\u5373\u4f7f\u4f60\u63d2\u5165\u6210\u529f\u4e86\uff0c\u5f53\u524d size \u968f\u4e4b\u6539\u4e3a 1\uff0c\u540e\u9762\u6211\u4eec\u518d\u8c03\u7528 add(E e) \u8fdb\u884c\u6dfb\u52a0\u5143\u7d20\uff0c\u5c31\u4f1a\u8df3\u8fc7\u4e0b\u6807\u4e3a 0 \u7684\u4f4d\u7f6e\u3002\u7136\u540e\u5f53 add \u5230\u6211\u4eec\u521a\u624d set(int index, E element) \u65b9\u6cd5\u548c add(int index, E element) \u7684\u4f4d\u7f6e\u7684\u65f6\u5019\uff0c\u5c31\u4f1a\u53d1\u751f\u8986\u76d6\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\uff0c\u4f60\u73b0\u5728\u5e94\u8be5\u660e\u767d\u4e86 JDK \u8bbe\u8ba1 ArrayList \u7684\u715e\u8d39\u82e6\u5fc3\u3002\u6240\u4ee5\uff0c\u5e73\u65f6\u8fd8\u662f\u8981\u591a\u8bfb\u8bfb\u6e90\u7801\uff0c\u591a\u601d\u8003\u601d\u8003\uff01<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=4629\">\u8be6\u89e3 Java ArrayList\u4f7f\u7528add\u3001set\u629b\u51faIndexOutOfBoundsException\u5f02\u5e38\u7684\u95ee\u9898\uff01<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u4e24\u5929\uff0c\u6709\u540c\u4e8b\u53d1\u73b0\u4e86\u201c\u65b0\u5927\u9646\u201d\u3002\u8bf4\u662f\u53d1\u73b0\u4e86 JDK \u7684 bug\uff0c\u5176\u5b9e\u662f\u6ca1\u7406\u89e3 JDK \u7684\u8bbe\u8ba1\u601d\u8def\uff01 \u95ee\u9898\u7684\u8d77 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1741,1204,1743,1592,1166],"class_list":["post-4629","post","type-post","status-publish","format-standard","hentry","category-java","tag-arraylist","tag-filelist","tag-linkedlist","tag-skiplist","tag-vector"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4629","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4629"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4629\/revisions"}],"predecessor-version":[{"id":4630,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4629\/revisions\/4630"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}