The class jsgl.util.ArrayList
is a simple ArrayList implementation, providing methods for working with array of a dynamic size:
jsgl.util.ArrayList
is used internally by many API classes in JSGL. However, if you wish, you may use it as well in your own applications that use JSGL.
To create an ArrayList, use the jsgl.util.ArrayList
constructor:
var myList = new jsgl.util.ArrayList();
Name | Description |
---|---|
add(item: value) | Appends an item at the end of the list. |
clear([filter: Function]) | Removes items in the ArrayList with respect to provided filter function. If no filter function is provided, all the elements are removed from the list. |
contains(item: value) | Tests whether a given item is present in the list. |
get(index: Number) : value | Gets an item present at the specified index in the list. |
getCount : Number | Gets the number of items that are currently contained in the list. |
insertAt(item: value, index: Number) | Inserts and item at the specified index. |
remove(x: value) | Removes all the items that are equal to x from the list. |
removeAt(index: Number) | Removes an item at the specified index from the list. |
reverse() | Reverses the ordering of items in the list. |
setAt(item: value, index: Number) | Sets an item at specified index. If any item is already present at the index, it will be replaced. |
sort([comparator: Function]) | Sorts the list according to given comparator function. If no comparator is given, default comparison will be used. |
add(item: value)
Appends an item at the end of the list.
Name | Type | Description |
---|---|---|
item | any | The item to be appended. |
Create an ArrayList and append some heterogenous values:
var myList = new jsgl.util.ArrayList(); myList.add(42); myList.add('foo'); myList.add(function() { window.alert('bah') } ); myList.add(null); myList.add(true); window.alert(myList.getCount()); // shows "5"
clear([filter: Function])
Removes items in the ArrayList with respect to provided filter function. If no filter function is provided, all the elements are removed from the list.
Remove all the items from the list, resulting in length of 0:
myList.clear();
Remove negative numbers from the list:
var myList = new jsgl.util.ArrayList(); myList.add(8); myList.add(Math.PI); myList.add(-3); myList.add(-Math.PI); myList.add(42); myList.clear(function(item) { return item < 0; }); // now the list is [8, Math.PI, 42]
contains(item: value)
Tests whether a given item is present in the list.
Name | Type | Description |
---|---|---|
item | any | The item to be tested for presence. |
Boolean
var myList = new jsgl.util.ArrayList(); myList.add(1); myList.add(9); myList.add(8); myList.add(6); window.alert(myList.contains(2)); // shows "false" window.alert(myList.contains(8)); // shows "true" window.alert(myList.contains('bah')); // shows "false"
get(index: Number) : value
Gets an item present at the specified index in the list.
Name | Type | Description |
---|---|---|
index | Number | Index of the item to be returned, starting from 0. |
value
Obtain the 3rd item from the list:
var myList = new jsgl.util.ArrayList(); myList.add('zero'); myList.add('one'); myList.add('two'); myList.add('three'); myList.add('four'); window.alert(myList.get(2)); // shows 'two'
getCount() : Number
Gets the number of items that are currently contained in the list.
Number
ā€“ The current length of the list.
var myList = new jsgl.util.ArrayList(); window.alert(myList.getCount()); // shows 0 myList.add(4); myList.add(2); myList.add(4); window.alert(myList.getCount()); // shows 3
insertAt(item: value, index: Number)
Inserts and item at the specified index. All the elements starting at the index up to the end of the list are shifted right.
Name | Type | Description |
---|---|---|
item | any | The item to be inserted into the list. |
index | Number | Index where the item shall be inserted at. |
var myList = new jsgl.util.ArrayList(); myList.add(1); myList.add(8); myList.add(6); // now the list is [1, 8, 6] myList.insertAt(9,1); // now the list is [1, 9, 8, 6]
remove(x: value)
Removes all the items that are equal to x
from the list.
Name | Type | Value |
---|---|---|
x | any | The value whose all occurances will be removed from the list. |
Remove all the 'none' strings from the list
var myList = new jsgl.util.ArrayList(); myList.add('Annie'); myList.add('none'); myList.add('Venitte'); myList.add('none'); myList.add('Venda'); myList.remove('none'); // now the list is ['Annie','Venitte','Venda']
removeAt(index: Number)
Removes an item at the specified index from the list. The rest of the list is shifted left after the element is removed.
Name | Type | Description |
---|---|---|
index | Number | The index of the item to be removed. |
Remove the 2nd item from the list:
var myList = new jsgl.util.ArrayList(); myList.add(2); myList.add(8); myList.add(5); myList.removeAt(1); // now the list is [2, 5]
reverse()
Reverses the whole list.
Reverse the list of letters:
var myList = new jsgl.util.ArrayList(); myList.add("a"); myList.add("b"); myList.add("c"); myList.add("d"); // now the list is ["a","b","c","d"] myList.reverse(); // now the list is ["d","c","b","a"]
setAt(item: value, index: number)
Sets an item at specified index. If any item is already present at the index, it will be replaced.
Name | Type | Description |
---|---|---|
item | any | The item to be placed at the index. |
index | Number | Index of the item to be set. |
Replace 7
with 8
in the list:
var myList = new jsgl.util.ArrayList() myList.add(1); myList.add(9); myList.add(7); myList.add(6); // now the list is [1, 9, 7, 6] myList.setAt(8, 2); // now the list is [1, 9, 8, 6]
sort([comparator: Function])
Sorts the list according to a given comparator function. If no comparator is given, default comparison is used.
Name | Type | Description |
---|---|---|
comparator | Function(value,value) : Number | The comparator function to be used for sorting. |
version 1.0