ArrayUtil

Kind of class:class
Inherits from:none
Version:06/08/07
Author:Aaron Clinger, David Nelson
Classpath:org.casalib.util.ArrayUtil
File last modified:Monday, 01 December 2008, 13:34:40
Utilities for sorting, searching and manipulating Arrays.

Summary


Class methods
  • equals (first:Array, second:Array) : Boolean
    • Determines if two Arrays contain the same objects at the same index.
  • removeDuplicates (inArray:Array) : Array
    • Creates new Array comprised of only the non-identical elements of passed Array.
  • removeArrayItem (tarArray:Array, item:Object) : Number
    • Modifies original Array by removing all items that are identical to passed item.
  • removeArrayItems (tarArray:Array, items:Array) : Boolean
    • Removes only the specified items in an Array.
  • retainArrayItems (tarArray:Array, items:Array) : Boolean
    • Retains only the specified items in an Array.
  • indexOf (inArray:Array, item:Object, startIndex:Number) : Number
    • Finds the position of the first instance of passed item in inArray.
  • lastIndexOf (inArray:Array, item:Object, startIndex:Number) : Number
    • Finds the position of the last instance of passed item in inArray.
  • contains (inArray:Array, item:Object) : Number
    • Finds out how many instances of item Array contains.
  • containsAll (inArray:Array, items:Array) : Boolean
    • Determines if Array contains all items.
  • containsAny (inArray:Array, items:Array) : Boolean
    • Determines if Array inArray contains any element of Array items.
  • randomize (inArray:Array) : Array
    • Creates new Array composed of passed Array's items in a random order.
  • sum (inArray:Array) : Number
    • Adds all items in inArray and returns the value.
  • average (inArray:Array) : Number
    • Averages the values in inArray.
  • getLowestValue (inArray:Array) : Number
    • Finds the lowest value in inArray.
  • getHighestValue (inArray:Array) : Number
    • Finds the highest value in inArray.

Class methods

average

static function average (
inArray:Array) : Number

Averages the values in inArray.
Parameters:
inArray:
Array comprised only of numbers.
Returns:
  • The average of all numbers in the inArray.
Usage:
  • var numberArray:Array = new Array(2, 3, 8, 3);
    trace("Average is: " + ArrayUtil.average(numberArray));

contains

static function contains (
inArray:Array, item:Object) : Number

Finds out how many instances of item Array contains.
Parameters:
inArray:
Array to search for item in.
item :
Object to find.
Returns:
  • The amount of item's found; if none were found returns 0.
Usage:
  • var numberArray:Array = new Array(1, 2, 3, 7, 7, 7, 4, 5);
    trace("numberArray contains " + ArrayUtil.contains(numberArray, 7) + " 7's.");
Usage note:
  • If you are trying to find if an array contains an item and don't need to know the total, use indexOf instead. item can be any object; Number, String, Object, etc...

containsAll

static function containsAll (
inArray:Array, items:Array) : Boolean

Determines if Array contains all items.
Parameters:
inArray:
Array to search for items in.
items :
Array of elements to search for.
Returns:
  • Returns true if inArray contains all elements of items; otherwise false.
Usage:
  • var numberArray:Array = new Array(1, 2, 3, 4, 5);
    trace(ArrayUtil.containsAll(numberArray, new Array(1, 3, 5)));

containsAny

static function containsAny (
inArray:Array, items:Array) : Boolean

Determines if Array inArray contains any element of Array items.
Parameters:
inArray:
Array to search for items in.
items :
Array of elements to search for.
Returns:
  • Returns true if inArray contains any element of items; otherwise false.
Usage:
  • var numberArray:Array = new Array(1, 2, 3, 4, 5);
    trace(ArrayUtil.containsAny(numberArray, new Array(1, 3, 5)));

equals

static function equals (
first:Array, second:Array) : Boolean

Determines if two Arrays contain the same objects at the same index.
Parameters:
first :
First Array to compare to second.
second:
Second Array to compare to first.
Returns:
  • Returns true if Arrays are the same; otherwise false.

getHighestValue

static function getHighestValue (
inArray:Array) : Number

Finds the highest value in inArray.
Parameters:
inArray:
Array comprised only of numbers.
Returns:
  • The highest value in inArray.
Usage:
  • var numberArray:Array = new Array(2, 1, 5, 4, 3);
    trace("The highest value is: " + ArrayUtil.getHighestValue(numberArray));
