{"id":114,"date":"2026-05-19T14:29:01","date_gmt":"2026-05-19T06:29:01","guid":{"rendered":"https:\/\/tachyoncat.top\/?p=114"},"modified":"2026-05-19T16:05:09","modified_gmt":"2026-05-19T08:05:09","slug":"dart%e5%9f%ba%e6%9c%ac%e8%af%ad%e6%b3%952","status":"publish","type":"post","link":"https:\/\/tachyoncat.top\/index.php\/2026\/05\/19\/dart%e5%9f%ba%e6%9c%ac%e8%af%ad%e6%b3%952\/","title":{"rendered":"Dart\u57fa\u672c\u8bed\u6cd52 <\u5217\u8868\u4e0e\u96c6\u5408>"},"content":{"rendered":"<h2>1. List\uff08\u5217\u8868\uff09<\/h2>\n<p>\u5217\u8868\u662f<strong>\u6709\u5e8f\u4e14\u53ef\u91cd\u590d<\/strong>\u7684\u96c6\u5408\uff0c\u7c7b\u4f3c C++ \u7684 <code>vector<\/code>\u3002<\/p>\n<h3>\u5e38\u7528\u65b9\u6cd5<\/h3>\n<h4>\u6dfb\u52a0\u5143\u7d20<\/h4>\n<pre><code class=\"language-dart line-numbers\">var fruits = ['\u82f9\u679c', '\u9999\u8549'];\n\n\/\/ \u5728\u672b\u5c3e\u6dfb\u52a0\u5355\u4e2a\u5143\u7d20\nfruits.add('\u6a59\u5b50');           \/\/ ['\u82f9\u679c', '\u9999\u8549', '\u6a59\u5b50']\n\n\/\/ \u5728\u672b\u5c3e\u6dfb\u52a0\u591a\u4e2a\u5143\u7d20\nfruits.addAll(['\u8461\u8404', '\u897f\u74dc']);  \/\/ ['\u82f9\u679c', '\u9999\u8549', '\u6a59\u5b50', '\u8461\u8404', '\u897f\u74dc']\n\n\/\/ \u5728\u6307\u5b9a\u4f4d\u7f6e\u63d2\u5165\nfruits.insert(1, '\u8349\u8393');     \/\/ ['\u82f9\u679c', '\u8349\u8393', '\u9999\u8549', '\u6a59\u5b50', '\u8461\u8404', '\u897f\u74dc']\n\n\/\/ \u5728\u6307\u5b9a\u4f4d\u7f6e\u63d2\u5165\u591a\u4e2a\nfruits.insertAll(0, ['\u8292\u679c', '\u7315\u7334\u6843']);  \/\/ ['\u8292\u679c', '\u7315\u7334\u6843', '\u82f9\u679c', '\u8349\u8393', '\u9999\u8549', '\u6a59\u5b50', '\u8461\u8404', '\u897f\u74dc']\n<\/code><\/pre>\n<h4>\u5220\u9664\u5143\u7d20<\/h4>\n<pre><code class=\"language-dart line-numbers\">var fruits = ['\u82f9\u679c', '\u9999\u8549', '\u6a59\u5b50', '\u9999\u8549'];\n\n\/\/ \u5220\u9664\u6307\u5b9a\u5143\u7d20\uff08\u53ea\u5220\u9664\u7b2c\u4e00\u4e2a\uff09\nfruits.remove('\u9999\u8549');        \/\/ ['\u82f9\u679c', '\u6a59\u5b50', '\u9999\u8549']\n\n\/\/ \u5220\u9664\u6307\u5b9a\u4f4d\u7f6e\u7684\u5143\u7d20\nfruits.removeAt(1);          \/\/ ['\u82f9\u679c', '\u9999\u8549'] \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n\n\/\/ \u5220\u9664\u6700\u540e\u4e00\u4e2a\u5143\u7d20\nfruits.removeLast();          \/\/ ['\u82f9\u679c', '\u9999\u8549'] \u8fd4\u56de\u88ab\u5220\u9664\u7684\u5143\u7d20\n\n\/\/ \u5220\u9664\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\nfruits.removeWhere((item) =&gt; item.length &gt; 2);  \/\/ \u5220\u9664\u957f\u5ea6\u5927\u4e8e2\u7684\n\n\/\/ \u6e05\u7a7a\u5217\u8868\nfruits.clear();              \/\/ []\n<\/code><\/pre>\n<h4>\u67e5\u627e\u5143\u7d20<\/h4>\n<pre><code class=\"language-dart line-numbers\">var fruits = ['\u82f9\u679c', '\u9999\u8549', '\u6a59\u5b50', '\u9999\u8549'];\n\n\/\/ \u67e5\u627e\u5143\u7d20\u4f4d\u7f6e\uff08\u4ece\u524d\u5f80\u540e\uff09\nfruits.indexOf('\u9999\u8549');       \/\/ 1\n\n\/\/ \u67e5\u627e\u5143\u7d20\u4f4d\u7f6e\uff08\u4ece\u540e\u5f80\u524d\uff09\nfruits.lastIndexOf('\u9999\u8549');   \/\/ 3\n\n\/\/ \u68c0\u67e5\u662f\u5426\u5305\u542b\u5143\u7d20\nfruits.contains('\u82f9\u679c');     \/\/ true\n<\/code><\/pre>\n<h4>\u4fee\u6539\u5143\u7d20<\/h4>\n<pre><code class=\"language-dart line-numbers\">var fruits = ['\u82f9\u679c', '\u9999\u8549', '\u6a59\u5b50'];\n\n\/\/ \u4fee\u6539\u6307\u5b9a\u4f4d\u7f6e\u5143\u7d20\nfruits[1] = '\u8349\u8393';          \/\/ ['\u82f9\u679c', '\u8349\u8393', '\u6a59\u5b50']\n\n\/\/ \u66ff\u6362\u4e00\u4e2a\u5143\u7d20\nfruits.replaceRange(0, 1, ['\u8292\u679c']);  \/\/ ['\u8292\u679c', '\u8349\u8393', '\u6a59\u5b50']\n\n\/\/ \u968f\u673a\u6253\u4e71\u987a\u5e8f\nfruits.shuffle();            \/\/ \u968f\u673a\u6392\u5e8f\n<\/code><\/pre>\n<h4>\u904d\u5386\u4e0e\u8f6c\u6362<\/h4>\n<pre><code class=\"language-dart line-numbers\">var numbers = [1, 2, 3, 4, 5];\n\n\/\/ for \u5faa\u73af\nfor (var i = 0; i &lt; numbers.length; i++) {\n  print(numbers[i]);\n}\n\n\/\/ for-in \u5faa\u73af\nfor (var num in numbers) {\n  print(num);\n}\n\n\/\/ forEach \u65b9\u6cd5\nnumbers.forEach((num) =&gt; print(num));\n\n\/\/ map \u8f6c\u6362\nvar doubled = numbers.map((n) =&gt; n * 2).toList();  \/\/ [2, 4, 6, 8, 10]\n\n\/\/ where \u8fc7\u6ee4\nvar evens = numbers.where((n) =&gt; n % 2 == 0).toList();  \/\/ [2, 4]\n\n\/\/ reduce \u805a\u5408\nvar sum = numbers.reduce((a, b) =&gt; a + b);  \/\/ 15\n\n\/\/ any \u548c every \u5224\u65ad\nnumbers.any((n) =&gt; n &gt; 3);   \/\/ true\uff08\u662f\u5426\u6709\u5927\u4e8e3\u7684\u5143\u7d20\uff09\nnumbers.every((n) =&gt; n &gt; 0); \/\/ true\uff08\u662f\u5426\u6240\u6709\u5143\u7d20\u90fd\u5927\u4e8e0\uff09\n<\/code><\/pre>\n<h4>\u6392\u5e8f<\/h4>\n<pre><code class=\"language-dart line-numbers\">var numbers = [3, 1, 4, 1, 5, 9, 2];\n\n\/\/ \u5347\u5e8f\u6392\u5e8f\nnumbers.sort();             \/\/ [1, 1, 2, 3, 4, 5, 9]\n\n\/\/ \u81ea\u5b9a\u4e49\u6392\u5e8f\uff08\u964d\u5e8f\uff09\nnumbers.sort((a, b) =&gt; b.compareTo(a));  \/\/ [9, 5, 4, 3, 2, 1, 1]\n<\/code><\/pre>\n<hr \/>\n<h2>2. Set\uff08\u96c6\u5408\uff09<\/h2>\n<p>\u96c6\u5408\u662f<strong>\u65e0\u5e8f\u4e14\u4e0d\u91cd\u590d<\/strong>\u7684\u96c6\u5408\uff0c\u7c7b\u4f3c C++ \u7684 <code>unordered_set<\/code>\u3002<\/p>\n<h3>\u521b\u5efa\u65b9\u5f0f<\/h3>\n<pre><code class=\"language-dart line-numbers\">\/\/ \u4f7f\u7528\u7c7b\u578b\nvar colors = &lt;String&gt;{'\u7ea2\u8272', '\u7eff\u8272', '\u84dd\u8272'};\n\n\/\/ \u7c7b\u578b\u63a8\u65ad\nvar numbers = {1, 2, 3, 4, 5};\n\n\/\/ \u7a7a\u96c6\u5408\uff08\u6ce8\u610f\uff1a{} \u4f1a\u9ed8\u8ba4\u521b\u5efa Map\uff0c\u800c\u4e0d\u662f Set\uff09\nvar emptySet = &lt;int&gt;{};\n<\/code><\/pre>\n<h3>\u5e38\u7528\u65b9\u6cd5<\/h3>\n<h4>\u6dfb\u52a0\u4e0e\u5220\u9664<\/h4>\n<pre><code class=\"language-dart line-numbers\">var fruits = {'\u82f9\u679c', '\u9999\u8549'};\n\n\/\/ \u6dfb\u52a0\u5355\u4e2a\u5143\u7d20\nfruits.add('\u6a59\u5b50');          \/\/ {'\u82f9\u679c', '\u9999\u8549', '\u6a59\u5b50'}\n\/\/ \u5982\u679c\u5df2\u5b58\u5728\uff0c\u4e0d\u4f1a\u6dfb\u52a0\uff08\u65e0\u91cd\u590d\uff09\n\n\/\/ \u6dfb\u52a0\u591a\u4e2a\u5143\u7d20\nfruits.addAll(['\u8461\u8404', '\u897f\u74dc']);  \/\/ {'\u82f9\u679c', '\u9999\u8549', '\u6a59\u5b50', '\u8461\u8404', '\u897f\u74dc'}\n\n\/\/ \u5220\u9664\u5143\u7d20\nfruits.remove('\u9999\u8549');        \/\/ {'\u82f9\u679c', '\u6a59\u5b50', '\u8461\u8404', '\u897f\u74dc'}\n\n\/\/ \u5220\u9664\u7b26\u5408\u6761\u4ef6\u7684\nfruits.removeWhere((item) =&gt; item.length &gt; 2);\n\n\/\/ \u6e05\u7a7a\u96c6\u5408\nfruits.clear();\n<\/code><\/pre>\n<h4>\u96c6\u5408\u8fd0\u7b97<\/h4>\n<pre><code class=\"language-dart line-numbers\">var setA = {1, 2, 3, 4};\nvar setB = {3, 4, 5, 6};\n\n\/\/ \u4ea4\u96c6\nvar intersection = setA.intersection(setB);     \/\/ {3, 4}\n\n\/\/ \u5e76\u96c6\nvar union = setA.union(setB);                    \/\/ {1, 2, 3, 4, 5, 6}\n\n\/\/ \u5dee\u96c6\uff08A\u6709B\u6ca1\u6709\u7684\uff09\nvar difference = setA.difference(setB);          \/\/ {1, 2}\n\n\/\/ \u68c0\u67e5\u662f\u5426\u5305\u542b\nsetA.contains(1);                               \/\/ true\nsetA.containsAll([1, 2]);                       \/\/ true\uff08\u662f\u5426\u5305\u542b\u6240\u6709\uff09\n<\/code><\/pre>\n<h4>\u904d\u5386<\/h4>\n<pre><code class=\"language-dart line-numbers\">var fruits = {'\u82f9\u679c', '\u9999\u8549', '\u6a59\u5b50'};\n\nfor (var fruit in fruits) {\n  print(fruit);\n}\n\nfruits.forEach((fruit) =&gt; print(fruit));\n<\/code><\/pre>\n<hr \/>\n<h2>3. List vs Set \u6838\u5fc3\u533a\u522b<\/h2>\n<table>\n<thead>\n<tr>\n<th>\u7279\u6027<\/th>\n<th>List\uff08\u5217\u8868\uff09<\/th>\n<th>Set\uff08\u96c6\u5408\uff09<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>\u6709\u5e8f\u6027<\/strong><\/td>\n<td>\u2705 \u6709\u5e8f\uff08\u6309\u7d22\u5f15\uff09<\/td>\n<td>\u274c \u65e0\u5e8f<\/td>\n<\/tr>\n<tr>\n<td><strong>\u91cd\u590d\u5143\u7d20<\/strong><\/td>\n<td>\u2705 \u5141\u8bb8<\/td>\n<td>\u274c \u4e0d\u5141\u8bb8<\/td>\n<\/tr>\n<tr>\n<td><strong>\u7d22\u5f15\u8bbf\u95ee<\/strong><\/td>\n<td>\u2705 \u652f\u6301 <code>[index]<\/code><\/td>\n<td>\u274c \u4e0d\u652f\u6301<\/td>\n<\/tr>\n<tr>\n<td><strong>\u9002\u7528\u573a\u666f<\/strong><\/td>\n<td>\u9700\u8981\u6309\u987a\u5e8f\u8bbf\u95ee\u6570\u636e<\/td>\n<td>\u9700\u8981\u53bb\u91cd\u6216\u68c0\u67e5\u5b58\u5728\u6027<\/td>\n<\/tr>\n<tr>\n<td><strong>\u6027\u80fd<\/strong><\/td>\n<td>\u67e5\u627e\u8f83\u6162 O(n)<\/td>\n<td>\u67e5\u627e\u5feb O(1)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u793a\u4f8b\u5bf9\u6bd4<\/h3>\n<pre><code class=\"language-dart line-numbers\">\/\/ List - \u6709\u5e8f\u53ef\u91cd\u590d\nvar list = ['A', 'B', 'A', 'C'];\nprint(list[0]);  \/\/ 'A' - \u53ef\u4ee5\u7528\u7d22\u5f15\u8bbf\u95ee\nprint(list);     \/\/ [A, B, A, C]\n\n\/\/ Set - \u65e0\u5e8f\u4e0d\u91cd\u590d\nvar set = {'A', 'B', 'A', 'C'};\n\/\/ print(set[0]);  \/\/ \u9519\u8bef\uff01\u4e0d\u80fd\u7d22\u5f15\u8bbf\u95ee\nprint(set);      \/\/ {A, B, C} - \u81ea\u52a8\u53bb\u91cd\n<\/code><\/pre>\n<hr \/>\n<h2>4. \u5feb\u901f\u8bb0\u5fc6<\/h2>\n<blockquote><p>\n  \ud83c\udfaf <strong>\u5217\u8868<\/strong>\uff1a\u6709\u5e8f\u6709\u91cd\u590d\uff0c\u9700\u8981\u7d22\u5f15\u8bbf\u95ee \u2192 \u7528 <code>List<\/code><\/p>\n<p>  \ud83c\udfaf <strong>\u96c6\u5408<\/strong>\uff1a\u65e0\u9700\u53bb\u91cd\uff0c\u5feb\u901f\u67e5\u627e \u2192 \u7528 <code>Set<\/code><\/p>\n<p>  \ud83c\udfaf <strong>\u5e38\u7528\u64cd\u4f5c<\/strong>\uff1a<br \/>\n  - \u6dfb\u52a0\uff1a<code>add()<\/code> \/ <code>addAll()<\/code><br \/>\n  - \u5220\u9664\uff1a<code>remove()<\/code> \/ <code>removeLast()<\/code> \/ <code>clear()<\/code><br \/>\n  - \u67e5\u627e\uff1a<code>indexOf()<\/code> \/ <code>contains()<\/code><br \/>\n  - \u904d\u5386\uff1a<code>for-in<\/code> \/ <code>forEach()<\/code> \/ <code>map()<\/code>\n<\/p><\/blockquote>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>1. List\uff08\u5217\u8868\uff09 \u5217\u8868\u662f\u6709\u5e8f\u4e14\u53ef\u91cd\u590d\u7684\u96c6\u5408\uff0c\u7c7b\u4f3c C++ \u7684 vector\u3002 \u5e38\u7528\u65b9\u6cd5 \u6dfb\u52a0\u5143\u7d20 var fruits =  &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-114","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/tachyoncat.top\/index.php\/wp-json\/wp\/v2\/posts\/114","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tachyoncat.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tachyoncat.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tachyoncat.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tachyoncat.top\/index.php\/wp-json\/wp\/v2\/comments?post=114"}],"version-history":[{"count":2,"href":"https:\/\/tachyoncat.top\/index.php\/wp-json\/wp\/v2\/posts\/114\/revisions"}],"predecessor-version":[{"id":116,"href":"https:\/\/tachyoncat.top\/index.php\/wp-json\/wp\/v2\/posts\/114\/revisions\/116"}],"wp:attachment":[{"href":"https:\/\/tachyoncat.top\/index.php\/wp-json\/wp\/v2\/media?parent=114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tachyoncat.top\/index.php\/wp-json\/wp\/v2\/categories?post=114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tachyoncat.top\/index.php\/wp-json\/wp\/v2\/tags?post=114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}