List


Scenario

Description: Create a List object to contain a collection of mixed objects.

var list = new List();
list.add(1);
list.addRange(2, 3, 4, 5);
list.addRange("foo", 6, "bar", 7);

for (var i = 0; i < list.length; i++)
    print(list[i]);

list.clear();
list.addRange(1, 2, 3, 4, 5, 6, 7, 8);
list.removeAll(function (q) { return q > 4; });

for (var i = 0; i < list.length; i++)
    print(list[i]);

add

Type: Function
Return: int
Description: Add a new element to the list

list = new List();
list.add(12345);

addRange

Type: Function
Return: int
Description: Add a number of elements to the list

list = new List();
list.addRange(1, 2, 3, 4, 5);

clear

Type: Function
Return: int
Description: Remove all elements from the list

list = new List();
list.add(12345);
list.clear();

count

Type: Property (read only)
Return: int
Description: Get the number of elements contained in the list

list = new List();
list.addRange(1, 2, 3, 4, 5);
print("there are " + list.count + " elements in this list");

find

Type: Function
Return: mixed
Description: Get the first element matching a query

list = new List();
list.addRange(1, 2, 3, 4, 50);
item = list.find(function (q) { return q > 4; });
print(item);

findAll

Type: Function
Return: List
Description: Get a List of all elements matching a query

list = new List();
list.addRange(1, 2, 3, 4, 50);
list2 = list.findAll(function (q) { return q > 3; });
print("found " + list2.count + " items");

findIndex

Type: Function
Return: int
Description: Get the index of the first element matching a query

list = new List();
list.addRange(1, 2, 3, 4, 50);
index = list.findIndex(function (q) { return q > 3; });
print(list[index]);

findLastIndex

Type: Function
Return: int
Description: Get the index of the last element matching a query

list = new List();
list.addRange(1, 2, 3, 4, 50);
index = list.findIndex(function (q) { return q > 3; });
print(list[index]);

getRange

Type: Function
Return: List
Description: Get a list of elements between a certain index range

list = new List();
list.addRange(1, 2, 3, 4, 50);
list2 = list.getRange(0, 2);

for (var i = 0; i < list2.length; i++)
    print(list2[i]); // 1 and 2

indexOf

Type: Function
Return: int
Description: Get the index of the first element which equals an object

list = new List();
list.addRange(1, 2, 3, 4, 50, 3);
index = list.indexOf(3);
print(index); // 2

insert

Type: Function
Return: int
Description: Insert an element into the list

list = new List();
list.addRange(1, 2, 3, 4, 50, 3);
list.insert(0, "foo"); // list is now ["foo", 1, 2, 3, 4, 50, 3]

insertRange

Type: Function
Return: int
Description: Insert a number of elements into the list

list = new List();
list.addRange(1, 2, 3, 4, 50, 3);
list.insertRange(0, "foo", "bar"); // list is now ["foo", "bar", 1, 2, 3, 4, 50, 3]

join

Type: Function
Return: String
Description: Join all the elements in the list into a string

list = new List();
list.addRange(1, 2, 3, 4, 50, 3);
print(list.join(":"); // "1:2:3:4:50:3"

lastIndexOf

Type: Function
Return: int
Description: Get the index of the last element which equals an object

list = new List();
list.addRange(1, 2, 3, 4, 50, 3);
index = list.lastIndexOf(3);
print(index); // 5

length

Type: Property (read only)
Return: int
Description: The same as count

remove

Type: Function
Return: int
Description: Remove the first element which equals an object

list = new List();
list.addRange(1, 2, 3, 4, 50, 3);
list.remove(3); // list now [1, 2, 4, 50, 3]

removeAll

Type: Function
Return: int
Description: Remove all elements that match a query

list = new List();
list.addRange(1, 2, 3, 4, 50, 3);
list.removeAll(function (q) { return q > 3; }); // list now [1, 2]

removeAt

Type: Function
Return: int
Description: Remove an element at a specific index

list = new List();
list.addRange(1, 2, 3, 4, 50, 3);
list.removeAt(0); // list now [2, 3, 4, 50, 3]

removeRange

Type: Function
Return: int
Description: Remove a sequence of elements

list = new List();
list.addRange(1, 2, 3, 4, 50, 3); // remove first 4 elements
list.removeRange(0, 4); // list now [50, 3]

reverse

Type: Function
Return: bool
Description: Reverse the list elements

list = new List();
list.addRange(1, 2, 3, 4, 50, 3);
list.reverse(); // list now [3, 50, 4, 3, 2, 1]

sort

Type: Function
Return: bool
Description: Sort the list based on a comparison

list = new List();
list.addRange(1, 2, 3, 4, 50, 3);
list.sort(function (x, y) { return x > y ? 1 : x < y ? -1 : 0; });
print(list); // list now [1, 2, 3, 3, 4, 50]

Last edited Dec 22, 2012 at 3:03 PM by oobenoob, version 4