1# @ohos.util.List (线性容器List) 2 3List底层通过单向链表实现,每个节点有一个指向后一个元素的引用。当需要查询元素时,必须从头遍历,插入、删除效率高,查询效率低。List允许元素为null。 4 5List和[LinkedList](js-apis-linkedlist.md)相比,LinkedList是双向链表,可以快速地在头尾进行增删,而List是单向链表,无法双向操作。 6 7> **注意:** 8> 9> 在List中使用\[index\]的方式虽然能够获取对应位置的元素,但这会导致未定义结果。推荐使用get()方法。 10 11**推荐使用场景:** 当需要频繁的插入删除元素,并且需要使用单向链表时,推荐使用List高效操作。 12 13文档中存在泛型的使用,涉及以下泛型标记符:<br> 14- T:Type,类 15 16> **说明:** 17> 18> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 19 20 21## 导入模块 22 23```ts 24import { List } from '@kit.ArkTS'; 25``` 26 27 28## List 29 30### 属性 31 32**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 33 34**系统能力:** SystemCapability.Utils.Lang 35 36| 名称 | 类型 | 可读 | 可写 | 说明 | 37| -------- | -------- | -------- | -------- | -------- | 38| length | number | 是 | 否 | List的元素个数。 | 39 40 41### constructor 42 43constructor() 44 45List的构造函数。 46 47**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 48 49**系统能力:** SystemCapability.Utils.Lang 50 51**错误码:** 52 53以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 54 55| 错误码ID | 错误信息 | 56| -------- | -------- | 57| 10200012 | The List's constructor cannot be directly invoked. | 58 59**示例:** 60 61```ts 62let list: List<string | number | boolean | object> = new List(); 63``` 64 65 66### add 67 68add(element: T): boolean 69 70在List尾部插入元素。 71 72**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 73 74**系统能力:** SystemCapability.Utils.Lang 75 76**参数:** 77 78| 参数名 | 类型 | 必填 | 说明 | 79| -------- | -------- | -------- | -------- | 80| element | T | 是 | 添加进去的元素。 | 81 82**返回值:** 83 84| 类型 | 说明 | 85| -------- | -------- | 86| boolean | 插入成功返回true,否则返回false。 | 87 88**错误码:** 89 90以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 91 92| 错误码ID | 错误信息 | 93| -------- | -------- | 94| 10200011 | The add method cannot be bound. | 95 96**示例:** 97 98```ts 99let list: List<string | number | boolean | object> = new List(); 100let result1 = list.add("a"); 101let result2 = list.add(1); 102let b = [1, 2, 3]; 103let result3 = list.add(b); 104class C { 105 name: string = '' 106 age: string = '' 107} 108let c: C = {name : "Dylan", age : "13"}; 109let result4 = list.add(c); 110let result5 = list.add(false); 111``` 112 113### insert 114 115insert(element: T, index: number): void 116 117在长度范围内任意位置插入指定元素。 118 119**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 120 121**系统能力:** SystemCapability.Utils.Lang 122 123**参数:** 124 125| 参数名 | 类型 | 必填 | 说明 | 126| -------- | -------- | -------- | -------- | 127| element | T | 是 | 插入元素。 | 128| index | number | 是 | 插入的位置索引。需要小于等于int32_max即2147483647。 | 129 130**错误码:** 131 132以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 133 134| 错误码ID | 错误信息 | 135| -------- | -------- | 136| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 137| 10200001 | The value of index is out of range. | 138| 10200011 | The insert method cannot be bound. | 139 140**示例:** 141 142```ts 143let list: List<string | number | boolean> = new List(); 144list.insert("A", 0); 145list.insert(0, 1); 146list.insert(true, 2); 147``` 148 149### has 150 151has(element: T): boolean 152 153判断此List中是否含有该指定元素。 154 155**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 156 157**系统能力:** SystemCapability.Utils.Lang 158 159**参数:** 160 161| 参数名 | 类型 | 必填 | 说明 | 162| -------- | -------- | -------- | -------- | 163| element | T | 是 | 指定元素。 | 164 165**返回值:** 166 167| 类型 | 说明 | 168| -------- | -------- | 169| boolean | 包含指定元素返回true,否则返回false。 | 170 171**错误码:** 172 173以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 174 175| 错误码ID | 错误信息 | 176| -------- | -------- | 177| 10200011 | The has method cannot be bound. | 178 179**示例:** 180 181```ts 182let list: List<string> = new List(); 183list.add("squirrel"); 184let result = list.has("squirrel"); 185``` 186 187### get 188 189get(index: number): T 190 191根据下标获取List中的元素。 192 193**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 194 195**系统能力:** SystemCapability.Utils.Lang 196 197**参数:** 198 199| 参数名 | 类型 | 必填 | 说明 | 200| -------- | -------- | -------- | -------- | 201| index | number | 是 | 要查找的下标。需要小于等于int32_max即2147483647。 | 202 203**返回值:** 204 205| 类型 | 说明 | 206| -------- | -------- | 207| T | 根据下标查找到的元素。 | 208 209**错误码:** 210 211以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 212 213| 错误码ID | 错误信息 | 214| -------- | -------- | 215| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 216| 10200011 | The get method cannot be bound. | 217 218**示例:** 219 220```ts 221let list: List<number> = new List(); 222list.add(2); 223list.add(4); 224list.add(5); 225list.add(2); 226list.add(1); 227list.add(2); 228list.add(4); 229let result = list.get(2); 230``` 231 232### getLastIndexOf 233 234getLastIndexOf(element: T): number 235 236查找指定元素最后一次出现的下标值,查找失败返回-1。 237 238**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 239 240**系统能力:** SystemCapability.Utils.Lang 241 242**参数:** 243 244| 参数名 | 类型 | 必填 | 说明 | 245| -------- | -------- | -------- | -------- | 246| element | T | 是 | 指定元素。 | 247 248**返回值:** 249 250| 类型 | 说明 | 251| -------- | -------- | 252| number | 返回指定元素最后一次出现的下标值,没有找到返回-1。 | 253 254**错误码:** 255 256以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 257 258| 错误码ID | 错误信息 | 259| -------- | -------- | 260| 10200011 | The getLastIndexOf method cannot be bound. | 261 262**示例:** 263 264```ts 265let list: List<number> = new List(); 266list.add(2); 267list.add(4); 268list.add(5); 269list.add(2); 270list.add(1); 271list.add(2); 272list.add(4); 273let result = list.getLastIndexOf(2); 274``` 275 276### getIndexOf 277 278getIndexOf(element: T): number 279 280查找指定元素第一次出现的下标值,查找失败返回-1。 281 282**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 283 284**系统能力:** SystemCapability.Utils.Lang 285 286**参数:** 287 288| 参数名 | 类型 | 必填 | 说明 | 289| -------- | -------- | -------- | -------- | 290| element | T | 是 | 指定元素。 | 291 292**返回值:** 293 294| 类型 | 说明 | 295| -------- | -------- | 296| number | 返回第一次找到指定元素的下标,没有找到返回-1。 | 297 298**错误码:** 299 300以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 301 302| 错误码ID | 错误信息 | 303| -------- | -------- | 304| 10200011 | The getIndexOf method cannot be bound. | 305 306**示例:** 307 308```ts 309let list: List<number> = new List(); 310list.add(2); 311list.add(4); 312list.add(5); 313list.add(2); 314list.add(1); 315list.add(2); 316list.add(4); 317let result = list.getIndexOf(2); 318``` 319 320### equal 321 322equal(obj: Object): boolean 323 324比较指定对象与此List是否相等。 325 326**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 327 328**系统能力:** SystemCapability.Utils.Lang 329 330**参数:** 331 332| 参数名 | 类型 | 必填 | 说明 | 333| -------- | -------- | -------- | -------- | 334| obj | Object | 是 | 用来比较的对象。 | 335 336**返回值:** 337 338| 类型 | 说明 | 339| -------- | -------- | 340| boolean | 如果对象与此列表相同返回true,否则返回false。 | 341 342**错误码:** 343 344以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 345 346| 错误码ID | 错误信息 | 347| -------- | -------- | 348| 10200011 | The equal method cannot be bound. | 349 350**示例:** 351 352```ts 353let list: List<number> = new List(); 354list.add(2); 355list.add(4); 356list.add(5); 357let obj: List<number> = new List(); 358obj.add(2); 359obj.add(4); 360obj.add(5); 361let result = list.equal(obj); 362``` 363 364### removeByIndex 365 366removeByIndex(index: number): T 367 368根据元素的下标值查找元素,并将其删除。 369 370**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 371 372**系统能力:** SystemCapability.Utils.Lang 373 374**参数:** 375 376| 参数名 | 类型 | 必填 | 说明 | 377| -------- | -------- | -------- | -------- | 378| index | number | 是 | 指定元素的下标值。需要小于等于int32_max即2147483647。 | 379 380**返回值:** 381 382| 类型 | 说明 | 383| -------- | -------- | 384| T | 返回被删除的元素。 | 385 386**错误码:** 387 388以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 389 390| 错误码ID | 错误信息 | 391| -------- | -------- | 392| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 393| 10200001 | The value of index is out of range. | 394| 10200011 | The removeByIndex method cannot be bound. | 395 396**示例:** 397 398```ts 399let list: List<number> = new List(); 400list.add(2); 401list.add(4); 402list.add(5); 403list.add(2); 404list.add(4); 405let result = list.removeByIndex(2); 406``` 407 408### remove 409 410remove(element: T): boolean 411 412删除查找到的第一个指定的元素。 413 414**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 415 416**系统能力:** SystemCapability.Utils.Lang 417 418**参数:** 419 420| 参数名 | 类型 | 必填 | 说明 | 421| -------- | -------- | -------- | -------- | 422| element | T | 是 | 指定元素。 | 423 424**返回值:** 425 426| 类型 | 说明 | 427| -------- | -------- | 428| boolean | 删除成功返回true,否则返回false。 | 429 430**错误码:** 431 432以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 433 434| 错误码ID | 错误信息 | 435| -------- | -------- | 436| 10200011 | The remove method cannot be bound. | 437 438**示例:** 439 440```ts 441let list: List<number> = new List(); 442list.add(2); 443list.add(4); 444list.add(5); 445list.add(4); 446let result = list.remove(2); 447``` 448 449### replaceAllElements 450 451replaceAllElements(callbackFn: (value: T, index?: number, list?: List<T>) => T, 452thisArg?: Object): void 453 454用户操作List中的元素,用操作后的元素替换原元素并返回操作后的元素。 455 456**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 457 458**系统能力:** SystemCapability.Utils.Lang 459 460**参数:** 461 462| 参数名 | 类型 | 必填 | 说明 | 463| -------- | -------- | -------- | -------- | 464| callbackFn | function | 是 | 回调函数。 | 465| thisArg | Object | 否 | callbackfn被调用时用作this值,默认值为当前实例对象。 | 466 467callbackfn的参数说明: 468 469| 参数名 | 类型 | 必填 | 说明 | 470| -------- | -------- | -------- | -------- | 471| value | T | 是 | 当前遍历到的元素。 | 472| index | number | 否 | 当前遍历到的下标值,默认值为0。 | 473| list | List<T> | 否 | 当前调用replaceAllElements方法的实例对象,默认值为当前实例对象。 | 474 475**错误码:** 476 477以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 478 479| 错误码ID | 错误信息 | 480| -------- | -------- | 481| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 482| 10200011 | The replaceAllElements method cannot be bound. | 483 484**示例:** 485 486```ts 487let list: List<number> = new List(); 488list.add(2); 489list.add(4); 490list.add(5); 491list.add(4); 492list.replaceAllElements((value: number) => { 493 // 用户操作逻辑根据实际场景进行添加。 494 return value; 495}); 496``` 497 498### forEach 499 500forEach(callbackFn: (value: T, index?: number, List?: List<T>) => void, 501thisArg?: Object): void 502 503通过回调函数来遍历List实例对象上的元素以及元素对应的下标。 504 505**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 506 507**系统能力:** SystemCapability.Utils.Lang 508 509**参数:** 510 511| 参数名 | 类型 | 必填 | 说明 | 512| -------- | -------- | -------- | -------- | 513| callbackFn | function | 是 | 回调函数。 | 514| thisArg | Object | 否 | callbackfn被调用时用作this值,默认值为当前实例对象。 | 515 516callbackfn的参数说明: 517 518| 参数名 | 类型 | 必填 | 说明 | 519| -------- | -------- | -------- | -------- | 520| value | T | 是 | 当前遍历到的元素。 | 521| index | number | 否 | 当前遍历到的下标值,默认值为0。 | 522| List | List<T> | 否 | 当前调用forEach方法的实例对象,默认值为当前实例对象。 | 523 524**错误码:** 525 526以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 527 528| 错误码ID | 错误信息 | 529| -------- | -------- | 530| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 531| 10200011 | The forEach method cannot be bound. | 532 533**示例:** 534 535```ts 536let list: List<number> = new List(); 537list.add(2); 538list.add(4); 539list.add(5); 540list.add(4); 541list.forEach((value: number, index?: number) => { 542 console.log("value:" + value, "index:" + index); 543}); 544``` 545 546### sort 547 548sort(comparator: (firstValue: T, secondValue: T) => number): void 549 550对List中的元素进行一个排序操作。 551 552**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 553 554**系统能力:** SystemCapability.Utils.Lang 555 556**参数:** 557 558| 参数名 | 类型 | 必填 | 说明 | 559| -------- | -------- | -------- | -------- | 560| comparator | function | 是 | 回调函数。 | 561 562comparator的参数说明: 563 564| 参数名 | 类型 | 必填 | 说明 | 565| -------- | -------- | -------- | -------- | 566| firstValue | T | 是 | 前一项元素。 | 567| secondValue | T | 是 | 后一项元素。 | 568 569**错误码:** 570 571以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 572 573| 错误码ID | 错误信息 | 574| -------- | -------- | 575| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 576| 10200011 | The sort method cannot be bound. | 577 578**示例:** 579 580```ts 581let list: List<number> = new List(); 582list.add(2); 583list.add(4); 584list.add(5); 585list.add(4); 586list.sort((a: number, b: number) => a - b); // 结果为升序排列 587list.sort((a: number, b: number) => b - a); // 结果为降序排列 588``` 589 590### getSubList 591 592getSubList(fromIndex: number, toIndex: number): List<T> 593 594根据下标截取List中的一段元素,并返回这一段List实例,包括起始值但不包括终止值。 595 596**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 597 598**系统能力:** SystemCapability.Utils.Lang 599 600**参数:** 601 602| 参数名 | 类型 | 必填 | 说明 | 603| -------- | -------- | -------- | -------- | 604| fromIndex | number | 是 | 起始下标。 | 605| toIndex | number | 是 | 终止下标。 | 606 607**返回值:** 608 609| 类型 | 说明 | 610| -------- | -------- | 611| List<T> | 返回List对象实例。 | 612 613**错误码:** 614 615以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 616 617| 错误码ID | 错误信息 | 618| -------- | -------- | 619| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 620| 10200001 | The value of fromIndex or toIndex is out of range. | 621| 10200011 | The getSubList method cannot be bound. | 622 623**示例:** 624 625```ts 626let list: List<number> = new List(); 627list.add(2); 628list.add(4); 629list.add(5); 630list.add(4); 631let result = list.getSubList(1, 3); 632``` 633 634### clear 635 636clear(): void 637 638清除List中的所有元素,并把length置为0。 639 640**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 641 642**系统能力:** SystemCapability.Utils.Lang 643 644**错误码:** 645 646以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 647 648| 错误码ID | 错误信息 | 649| -------- | -------- | 650| 10200011 | The clear method cannot be bound. | 651 652**示例:** 653 654```ts 655let list: List<number> = new List(); 656list.add(2); 657list.add(4); 658list.add(5); 659list.add(4); 660list.clear(); 661``` 662 663### set 664 665set(index: number, element: T): T 666 667将此 List 中指定位置的元素替换为指定元素。 668 669**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 670 671**系统能力:** SystemCapability.Utils.Lang 672 673**参数:** 674 675| 参数名 | 类型 | 必填 | 说明 | 676| -------- | -------- | -------- | -------- | 677| index | number | 是 | 查找的下标值。需要小于等于int32_max即2147483647。 | 678| element | T | 是 | 用来替换的元素。 | 679 680**返回值:** 681 682| 类型 | 说明 | 683| -------- | -------- | 684| T | 返回替换后的元素 | 685 686**错误码:** 687 688以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 689 690| 错误码ID | 错误信息 | 691| -------- | -------- | 692| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 693| 10200001 | The value of index is out of range. | 694| 10200011 | The set method cannot be bound. | 695 696**示例:** 697 698```ts 699let list: List<number | string> = new List(); 700list.add(2); 701list.add(4); 702list.add(5); 703list.add(4); 704let result = list.set(2, "b"); 705``` 706 707### convertToArray 708 709convertToArray(): Array<T> 710 711把当前List实例转换成数组,并返回转换后的数组。 712 713**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 714 715**系统能力:** SystemCapability.Utils.Lang 716 717**返回值:** 718 719| 类型 | 说明 | 720| -------- | -------- | 721| Array<T> | 返回转换后的数组。 | 722 723**错误码:** 724 725以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 726 727| 错误码ID | 错误信息 | 728| -------- | -------- | 729| 10200011 | The convertToArray method cannot be bound. | 730 731**示例:** 732 733```ts 734let list: List<number> = new List(); 735list.add(2); 736list.add(4); 737list.add(5); 738list.add(4); 739let result = list.convertToArray(); 740``` 741 742### isEmpty 743 744isEmpty(): boolean 745 746判断该List是否为空。 747 748**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 749 750**系统能力:** SystemCapability.Utils.Lang 751 752**返回值:** 753 754| 类型 | 说明 | 755| -------- | -------- | 756| boolean | 为空返回true,不为空返回false。 | 757 758**错误码:** 759 760以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 761 762| 错误码ID | 错误信息 | 763| -------- | -------- | 764| 10200011 | The isEmpty method cannot be bound. | 765 766**示例:** 767 768```ts 769let list: List<number> = new List(); 770list.add(2); 771list.add(4); 772list.add(5); 773list.add(4); 774let result = list.isEmpty(); 775``` 776 777### getFirst 778 779getFirst(): T 780 781获取List实例中的第一个元素。 782 783**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 784 785**系统能力:** SystemCapability.Utils.Lang 786 787**返回值:** 788 789| 类型 | 说明 | 790| -------- | -------- | 791| T | 返回实例的第一个元素。 | 792 793**错误码:** 794 795以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 796 797| 错误码ID | 错误信息 | 798| -------- | -------- | 799| 10200011 | The getFirst method cannot be bound. | 800 801**示例:** 802 803```ts 804let list: List<number> = new List(); 805list.add(2); 806list.add(4); 807list.add(5); 808list.add(4); 809let result = list.getFirst(); 810``` 811 812### getLast 813 814getLast(): T 815 816获取List实例中的最后一个元素。 817 818**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 819 820**系统能力:** SystemCapability.Utils.Lang 821 822**返回值:** 823 824| 类型 | 说明 | 825| -------- | -------- | 826| T | 返回实例的最后一个元素。 | 827 828**错误码:** 829 830以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 831 832| 错误码ID | 错误信息 | 833| -------- | -------- | 834| 10200011 | The getLast method cannot be bound. | 835 836**示例:** 837 838```ts 839let list: List<number> = new List(); 840list.add(2); 841list.add(4); 842list.add(5); 843list.add(4); 844let result = list.getLast(); 845``` 846 847### [Symbol.iterator] 848 849[Symbol.iterator]\(): IterableIterator<T> 850 851返回一个迭代器,迭代器的每一项都是一个 JavaScript 对象,并返回该对象。 852 853**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 854 855**系统能力:** SystemCapability.Utils.Lang 856 857**返回值:** 858 859| 类型 | 说明 | 860| -------- | -------- | 861| IterableIterator<T> | 返回一个迭代器。 | 862 863**错误码:** 864 865以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 866 867| 错误码ID | 错误信息 | 868| -------- | -------- | 869| 10200011 | The Symbol.iterator method cannot be bound. | 870 871**示例:** 872 873```ts 874let list: List<number> = new List(); 875list.add(2); 876list.add(4); 877list.add(5); 878list.add(4); 879 880// 使用方法一: 881let items = Array.from(list) 882for (let item of items) { 883 console.log("value: " + item); 884} 885 886// 使用方法二: 887let iter = list[Symbol.iterator](); 888let temp: IteratorResult<number> = iter.next(); 889while(!temp.done) { 890 console.log("value: " + temp.value); 891 temp = iter.next(); 892} 893```