ArrayUtil

Kind of class:public class
Package:org.casalib.util
Inherits from:none
Version:10/06/08
Author:Aaron Clinger, David Nelson
Classpath:org.casalib.util.ArrayUtil
File last modified:Wednesday, 31 December 2008, 20:18:28
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.
  • addItemsAt (tarArray:Array, items:Array, index:int = 0x7fffffff) : Boolean
    • Modifies original Array by adding all the elements from another Array at a specified position.
  • removeDuplicates (inArray:Array) : Array
    • Creates new Array composed of only the non-identical elements of passed Array.
  • removeItem (tarArray:Array, item:*) : uint
    • Modifies original Array by removing all items that are identical to the specified item.
  • removeItems (tarArray:Array, items:Array) : Boolean
    • Removes only the specified items in an Array.
  • retainItems (tarArray:Array, items:Array) : Boolean
    • Retains only the specified items in an Array.
  • contains (inArray:Array, item:*) : uint
    • 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.
  • getIndexOfDifference (first:Array, second:Array, fromIndex:uint = 0) : int
    • Compares two Arrays and finds the first index where they differ.
  • 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

addItemsAt

public static function addItemsAt (
tarArray:Array, items:Array, index:int = 0x7fffffff) : Boolean

Modifies original Array by adding all the elements from another Array at a specified position.
Parameters:
tarArray:
Array to add elements to.
items :
Array of elements to add.
index :
Position where the elements should be added.
Returns:
  • Returns true if the Array was changed as a result of the call; otherwise false.
Example:
  • var alphabet:Array = new Array("a", "d", "e");
    var parts:Array    = new Array("b", "c");
    
    ArrayUtil.addItemsAt(alphabet, parts, 1);
    
    trace(alphabet);

average

public static function average (
inArray:Array) : Number

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

contains

public static function contains (
inArray:Array, item:*) : uint

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.
Example:
  • var numberArray:Array = new Array(1, 2, 3, 7, 7, 7, 4, 5);
    trace("numberArray contains " + ArrayUtil.contains(numberArray, 7) + " 7's.");

containsAll

public 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.
Example:
  • var numberArray:Array = new Array(1, 2, 3, 4, 5);
    trace(ArrayUtil.containsAll(numberArray, new Array(1, 3, 5)));

containsAny

public 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.
Example:
  • var numberArray:Array = new Array(1, 2, 3, 4, 5);
    trace(ArrayUtil.containsAny(numberArray, new Array(9, 3, 6)));

equals

public 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 the second.
second:
Second Array to compare to the first.
Returns:
  • Returns true if Arrays are the same; otherwise false.

getHighestValue

public static function getHighestValue (
inArray:Array) : Number

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

getIndexOfDifference

public static function getIndexOfDifference (
first:Array, second:Array, fromIndex:uint = 0) : int

Compares two Arrays and finds the first index where they differ.
Parameters:
first :
First Array to compare to the second.
second :
Second Array to compare to the first.
fromIndex:
The location in the Arrays from which to start searching for a difference.
Returns:
  • The first position/index where the Arrays differ; if Arrays are identical returns -1.
Example:
  • var color:Array     = new Array("Red", "Blue", "Green", "Indigo", "Violet");
    var colorsAlt:Array = new Array("Red", "Blue", "Green", "Violet");
    
    trace(ArrayUtil.getIndexOfDifference(color, colorsAlt)); // Traces 3

getLowestValue

public static function getLowestValue (
inArray:Array) : Number

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

randomize

public 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 composed of passed Array's items in a random order.
Example:
  • var numberArray:Array = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    trace(ArrayUtil.randomize(numberArray));

removeDuplicates

public static function removeDuplicates (
inArray:Array) : Array

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

removeItem

public static function removeItem (
tarArray:Array, item:*) : uint

Modifies original Array by removing all items that are identical to the specified item.
Parameters:
tarArray:
Array to remove passed item.
item :
Element to remove.
Returns:
  • The amount of removed elements that matched item, if none found returns 0.
Example:
  • var numberArray:Array = new Array(1, 2, 3, 7, 7, 7, 4, 5);
    trace("Removed " + ArrayUtil.removeArrayItem(numberArray, 7) + " items.");
    trace(numberArray);

removeItems

public static function removeItems (
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.
Example:
  • var numberArray:Array = new Array(1, 2, 3, 7, 7, 7, 4, 5);
    ArrayUtil.removeItems(numberArray, new Array(1, 3, 7, 5));
    trace(numberArray);

retainItems

public static function retainItems (
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.
Example:
  • var numberArray:Array = new Array(1, 2, 3, 7, 7, 7, 4, 5);
    ArrayUtil.retainItems(numberArray, new Array(2, 4));
    trace(numberArray);

sum

public static function sum (
inArray:Array) : Number

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