Packageorg.as3collections.lists
Classpublic class ArrayList
InheritanceArrayList Inheritance AbstractList Inheritance AbstractArrayCollection Inheritance Object
Subclasses SortedArrayList

Resizable-array implementation of the IList interface. Implements all optional list operations, and permits all elements, including null.

Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList object, its capacity grows automatically.

In addition to implementing the IList interface, this class provides the ensureCapacity method to arbitrarily manipulate the size of the array (this usage is not common) that is used internally to store the elements. Check the examples at the bottom of the page for further information about usage.

It's possible to create unique lists, typed lists and even unique typed lists. You just send the ArrayList object to the wrappers UniqueList or TypedList or uses the ListUtil.getUniqueList, ListUtil.getTypedList or ListUtil.getUniqueTypedList.

This documentation is partially based in the Java Collections Framework JavaDoc documentation. For further information see Java Collections Framework

View the examples

See also

AbstractList
TypedList
UniqueList
SortedArrayList
ListUtil.getUniqueList()
ListUtil.getTypedList()
ListUtil.getUniqueTypedList()


Public Properties
 PropertyDefined By
 InheritedallEquatable : Boolean
[read-only] Indicates whether all elements in this collection implement the interface org.as3coreaddendum.system.IEquatable.
AbstractArrayCollection
 InheritedmodCount : int
[read-only] The number of times this list has been structurally modified.
AbstractList
Public Methods
 MethodDefined By
  
ArrayList(source:Array = null)
Constructor, creates a new ArrayList object.
ArrayList
 Inherited
add(element:*):Boolean
[override] Appends the specified element to the end of this list (optional operation).
AbstractList
 Inherited
addAll(collection:ICollection):Boolean
[override] Adds all of the elements in the specified collection to this list (optional operation).
AbstractList
 Inherited
addAllAt(index:int, collection:ICollection):Boolean
Inserts all of the elements in the specified collection into this list at the specified position (optional operation).
AbstractList
  
addAt(index:int, element:*):Boolean
[override] Inserts the specified element at the specified position in this list.
ArrayList
  
clear():void
[override] Removes all of the elements from this list.
ArrayList
  
clone():*
[override] Creates and return a new ArrayList object containing all elements in this list (in the same order).
ArrayList
 Inherited
contains(o:*):Boolean
Returns true if this collection contains the specified object.
AbstractArrayCollection
 Inherited
containsAll(collection:ICollection):Boolean
Returns true if this collection contains all of the elements in the specified collection.
AbstractArrayCollection
  
ensureCapacity(minCapacity:int):void
Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minCapacity argument.
ArrayList
 Inherited
equals(other:*):Boolean
[override] This method uses CollectionUtil.equalConsideringOrder method to perform equality, sending this list and other argument.
AbstractList
 Inherited
getAt(index:int):*
Returns the element at the specified position in this list.
AbstractList
 Inherited
indexOf(element:*, fromIndex:int = 0):int
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
AbstractList
 Inherited
isEmpty():Boolean
Returns true if this collection contains no elements.
AbstractArrayCollection
  
[override] Returns an iterator over a set of elements.
ArrayList
 Inherited
lastIndexOf(element:*, fromIndex:int = 0x7fffffff):int
Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
AbstractList
  
[override] Returns a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list.
ArrayList
 Inherited
remove(o:*):Boolean
Removes a single instance (only one occurrence) of the specified object from this collection, if it is present (optional operation).
AbstractArrayCollection
 Inherited
removeAll(collection:ICollection):Boolean
Removes all of this collection's elements that are also contained in the specified collection (optional operation).
AbstractArrayCollection
  
removeAt(index:int):*
[override] Removes the element at the specified position in this list.
ArrayList
  
removeRange(fromIndex:int, toIndex:int):ICollection
[override] Removes all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.
ArrayList
 Inherited
retainAll(collection:ICollection):Boolean
Retains only the elements in this collection that are contained in the specified collection (optional operation).
AbstractArrayCollection
 Inherited
reverse():void
Reverses the order of the elements in this list.
AbstractList
  
setAt(index:int, element:*):*
[override] Replaces the element at the specified position in this list with the specified element.
ArrayList
 Inherited
size():int
Returns the number of elements in this collection.
AbstractArrayCollection
  
subList(fromIndex:int, toIndex:int):IList
[override] Returns a new ArrayList that is a view of the portion of this ArrayList between the specified fromIndex, inclusive, and toIndex, exclusive.
ArrayList
 Inherited
