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