JavaScript Graphics Library (JSGL.org) Draw and move interactive vector graphics easily in Javascript!

ArrayList (class jsgl.util.ArrayList)

The class jsgl.util.ArrayList is a simple ArrayList implementation, providing methods for working with array of a dynamic size:

  • appending values to the list,
  • removing values from the list,
  • getting values from the list,
  • replacing values in the list,
  • sorting the list,
  • clearing the list.

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.

Creation

To create an ArrayList, use the jsgl.util.ArrayList constructor:

var myList = new jsgl.util.ArrayList();

Method Summary

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.

Method Detail

add(item: value)

Appends an item at the end of the list.

Params

Name Type Description
item any The item to be appended.

Example

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"

Since

version 1.0

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.

Example 1

Remove all the items from the list, resulting in length of 0:

myList.clear();

Example 2

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]

Since

version 1.0

contains(item: value)

Tests whether a given item is present in the list.

Parameters

Name Type Description
item any The item to be tested for presence.

Returns

Boolean

Example

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"

Since

version 1.0

get(index: Number) : value

Gets an item present at the specified index in the list.

Parameters

Name Type Description
index Number Index of the item to be returned, starting from 0.

Returns

value

Example

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'

Since

version 1.0

getCount() : Number

Gets the number of items that are currently contained in the list.

Returns

Number – The current length of the list.

Example

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

Since

version 1.0

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.

Parameters

Name Type Description
item any The item to be inserted into the list.
index Number Index where the item shall be inserted at.

Example

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]

Since

version 1.0

remove(x: value)

Removes all the items that are equal to x from the list.

Parameters

Name Type Value
x any The value whose all occurances will be removed from the list.

Example

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']

Since

version 1.0

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.

Parameters

Name Type Description
index Number The index of the item to be removed.

Example

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]

Since

version 1.0

reverse()

Reverses the whole list.

Example

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"]

Since

version 1.0

setAt(item: value, index: number)

Sets an item at specified index. If any item is already present at the index, it will be replaced.

Parameters

Name Type Description
item any The item to be placed at the index.
index Number Index of the item to be set.

Example

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]

Since

version 1.0

sort([comparator: Function])

Sorts the list according to a given comparator function. If no comparator is given, default comparison is used.

Parameters

Name Type Description
comparator Function(value,value) : Number The comparator function to be used for sorting.

Since

version 1.0

 
arraylist.txt · Last modified: 2013/03/14 03:16 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: GNU Free Documentation License 1.3
Driven by DokuWiki Powered by PHP Valid XHTML 1.0 Valid CSS