toArray():Array
Returns an array containing all of the elements in this collection.
AbstractArrayCollection
 Inherited
toString():String
Returns the string representation of this instance.
AbstractArrayCollection
Constructor Detail
ArrayList()Constructor
public function ArrayList(source:Array = null)

Constructor, creates a new ArrayList object.

Parameters
source:Array (default = null) — an array to fill the list.
Method Detail
addAt()method
override public function addAt(index:int, element:*):Boolean

Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Parameters

index:int — index at which the specified element is to be inserted.
 
element:* — the element to be added.

Returns
Booleantrue if this list changed as a result of the call.

Throws
IndexOutOfBoundsError — if the index is out of range (index < 0 || index > size()).
clear()method 
override public function clear():void

Removes all of the elements from this list. The list will be empty after this method returns.

clone()method 
override public function clone():*

Creates and return a new ArrayList object containing all elements in this list (in the same order).

Returns
* — a new ArrayList object containing all elements in this list (in the same order).
ensureCapacity()method 
public function ensureCapacity(minCapacity:int):void

Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minCapacity argument.

This implementation uses Array.length = minCapacity of the internal array object.

Parameters

minCapacity:int

iterator()method 
override public function iterator():IIterator

Returns an iterator over a set of elements.

This implementation returns an ArrayIterator object.

Returns
IIterator — an iterator over a set of elements.

See also

listIterator()method 
override public function listIterator(index:int = 0):IListIterator

Returns a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list. The specified index indicates the first element that would be returned by an initial call to next. An initial call to previous would return the element with the specified index minus one.

This implementation returns an ListIterator object.

Parameters

index:int (default = 0) — index of first element to be returned from the list iterator (by a call to the next method)

Returns
IListIterator — a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list.

See also

removeAt()method 
override public function removeAt(index:int):*

Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.

Parameters

index:int — the index of the element to be removed.

Returns
* — the element previously at the specified position.

Throws
IndexOutOfBoundsError — if the index is out of range (index < 0 || index >= size()).
removeRange()method 
override public function removeRange(fromIndex:int, toIndex:int):ICollection

Removes all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive. Shifts any subsequent elements to the left (subtracts their indices).

If toIndex == fromIndex, this operation has no effect.

Parameters

fromIndex:int — the index to start removing elements (inclusive).
 
toIndex:int — the index to stop removing elements (exclusive).

Returns
ICollection — a new ArrayList object containing all the removed elements.

Throws
IndexOutOfBoundsError — if fromIndex or toIndex is out of range (index < 0 || index > size()).
setAt()method 
override public function setAt(index:int, element:*):*

Replaces the element at the specified position in this list with the specified element.

Parameters

index:int — index of the element to replace.
 
element:* — element to be stored at the specified position.

Returns
* — the element previously at the specified position.

Throws
IndexOutOfBoundsError — if the index is out of range (index < 0 || index >= size()).
subList()method 
override public function subList(fromIndex:int, toIndex:int):IList

Returns a new ArrayList that is a view of the portion of this ArrayList between the specified fromIndex, inclusive, and toIndex, exclusive. This method uses native Array.slice method.

Modifications in the returned ArrayList object doesn't affect this list.

This list is not modified.

Parameters

fromIndex:int — the index to start retrieving elements (inclusive).
 
toIndex:int — the index to stop retrieving elements (exclusive).

Returns
IList — a new ArrayList that is a view of the specified range within this list.