Since:
  • Flash Player 7

getLowestValue

static function getLowestValue (
inArray:Array) : Number

Finds the lowest value in inArray.
Parameters:
inArray:
Array comprised only of numbers.
Returns:
  • The lowest value in inArray.
Usage:
  • var numberArray:Array = new Array(2, 1, 5, 4, 3);
    trace("The lowest value is: " + ArrayUtil.getLowestValue(numberArray));
Since:
  • Flash Player 7

indexOf

static function indexOf (
inArray:Array, item:Object, startIndex:Number) : Number

Finds the position of the first instance of passed item in inArray.
Parameters:
inArray :
Array to find item's position in.
item :
Object to find position of.
startIndex:
[optional] The starting position of the search.
Returns:
  • The first index of the instance item; if none found returns -1.
Usage:
  • var colorArray = new Array("red", "blue", "pink", "black");
    trace("First postion of 'pink' is: " + ArrayUtil.indexOf(colorArray, "pink"));
Usage note:
  • item can be any object; Number, String, Object, etc...

lastIndexOf

static function lastIndexOf (
inArray:Array, item:Object, startIndex:Number) : Number

Finds the position of the last instance of passed item in inArray.
Parameters:
inArray :
Array to find item's position in.
item :
Object to find position of.
startIndex:
[optional] The starting position of the search.
Returns:
  • The last index of the instance item; if none found returns -1.
Usage:
  • var colorArray = new Array("red", "blue", "pink", "black");
    trace("First postion of 'pink' is: " + ArrayUtil.indexOf(colorArray, "pink"));
Usage note:
  • item can be any object; Number, String, Object, etc...

randomize

static function randomize (
inArray:Array) : Array

Creates new Array composed of passed Array's items in a random order.
Parameters:
inArray:
Array to create copy of, and randomize.
Returns:
  • A new Array comprised of passed Array's items in a random order.
Usage:
  • var numberArray:Array = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    trace(ArrayUtil.randomize(numberArray));
Since:
  • Flash Player 7

removeArrayItem

static function removeArrayItem (
tarArray:Array, item:Object) : Number

Modifies original Array by removing all items that are identical to passed item.
Parameters:
tarArray:
Array to remove passed item.
item :
Value to remove.
Returns:
  • The amount of removed elements that matched item, if none found returns 0.
Usage:
  • var numberArray:Array = new Array(1, 2, 3, 7, 7, 7, 4, 5);
    trace("Removed " + ArrayUtil.removeArrayItem(numberArray, 7) + " items.");
    trace(numberArray);
Usage note:
  • item can be any object; Number, String, Object, etc...

removeArrayItems

static function removeArrayItems (
tarArray:Array, items:Array) : Boolean

Removes only the specified items in an Array.
Parameters:
tarArray:
Array to remove specified items from.
items :
Array of elements to remove.
Returns:
  • Returns true if the Array was changed as a result of the call; otherwise false.
Usage:
  • var numberArray:Array = new Array(1, 2, 3, 7, 7, 7, 4, 5);
    ArrayUtil.removeArrayItems(numberArray, new Array(1, 3, 7, 5));
    trace(numberArray);

removeDuplicates

static function removeDuplicates (
inArray:Array) : Array

Creates new Array comprised of only the non-identical elements of passed Array.
Parameters:
inArray:
Array to remove equivalent items.
Returns:
  • A new Array comprised of only unique elements.
Usage:
  • var numberArray:Array = new Array(1, 2, 3, 4, 4, 4, 4, 5);
    trace(ArrayUtil.removeDuplicates(numberArray));

retainArrayItems

static function retainArrayItems (
tarArray:Array, items:Array) : Boolean

Retains only the specified items in an Array.
Parameters:
tarArray:
Array to remove non specified items from.
items :
Array of elements to keep.
Returns:
  • Returns true if the Array was changed as a result of the call; otherwise false.
Usage:
  • var numberArray:Array = new Array(1, 2, 3, 7, 7, 7, 4, 5);
    ArrayUtil.retainArrayItems(numberArray, new Array(2, 4));
    trace(numberArray);

sum

static function sum (
inArray:Array) : Number

Adds all items in inArray and returns the value.
Parameters:
inArray:
Array comprised only of numbers.
Returns:
  • The total of all numbers in inArray added.
Usage:
  • var numberArray:Array = new Array(2, 3);
    trace("Total is: " + ArrayUtil.sum(numberArray));