{"id":17141,"date":"2025-04-17T13:17:02","date_gmt":"2025-04-17T05:17:02","guid":{"rendered":"https:\/\/92it.top\/?p=17141"},"modified":"2025-04-17T13:18:03","modified_gmt":"2025-04-17T05:18:03","slug":"%e5%9c%a8java%e5%bc%80%e5%8f%91%e4%b8%ad%ef%bc%8c%e4%bd%bf%e7%94%a8arrays-aslist%e6%97%b6%e7%9a%84%e6%bd%9c%e5%9c%a8%e9%99%b7%e9%98%b1%e4%b8%8e%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=17141","title":{"rendered":"\u5728Java\u5f00\u53d1\u4e2d\uff0c\u4f7f\u7528Arrays.asList()\u65f6\u7684\u6f5c\u5728\u9677\u9631\u4e0e\u89e3\u51b3\u65b9\u6848"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\uff1a<a href=\"https:\/\/mp.weixin.qq.com\/s?__biz=MzAxNjk4ODE4OQ==&amp;mid=2247541299&amp;idx=1&amp;sn=e5f1586a6f2994d3d7f96e8863bc57e3&amp;chksm=9ac8e8a67b92539a0a6b494e2a01a09175e259de9de414b44efb009666da95d97757444c7c75&amp;mpshare=1&amp;scene=1&amp;srcid=04172QobbvqdYfgy85dUAyKE&amp;sharer_shareinfo=4952588b8881400a237d708e234142a5&amp;sharer_shareinfo_first=e372b024fc4df1771eb90861ec51aa66&amp;exportkey=n_ChQIAhIQhl3R40fnBvK4%2FgycBwZ6wRL0AQIE97dBBAEAAAAAACiTDSWEmtEAAAAOpnltbLcz9gKNyK89dVj0IjVa96rN1WWK0yuf68uVTcORorVALReMCdtalemCUKvsdI2yWxboWkkrjQYnBJT4tYBjismQ%2FzPKkgTcGiV57YJLmV2YUzknDVDiGThYletMOxe5wpo98Br%2FaTwtvFL3kd0JJH1ztFh79NQxwVuptMbpg9s%2BJacQ1gskXtOLQpJ2FhHU3QJ3kYpIcQABF34Miqi0hriA60cHmAzgh0RjXskXc6ec8qQkf8yFATE1x6FVhaQEMDktbpDyHGxAWXRNca62QhZ80NA%2F4LkxtFo%3D&amp;acctmode=0&amp;pass_ticket=pxRN28BA3wPELAc71cIkBch%2BiuLTW8BAvnCOuqVpRftBgeEqLxn5TriT2kqB1iRj&amp;wx_header=0#rd\">Arrays.asList() \u6570\u7ec4\u8f6c\u6362\u6210\u96c6\u5408\u917f\u6210\u7684\u7ebf\u4e0a\u4e8b\u6545\uff0c\u5dee\u70b9\u8981\u6eda\u86cb\u4e86\uff01<\/a><\/p>\n\n\n\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5728Java\u5f00\u53d1\u4e2d\uff0c\u6570\u7ec4\u4e0e\u96c6\u5408\u4e4b\u95f4\u7684\u8f6c\u6362\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u64cd\u4f5c\u3002<code>Arrays.asList()<\/code>\u65b9\u6cd5\u56e0\u5176\u7b80\u6d01\u7684\u8bed\u6cd5\u548c\u4fbf\u6377\u7684\u4f7f\u7528\u65b9\u5f0f\uff0c\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u96c6\u5408\uff08\u5982List\uff09\u3002<\/p>\n\n\n\n<p>\u7136\u800c\uff0c\u8fd9\u4e2a\u770b\u4f3c\u7b80\u5355\u7684\u65b9\u6cd5\u80cc\u540e\u5374\u9690\u85cf\u7740\u4e00\u4e2a\u5927\u5751\uff0c\u5982\u679c\u4e0d\u5c0f\u5fc3\u5904\u7406\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7ebf\u4e0a\u4e8b\u6545\uff0c\u672c\u6587\u4e5f\u662f\u672c\u4eba\u81ea\u8eab\u9879\u76ee\u9047\u5230\u7684\u60e8\u75db\u7ecf\u5386\uff0c\u5e0c\u671b\u80fd\u591f\u901a\u8fc7\u8bb2\u89e3\u8fd9\u4e2a\u95ee\u9898\uff0c\u8ba9\u5927\u5bb6\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\u907f\u5f00\u8fd9\u4e2a\u5751\u3002<\/p>\n\n\n\n<p><strong>\u4e8b\u6545\u56de\u987e<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u95ee\u9898\u662f\u51fa\u73b0\u5f00\u53d1\u4e00\u4e2a\u7535\u5546\u5e73\u53f0\u7684\u8ba2\u5355\u7cfb\u7edf\u65f6\uff0c\u9047\u5230\u4e86\u7c7b\u4f3c\u7684\u95ee\u9898\u3002\u9700\u8981\u5c06\u4e00\u4e2a\u8ba2\u5355ID\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e00\u4e2aList\uff0c\u5e76\u5728\u540e\u7eed\u7684\u4e1a\u52a1\u903b\u8f91\u4e2d\u5411\u8fd9\u4e2aList\u4e2d\u6dfb\u52a0\u65b0\u7684\u8ba2\u5355ID\u3002<\/p>\n\n\n\n<p>\u7531\u4e8e\u4e2a\u4eba\u7ecf\u9a8c\u4e0d\u8db3\uff0c\u4f7f\u7528\u4e86<code>Arrays.asList()<\/code>\u65b9\u6cd5\uff0c\u90e8\u7f72\u5230\u7ebf\u4e0a\u5bfc\u81f4\u5728\u6dfb\u52a0\u65b0\u8ba2\u5355ID\u65f6\u629b\u51fa\u4e86<code>UnsupportedOperationException<\/code>\u5f02\u5e38\uff0c\u6574\u4e2a\u8ba2\u5355\u5904\u7406\u6d41\u7a0b\u4e2d\u65ad\uff0c\u7ebf\u4e0a\u7cfb\u7edf\u51fa\u73b0\u4e86\u4e25\u91cd\u7684\u6545\u969c\u3002<\/p>\n\n\n\n<p><strong>\u5f71\u54cd\u5206\u6790<\/strong><\/p>\n\n\n\n<p>\u8fd9\u6b21\u4e8b\u6545\u5bf9\u7535\u5546\u5e73\u53f0\u9020\u6210\u4e86\u5de8\u5927\u7684\u5f71\u54cd\uff1a<\/p>\n\n\n\n<ul>\n<li><strong>\u7528\u6237\u4f53\u9a8c\u4e0b\u964d\uff1a<\/strong>&nbsp;\u7531\u4e8e\u8ba2\u5355\u5904\u7406\u6d41\u7a0b\u4e2d\u65ad\uff0c\u7528\u6237\u65e0\u6cd5\u6b63\u5e38\u4e0b\u5355\uff0c\u5bfc\u81f4\u7528\u6237\u4f53\u9a8c\u5927\u5e45\u4e0b\u964d\u3002<\/li>\n\n\n\n<li><strong>\u4e1a\u52a1\u4e2d\u65ad\uff1a<\/strong>&nbsp;\u8ba2\u5355\u7cfb\u7edf\u7684\u6545\u969c\u76f4\u63a5\u5f71\u54cd\u5230\u4e86\u6574\u4e2a\u7535\u5546\u5e73\u53f0\u7684\u4e1a\u52a1\u8fd0\u8425\uff0c\u9020\u6210\u4e86\u5927\u91cf\u7684\u8ba2\u5355\u79ef\u538b\u3002<\/li>\n\n\n\n<li><strong>\u7ecf\u6d4e\u635f\u5931\uff1a<\/strong>&nbsp;\u7531\u4e8e\u4e1a\u52a1\u4e2d\u65ad\uff0c\u5e73\u53f0\u5931\u53bb\u4e86\u5927\u91cf\u7684\u6f5c\u5728\u6536\u5165\uff0c\u7ed9\u516c\u53f8\u5e26\u6765\u4e86\u4e0d\u5c0f\u7684\u7ecf\u6d4e\u635f\u5931\u3002<\/li>\n\n\n\n<li><strong>\u4fe1\u4efb\u5371\u673a\uff1a<\/strong>&nbsp;\u9891\u7e41\u7684\u7cfb\u7edf\u6545\u969c\u8ba9\u7528\u6237\u5bf9\u5e73\u53f0\u7684\u4fe1\u4efb\u5ea6\u4e0b\u964d\uff0c\u53ef\u80fd\u5bfc\u81f4\u7528\u6237\u6d41\u5931\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u540e\u9762\u4e5f\u662f\u53ca\u65f6\u4fee\u590d\uff0c\u5438\u53d6\u672c\u6b21\u4e8b\u6545\u6559\u8bad\u3002<\/p>\n\n\n\n<p><strong>\u4e8b\u6545\u95ee\u9898\u63cf\u8ff0<\/strong><\/p>\n\n\n\n<p>\u9996\u5148\u5148\u7b80\u5355\u63cf\u8ff0\u4e00\u4e0b\u8fd9\u4e2a\u4e8b\u6545\u95ee\u9898\uff0c\u5e9f\u8bdd\u4e0d\u591a\u8bf4\uff0c\u76f4\u63a5\u770b\u5b9e\u9645\u4e1a\u52a1\u4ee3\u7801\uff0c\u4e5f\u5c31\u5c06\u4e00\u4e2a\u6570\u7ec4\u8f6c\u6362\u4e3aList\uff0c\u5e76\u5bf9\u8fd9\u4e2aList\u8fdb\u884c\u589e\u5220\u64cd\u4f5c\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Integer[] arr = {1, 2};\nList&lt;Integer> list = Arrays.asList(arr);\nlist.add(3); \n<\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u4ee3\u7801\u770b\u8d77\u6765\uff0c\u6ca1\u95ee\u9898\uff0c\u7f16\u8bd1\u5668\u4e5f\u6ca1\u6709\u62a5\u9519\u3002\u4f46\u662f\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\u65f6\uff0c\u4f1a\u629b\u51fa<code>UnsupportedOperationException<\/code>\u5f02\u5e38\uff0c\u63d0\u793a\u6211\u4eec\u4e0d\u652f\u6301\u6dfb\u52a0\u64cd\u4f5c\u3002\u8fd9\u7a76\u7adf\u662f\u600e\u4e48\u56de\u4e8b\u5462\uff1f<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"137\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-2-1024x137.png\" alt=\"\" class=\"wp-image-17142\" style=\"width:590px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-2-1024x137.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-2-300x40.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-2-768x103.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-2-830x111.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-2-230x31.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-2-350x47.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-2-480x64.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-2.png 1498w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u6240\u4ee5\u8fd9\u662f\u4e0d\u662f\u4e2a\u5927\u5751\uff0c\u5982\u679c\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u6ca1\u6709\u8fdb\u884c\u81ea\u6d4b\uff0c\u5fc5\u5b9a\u4e0d\u4f1a\u917f\u6210\u91cd\u5927\u7ebf\u4e0a\u4e8b\u6545\u3002<\/p>\n\n\n\n<p><strong>\u95ee\u9898\u5206\u6790<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u4e0a\u8ff0\u5df2\u7ecf\u77e5\u9053\u8fd9\u4e2a\u4f7f\u7528<code>Arrays.asList()<\/code>\u5c06\u6570\u7ec4\u88c5\u6362\u6210List\u4f1a\u5728add\u60c5\u51b5\u4f1a\u62a5\u9519\uff0c\u63a5\u4e0b\u6765\u6df1\u5165\u6e90\u7801\u8fdb\u884c\u5206\u6790\u3002<\/p>\n\n\n\n<p><strong>Arrays.asList()\u7684\u5185\u90e8\u5b9e\u73b0<\/strong><\/p>\n\n\n\n<p><code>Arrays.asList(arr)<\/code>\u65b9\u6cd5\u5b9e\u9645\u4e0a\u8fd4\u56de\u7684\u662f\u4e00\u4e2aArrays\u7c7b\u7684\u5185\u90e8\u7c7b<code>ArrayList<\/code>\uff0c\u800c\u4e0d\u662f\u6211\u4eec\u5e38\u7528\u7684<code>java.util.ArrayList<\/code>\u3002\u8fd9\u4e2a\u5185\u90e8\u7c7b<code>ArrayList<\/code>\u7ee7\u627f\u81ea<code>AbstractList<\/code>\uff0c\u5e76\u6ca1\u6709\u5b9e\u73b0add\u548cremove\u65b9\u6cd5\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"944\" height=\"832\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-5.png\" alt=\"\" class=\"wp-image-17145\" style=\"width:420px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-5.png 944w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-5-300x264.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-5-768x677.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-5-830x732.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-5-230x203.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-5-350x308.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-5-480x423.png 480w\" sizes=\"(max-width: 944px) 100vw, 944px\" \/><\/figure><\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"627\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-3-1024x627.png\" alt=\"\" class=\"wp-image-17143\" style=\"width:504px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-3-1024x627.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-3-300x184.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-3-768x471.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-3-830x509.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-3-230x141.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-3-350x214.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-3-480x294.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-3.png 1348w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>Arrays\u7c7b\u7684\u5185\u90e8\u7c7bArrayList\u5b8c\u6574\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">private static class ArrayList&lt;E> extends AbstractList&lt;E>\n    implements RandomAccess, java.io.Serializable\n{\n    private static final long serialVersionUID = -2764017481108945198L;\n    private final E[] a;\n\n    ArrayList(E[] array) {\n        a = Objects.requireNonNull(array);\n    }\n\n    @Override\n    public int size() {\n        return a.length;\n    }\n\n    @Override\n    public Object[] toArray() {\n        return a.clone();\n    }\n\n    @Override\n    @SuppressWarnings(\"unchecked\")\n    public &lt;T> T[] toArray(T[] a) {\n        int size = size();\n        if (a.length &lt; size)\n            return Arrays.copyOf(this.a, size,\n                                 (Class&lt;? extends T[]>) a.getClass());\n        System.arraycopy(this.a, 0, a, 0, size);\n        if (a.length > size)\n            a[size] = null;\n        return a;\n    }\n\n    @Override\n    public E get(int index) {\n        return a[index];\n    }\n\n    @Override\n    public E set(int index, E element) {\n        E oldValue = a[index];\n        a[index] = element;\n        return oldValue;\n    }\n\n    @Override\n    public int indexOf(Object o) {\n        E[] a = this.a;\n        if (o == null) {\n            for (int i = 0; i &lt; a.length; i++)\n                if (a[i] == null)\n                    return i;\n        } else {\n            for (int i = 0; i &lt; a.length; i++)\n                if (o.equals(a[i]))\n                    return i;\n        }\n        return -1;\n    }\n\n    @Override\n    public boolean contains(Object o) {\n        return indexOf(o) != -1;\n    }\n\n    @Override\n    public Spliterator&lt;E> spliterator() {\n        return Spliterators.spliterator(a, Spliterator.ORDERED);\n    }\n\n    @Override\n    public void forEach(Consumer&lt;? super E> action) {\n        Objects.requireNonNull(action);\n        for (E e : a) {\n            action.accept(e);\n        }\n    }\n\n    @Override\n    public void replaceAll(UnaryOperator&lt;E> operator) {\n        Objects.requireNonNull(operator);\n        E[] a = this.a;\n        for (int i = 0; i &lt; a.length; i++) {\n            a[i] = operator.apply(a[i]);\n        }\n    }\n\n    @Override\n    public void sort(Comparator&lt;? super E> c) {\n        Arrays.sort(a, c);\n    }\n}\n<\/pre>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\u5e76\u6ca1\u6709\u5b9e\u73b0add\u548cremove\u65b9\u6cd5\uff0c\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u5728\u7236\u7c7b\u4e2d\uff0c\u9ed8\u8ba4\u629b\u51fa<code>UnsupportedOperationException<\/code>\u5f02\u5e38\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"411\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-6-1024x411.png\" alt=\"\" class=\"wp-image-17146\" style=\"width:488px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-6-1024x411.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-6-300x121.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-6-768x309.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-6-830x333.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-6-230x92.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-6-350x141.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-6-480x193.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-6.png 1150w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<h4 class=\"wp-block-heading\"><strong>\u5177\u4f53\u539f\u56e0<\/strong><\/h4>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u4e0b\u9762\u662f\u5185\u90e8\u6e90\u7801\u7684\u5206\u6790<\/p>\n\n\n\n<p><strong>1.\u5185\u90e8\u7c7bArrayList\u7684\u9650\u5236\uff1a<\/strong><\/p>\n\n\n\n<ul>\n<li><code>Arrays.asList(arr)<\/code>\u8fd4\u56de\u7684\u5185\u90e8\u7c7bArrayList\u662f\u4e00\u4e2a\u56fa\u5b9a\u957f\u5ea6\u7684\u5217\u8868\uff0c\u5b83\u6ca1\u6709\u5b9e\u73b0add\u548cremove\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li>\u5f53\u6211\u4eec\u8c03\u7528<code>list.add(3)<\/code>\u65f6\uff0c\u5b9e\u9645\u4e0a\u8c03\u7528\u7684\u662f<code>AbstractList<\/code>\u4e2d\u7684add\u65b9\u6cd5\uff0c\u800c\u8fd9\u4e2a\u65b9\u6cd5\u76f4\u63a5\u629b\u51fa\u4e86<code>UnsupportedOperationException<\/code>\u5f02\u5e38\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>2.\u6e90\u7801\u5206\u6790\uff1a<\/strong><\/p>\n\n\n\n<ul>\n<li>AbstractList\u7684add\u65b9\u6cd5\u5982\u4e0b\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public void add(int index, E element) {\n    throw new UnsupportedOperationException();\n}\n<\/pre>\n\n\n\n<ul>\n<li>\u8fd9\u4e2a\u65b9\u6cd5\u6ca1\u6709\u5177\u4f53\u5b9e\u73b0\uff0c\u800c\u662f\u76f4\u63a5\u629b\u51fa\u5f02\u5e38\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5bf9\u6bd4<code>java.util.ArrayList<\/code>\u7c7b\uff0c\u5f88\u660e\u663e\u5b9e\u73b0add\u548cremove\u65b9\u6cd5\uff0c\u5e76\u6ca1\u6709\u76f4\u63a5\u629b\u51fa<code>UnsupportedOperationException<\/code>\u5f02\u5e38<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"656\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-8-1024x656.png\" alt=\"\" class=\"wp-image-17148\" style=\"width:514px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-8-1024x656.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-8-300x192.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-8-768x492.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-8-830x532.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-8-230x147.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-8-350x224.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-8-480x308.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2025\/04\/image-8.png 1398w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u901a\u8fc7\u4e0a\u8ff0\u6e90\u7801\uff0c\u4e3a\u4e86\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\uff0c\u9700\u8981\u4f7f\u7528<code>java.util.ArrayList<\/code>\u5bf9<code>Arrays.asList(arr)<\/code>\u8fd4\u56de\u7684\u5217\u8868\u8fdb\u884c\u5c01\u88c5\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u4f7f\u7528\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u6b63\u5e38\u8fdb\u884c\u589e\u5220\u64cd\u4f5c\u4e86\uff0c\u8be6\u7ec6\u6b65\u9aa4\u5982\u4e0b;<\/p>\n\n\n\n<p><strong>\u8be6\u7ec6\u6b65\u9aa4<\/strong><\/p>\n\n\n\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u7684\u57fa\u7840\u4e0a\u8fdb\u884c\u4ee3\u7801\u4f18\u5316\u4fee\u6539\u3002<\/p>\n\n\n\n<p>1.\u521b\u5efa\u6570\u7ec4\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Integer[] arr = {1, 2};\n<\/pre>\n\n\n\n<p>2.\u8f6c\u6362\u4e3aList\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">List&lt;Integer> list = Arrays.asList(arr);\n<\/pre>\n\n\n\n<p>3.\u5c01\u88c5\u4e3a\u53ef\u53d8List\uff1a\u8fd9\u4e2a\u5c31\u662f\u95ee\u9898\u89e3\u51b3\u7684\u5173\u952e\uff0c\u4f7f\u7528<code>java.util.ArrayList<\/code>\u5bf9<code>Arrays.asList(arr)<\/code>\u8fdb\u884c\u5c01\u88c5<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">ArrayList&lt;Integer> arrayList = new ArrayList&lt;>(Arrays.asList(arr));\n<\/pre>\n\n\n\n<p>4.\u8fdb\u884c\u589e\u5220\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">arrayList.add(3); \/\/ \u6b63\u5e38\u6dfb\u52a0\u5143\u7d20\narrayList.remove(1); \/\/ \u6b63\u5e38\u5220\u9664\u5143\u7d20\n<\/pre>\n\n\n\n<p><strong>\u4ee3\u7801\u793a\u4f8b<\/strong><\/p>\n\n\n\n<p>\u6700\u7ec8\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class Arrays_BugDemo {\n    public static void main(String[] args) {\n        Integer[] arr = {1, 2};\n        List&lt;Integer> list = Arrays.asList(arr);\n        \/\/ \u4f7f\u7528ArrayList\u8fdb\u884c\u5c01\u88c5\n        ArrayList&lt;Integer> arrayList = new ArrayList&lt;>(Arrays.asList(arr));\n        try {\n            list.add(3); \/\/ \u8fd9\u91cc\u4f1a\u62a5\u9519\n        } catch (UnsupportedOperationException e) {\n            System.out.println(\"list.add(3) \u62a5\u9519: \" + e.getMessage());\n        }\n        arrayList.add(3); \/\/ \u73b0\u5728\u53ef\u4ee5\u6b63\u5e38\u6dfb\u52a0\u5143\u7d20\n        arrayList.forEach(System.out::println);\n    }\n}\n<\/pre>\n\n\n\n<p><strong>Java \u4e2d<code>Arrays.asList()<\/code>\u8fd4\u56de\u4e0d\u53ef\u53d8\u7684<code>List<\/code>\uff0c\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u65b9\u9762\u539f\u56e0\uff1a<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u6570\u636e\u4e00\u81f4\u6027\u4e0e\u5b89\u5168\u6027<\/p>\n\n\n\n<ul>\n<li><strong>\u9632\u6b62\u610f\u5916\u4fee\u6539<\/strong>\uff1a<code>Arrays.asList()<\/code>\u8fd4\u56de\u7684<code>List<\/code>\u662f\u57fa\u4e8e\u539f\u59cb\u6570\u7ec4\u521b\u5efa\u7684\u89c6\u56fe\uff0c\u5176\u76ee\u7684\u662f\u4e3a\u4e86\u65b9\u4fbf\u4ee5<code>List<\/code>\u7684\u5f62\u5f0f\u64cd\u4f5c\u6570\u7ec4\u5185\u5bb9\u3002\u5982\u679c\u5141\u8bb8\u5bf9\u8fd9\u4e2a<code>List<\/code>\u8fdb\u884c\u6dfb\u52a0\u3001\u5220\u9664\u7b49\u7ed3\u6784\u4fee\u6539\u64cd\u4f5c\uff0c\u5c31\u4f1a\u5bfc\u81f4\u539f\u59cb\u6570\u7ec4\u7684\u957f\u5ea6\u548c\u5185\u5bb9\u53d1\u751f\u53d8\u5316\uff0c\u8fd9\u53ef\u80fd\u4f1a\u8fdd\u80cc\u6570\u7ec4\u672c\u8eab\u4f5c\u4e3a\u56fa\u5b9a\u957f\u5ea6\u6570\u636e\u7ed3\u6784\u7684\u8bbe\u8ba1\u521d\u8877\uff0c\u4e5f\u5bb9\u6613\u5728\u7a0b\u5e8f\u4e2d\u5f15\u5165\u96be\u4ee5\u6392\u67e5\u7684\u9519\u8bef\u3002\u4f8b\u5982\uff0c\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\uff0c\u591a\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u540c\u65f6\u8bbf\u95ee\u8fd9\u4e2a<code>List<\/code>\u548c\u539f\u59cb\u6570\u7ec4\uff0c\u5982\u679c<code>List<\/code>\u53ef\u968f\u610f\u4fee\u6539\uff0c\u53ef\u80fd\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>\u4fdd\u62a4\u539f\u59cb\u6570\u636e<\/strong>\uff1a\u5728\u5f88\u591a\u573a\u666f\u4e0b\uff0c\u6211\u4eec\u5e0c\u671b\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a<code>List<\/code>\u540e\uff0c\u53ea\u662f\u7528\u4e8e\u67e5\u8be2\u3001\u904d\u5386\u7b49\u64cd\u4f5c\uff0c\u5e76\u4e0d\u5e0c\u671b\u539f\u59cb\u6570\u636e\u88ab\u610f\u5916\u7be1\u6539\u3002\u8fd4\u56de\u4e0d\u53ef\u53d8\u7684<code>List<\/code>\u53ef\u4ee5\u786e\u4fdd\u539f\u59cb\u6570\u7ec4\u7684\u6570\u636e\u5b89\u5168\u6027\uff0c\u8ba9\u5f00\u53d1\u8005\u66f4\u653e\u5fc3\u5730\u4f7f\u7528\u8f6c\u6362\u540e\u7684<code>List<\/code>\u8fdb\u884c\u76f8\u5173\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u8bbe\u8ba1\u610f\u56fe\u4e0e\u529f\u80fd\u5b9a\u4f4d<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>\u8f7b\u91cf\u7ea7\u89c6\u56fe<\/strong>\uff1a<code>Arrays.asList()<\/code>\u4e3b\u8981\u662f\u63d0\u4f9b\u4e00\u79cd\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a<code>List<\/code>\u89c6\u56fe\u7684\u65b9\u5f0f\uff0c\u65b9\u4fbf\u5728\u9700\u8981<code>List<\/code>\u63a5\u53e3\u7684\u5730\u65b9\u4f7f\u7528\u6570\u7ec4\u6570\u636e\uff0c\u800c\u4e0d\u662f\u521b\u5efa\u4e00\u4e2a\u529f\u80fd\u5b8c\u5907\u3001\u53ef\u968f\u610f\u4fee\u6539\u7684<code>List<\/code>\u96c6\u5408\u3002\u5b83\u66f4\u50cf\u662f\u4e00\u4e2a \u201c\u5305\u88c5\u201d\uff0c\u8ba9\u6570\u7ec4\u80fd\u591f\u9002\u914d<code>List<\/code>\u76f8\u5173\u7684\u64cd\u4f5c\uff0c\u5982\u4f7f\u7528<code>for - each<\/code>\u5faa\u73af\u904d\u5386\u3001\u8c03\u7528<code>List<\/code>\u7684\u4e00\u4e9b\u67e5\u8be2\u65b9\u6cd5\u7b49\uff0c\u800c\u4e0d\u662f\u4e3a\u4e86\u8fdb\u884c\u590d\u6742\u7684\u96c6\u5408\u7ed3\u6784\u4fee\u6539\u3002<\/li>\n\n\n\n<li><strong>\u4e0e\u5176\u4ed6\u4e0d\u53ef\u53d8\u96c6\u5408\u7684\u4e00\u81f4\u6027<\/strong>\uff1aJava \u4e2d\u5b58\u5728\u4e00\u4e9b\u4e0d\u53ef\u53d8\u96c6\u5408\u7c7b\uff0c\u5982<code>Collections.unmodifiableList()<\/code>\u8fd4\u56de\u7684\u4e5f\u662f\u4e0d\u53ef\u53d8\u7684<code>List<\/code>\u3002<code>Arrays.asList()<\/code>\u8fd4\u56de\u4e0d\u53ef\u53d8<code>List<\/code>\u7b26\u5408 Java \u96c6\u5408\u6846\u67b6\u4e2d\u5bf9\u4e8e\u4e0d\u53ef\u53d8\u96c6\u5408\u7684\u8bbe\u8ba1\u7406\u5ff5\uff0c\u4f7f\u5f00\u53d1\u8005\u5728\u5904\u7406\u4e0d\u53ef\u53d8\u6570\u636e\u7ed3\u6784\u65f6\u6709\u7edf\u4e00\u7684\u7f16\u7a0b\u6a21\u578b\u548c\u4f7f\u7528\u4f53\u9a8c\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u6027\u80fd\u4e0e\u5b9e\u73b0\u6210\u672c<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>\u6027\u80fd\u8003\u8651<\/strong>\uff1a\u5b9e\u73b0\u53ef\u53d8\u96c6\u5408\u9700\u8981\u989d\u5916\u7684\u673a\u5236\u6765\u5904\u7406\u5143\u7d20\u7684\u6dfb\u52a0\u3001\u5220\u9664\u7b49\u64cd\u4f5c\uff0c\u6bd4\u5982\u52a8\u6001\u8c03\u6574\u6570\u7ec4\u5927\u5c0f\u3001\u7ef4\u62a4\u5143\u7d20\u987a\u5e8f\u7b49\u3002\u800c\u8fd4\u56de\u4e0d\u53ef\u53d8\u7684<code>List<\/code>\u53ef\u4ee5\u907f\u514d\u8fd9\u4e9b\u590d\u6742\u7684\u64cd\u4f5c\u548c\u989d\u5916\u7684\u5f00\u9500\uff0c\u5728\u6027\u80fd\u4e0a\u66f4\u4f18\u3002\u56e0\u4e3a\u5b83\u4e0d\u9700\u8981\u5904\u7406\u5143\u7d20\u63d2\u5165\u3001\u5220\u9664\u65f6\u53ef\u80fd\u6d89\u53ca\u7684\u6570\u7ec4\u6269\u5bb9\u3001\u6570\u636e\u8fc1\u79fb\u7b49\u64cd\u4f5c\uff0c\u5bf9\u4e8e\u4ec5\u9700\u8bfb\u53d6\u6570\u636e\u7684\u573a\u666f\uff0c\u80fd\u63d0\u9ad8\u8fd0\u884c\u6548\u7387\u3002<\/li>\n\n\n\n<li><strong>\u7b80\u5316\u5b9e\u73b0<\/strong>\uff1a\u4ece\u5b9e\u73b0\u89d2\u5ea6\u770b\uff0c\u8fd4\u56de\u4e0d\u53ef\u53d8\u7684<code>List<\/code>\u5b9e\u73b0\u8d77\u6765\u76f8\u5bf9\u7b80\u5355\u3002<code>Arrays.asList()<\/code>\u8fd4\u56de\u7684<code>List<\/code>\u5b9e\u9645\u4e0a\u662f<code>Arrays<\/code>\u7c7b\u7684\u4e00\u4e2a\u5185\u90e8\u7c7b\uff0c\u8fd9\u4e2a\u5185\u90e8\u7c7b\u76f4\u63a5\u5f15\u7528\u539f\u59cb\u6570\u7ec4\uff0c\u6ca1\u6709\u63d0\u4f9b<code>add<\/code>\u3001<code>remove<\/code>\u7b49\u4fee\u6539\u7ed3\u6784\u7684\u65b9\u6cd5\uff08\u8c03\u7528\u8fd9\u4e9b\u65b9\u6cd5\u4f1a\u629b\u51fa<code>UnsupportedOperationException<\/code>\u5f02\u5e38\uff09\uff0c\u8fd9\u6837\u7684\u8bbe\u8ba1\u7b80\u5316\u4e86\u4ee3\u7801\u5b9e\u73b0\uff0c\u51cf\u5c11\u4e86\u51fa\u9519\u7684\u53ef\u80fd\u6027\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1aArrays.asList() \u6570\u7ec4\u8f6c\u6362\u6210\u96c6\u5408\u917f\u6210\u7684\u7ebf\u4e0a\u4e8b\u6545\uff0c\u5dee\u70b9\u8981\u6eda\u86cb\u4e86\uff01 \u5f15\u8a00 \u5728Java\u5f00\u53d1\u4e2d\uff0c\u6570 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17141"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17141"}],"version-history":[{"count":3,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17141\/revisions"}],"predecessor-version":[{"id":17151,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17141\/revisions\/17151"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}