Throws
IndexOutOfBoundsError — if fromIndex or toIndex is out of range (index < 0 || index > size()).
Examples
Example 1
     import org.as3collections.IList;
     import org.as3collections.lists.ArrayList;
     
     var list1:IList = new ArrayList();
     
     list1                           // []
     
     list1.size()                    // 0
     list1.contains(null)            // false
     list1.contains("abc")           // false
     list1.isEmpty()                 // true
     list1.modCount                  // 0
     
     list1.clear()
     
     list1.modCount                  // 0
     list1.isEmpty()                 // true
     list1.size()                    // 0
     
     list1.add(null)                 // true
     list1                           // [null]
     list1.isEmpty()                 // false
     list1.size()                    // 1
     list1.modCount                  // 1
     list1.contains(null)            // true
     list1.contains("abc")           // false
     
     list1.add("abc")                // true
     list1                           // [null,abc]
     list1.size()                    // 2
     list1.modCount                  // 2
     list1.contains("abc")           // true
     
     list1.add(null)                 // true
     list1                           // [null,abc,null]
     list1.size()                    // 3
     list1.modCount                  // 3
     list1.indexOf(null)             // 0
     list1.lastIndexOf(null)         // 2
     
     list1.addAt(0, 123)             // true
     list1                           // [123,null,abc,null]
     list1.size()                    // 4
     list1.modCount                  // 4
     
     list1.addAt(4, "def")           // true
     list1                           // [123,null,abc,null,def]
     list1.size()                    // 5
     
     list1.addAt(4, "abc")           // true
     list1                           // [123,null,abc,null,abc,def]
     list1.size()                    // 6
     list1.modCount                  // 6
     
     list1.getAt(0)                  // 123
     list1.getAt(2)                  // abc
     list1.getAt(5)                  // def
     
     list1.removeAt(0)               // 123
     list1                           // [null,abc,null,abc,def]
     list1.size()                    // 5
     list1.modCount                  // 7
     
     list1.removeAt(4)               // def
     list1                           // [null,abc,null,abc]
     list1.size()                    // 4
     list1.modCount                  // 8
     
     list1.removeAt(0)               // null
     list1                           // [abc,null,abc]
     list1.size()                    // 3
     list1.modCount                  // 9
     
     var list2:IList = list1.clone();
     
     list2                           // [abc,null,abc]
     
     list1.containsAll(list1)        // true
     list1.containsAll(list2)        // true
     list2.containsAll(list1)        // true
     list1.equals(list2)             // true
     
     list2.remove("abc")             // true
     list2.remove("abc")             // true
     list2.add(null)                 // true
     list2                           // [null,null]
     
     list1.containsAll(list2)        // true
     list2.containsAll(list1)        // false
     list1.equals(list2)             // false
     
     list1                           // [abc,null,abc]
     list1.size()                    // 3
     list1.setAt(2, "ghi")           // abc
     list1                           // [abc,null,ghi]
     list1.size()                    // 3
     list1.modCount                  // 9
     
     list1.clear()
     
     list1.modCount                  // 10
     list1.isEmpty()                 // true
     list1.size()                    // 0
     
Example 2
     import org.as3collections.IList;
     import org.as3collections.lists.ArrayList;
     
     var arr:Array = [1, 2, 3, 4];
     var list1:IList = new ArrayList(arr);
     
     list1                                 // [1,2,3,4]
     list1.size()                          // 4
     list1.isEmpty()                       // false
     list1.modCount                        // 0
     
     var list2:IList = new ArrayList([9, 10, 11, 12]);
     
     list2                                 // [9,10,11,12]
     list2.size()                          // 4
     list2.isEmpty()                       // false
     list2.modCount                        // 0
     
     list1.addAll(list2)                   // true
     list1                                 // [1,2,3,4,9,10,11,12]
     list1.size()                          // 8
     list1.modCount                        // 4
     
     var list3:IList = new ArrayList([5, 6, 7, 8]);
     
     list3                                 // [5,6,7,8]
     list3.size()                          // 4
     list3.isEmpty()                       // false
     list3.modCount                        // 0
     
     list1.addAllAt(4, list3)              // true
     list1                                 // [1,2,3,4,5,6,7,8,9,10,11,12]
     list1.size()                          // 12
     list1.modCount                        // 8
     
     list1.containsAll(list3)              // true
     list3.containsAll(list1)              // false
     
     list1.removeAll(list3)                // true
     list1                                 // [1,2,3,4,9,10,11,12]
     list1.size()                          // 8
     list1.modCount                        // 12
     
     list1.removeAll(list3)                // false
     list1                                 // [1,2,3,4,9,10,11,12]
     list1.size()                          // 8
     list1.modCount                        // 12
     
     list1.retainAll(list2)                // true
     list1                                 // [9,10,11,12]
     list1.size()                          // 4
     list1.modCount                        // 16
     
     list1.subList(0, 1)                   // [9]
     list1.subList(0, 2)                   // [9,10]
     list1.subList(0, 4)                   // [9,10,11,12]
     list1.subList(0, list1.size())        // [9,10,11,12]
     
     list1.removeRange(1, 3)               // [10,11]
     list1                                 // [9,12]
     list1.size()                          // 2
     list1.modCount                        // 17
     
     list1.remove(9)                       // true
     list1                                 // [12]
     list1.size()                          // 1
     list1.modCount                        // 18
     
     list1.retainAll(list3)                // true
     list1                                 // []
     list1.size()                          // 0
     list1.modCount                        // 19
     
