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