Example 3
     import org.as3collections.IList;
     import org.as3collections.lists.ArrayList;
     
     var list1:ArrayList = new ArrayList();
     
     list1                     // []
     list1.addAt(3, 4)         // IndexOutOfBoundsError: The 'index' argument is out of bounds: 3 (min: 0, max: 0)
     
     list1.ensureCapacity(5)
     
     list1                     // [undefined,undefined,undefined,undefined,undefined]
     list1.modCount            // 1
     list1.isEmpty()           // false
     list1.size()              // 5
     
     list1.addAt(3, 4)         // true
     list1                     // [undefined,undefined,undefined,4,undefined,undefined]
     list1.modCount            // 2
     list1.size()              // 6
     
     list1.getAt(1)            // undefined
     
     list1.ensureCapacity(3)
     
     list1                     // [undefined,undefined,undefined,4,undefined,undefined]
     list1.modCount            // 2
     list1.size()              // 6
     
     list1.setAt(2, 3)         // undefined
     list1                     // [undefined,undefined,3,4,undefined,undefined]
     list1.modCount            // 2
     list1.size()              // 6
     
     list1.remove(undefined)   // true
     list1.remove(undefined)   // true
     list1                     // [3,4,undefined,undefined]
     list1.modCount            // 4
     list1.size()              // 4
     
Example 4 - Using equality (org.as3coreaddendum.system.IEquatable)
     package test
     {
         import org.as3coreaddendum.system.IEquatable;
     
         public class TestEquatableObject implements IEquatable
         {
             private var _id:String;
             
             public function get id(): String { return _id; }
     
             public function set id(value:String): void { _id = value; }
     
             public function TestEquatableObject(id:String)
             {
                 _id = id;
             }
     
             public function equals(other:: Boolean
             {
                 return other is TestEquatableObject && _id == (other as TestEquatableObject).id;
             }
     
             public function toString(): String
             {
                 return "[TestEquatableObject " + _id + "]";
             }
         }
     }
     
     import test.TestEquatableObject;
     
     import org.as3collections.IList;
     import org.as3collections.lists.ArrayList;
     import org.as3collections.lists.UniqueList;
     
     var list1:ArrayList = new ArrayList();
     
     list1                               // []
     
     var o1:TestEquatableObject = new TestEquatableObject("o1");
     var o2:TestEquatableObject = new TestEquatableObject("o2");
     var o3:TestEquatableObject = new TestEquatableObject("o3");
     var o4:TestEquatableObject = new TestEquatableObject("o4");
     
     list1.contains(o1)                  // false
     list1.add(o1)                       // true
     list1                               // [[TestEquatableObject o1]]
     list1.contains(o1)                  // true
     
     var o5:TestEquatableObject = new TestIndexablePriority("o1"); // -> Attention to the id, which is "o1"
     
     list1.contains(o5)                  // true -> without equality would return false, because o1 and o5 are different objects.
     
     list1.add(o5)                       // true
     list1                               // [[TestEquatableObject o1],[TestEquatableObject o1]]
     
     o1.equals(o5)                       // true
     o1.equals("abc")                    // false
     
     var list2:ArrayList = new ArrayList();
     
     list2.equals(list1)                 // false
     list2.add(o5)                       // true
     list1                               // [[TestEquatableObject o1],[TestEquatableObject o1]]
     list2                               // [[TestEquatableObject o1]]
     list2.equals(list1)                 // false
     
     list2.add(o5)                       // true
     list1                               // [[TestEquatableObject o1],[TestEquatableObject o1]]
     list2                               // [[TestEquatableObject o1],[TestEquatableObject o1]]
     list2.equals(list1)                 // true
     
     list2.remove(o1)                    // true -> equality used
     list2                               // [[TestEquatableObject o1]]
     
     var uniqueList:UniqueList = new UniqueList(new ArrayList());
     
     uniqueList.contains(o1)             // false
     uniqueList.add(o1)                  // true
     uniqueList                          // [[TestEquatableObject o1]]
     uniqueList.contains(o1)             // true
     
     uniqueList.add(o5)                  // false
     uniqueList.contains(o5)             // true -> by equality the object o5 is in the list because its 'id' is the same of the object o1.
     uniqueList                          // [[TestEquatableObject o1]]