阅读更多
var array:Array = new Array();
array.push("val 1", "val 2");
array[array.length] = "val 3";
array.unshift("z");
trace(array.shift());
trace(array.pop());
for (var i:int = 0; i < array.length; i++) {
trace(array[i]);
}
trace(ArrayUtilities.findMatchIndices(array, "val"));
array.splice(0, 2);
array.splice(1, 0, "r", "s", "t");
var list:String = "Peter Piper picked a peck of pickled peppers";
var words:Array = list.split(" ");
trace(words.join("|"));
var newWords:Array = words.concat();
var newWords2:Array = words.slice(0);
var newWords3:Array = ArrayUtilities.duplicate(words, true);
words.sort();
words.sort(Array.DESCENDING);
words.sort(Array.CASEINSENSITIVE);
var scores:Array = [10, 2, 2, 1, 1, 14, 5, 8, 20, 19, 6];
scores.sort(Array.NUMERIC);
var sortedScores:Object = scores.sort(Array.UNIQUESORT);
trace(sortedScores);
trace(scores);
var words:Array = ["tricycle", "relative", "aardvark", "jargon"];
var indices:Array = words.sort(Array.RETURNINDEXEDARRAY);
trace(words);
trace(indices);
for(var i:int = 0; i < words.length; i++) {
trace(words[indices[i]]);
}
words.sort(Array.CASEINSENSITIVE | Array.DESCENDING);
words.reverse();
var cars:Array = new Array( );
cars.push({make: "Honda", year: 1997, color: "maroon"});
cars.push({make: "Chrysler", year: 2000, color: "beige"});
cars.push({make: "Mercedes", year: 1985, color: "blue"});
cars.push({make: "Fiat", year: 1983, color: "gray"});
cars.push({make: "Honda", year: 1992, color: "silver"});
cars.push({make: "Chrysler", year: 1968, color: "gold"});
cars.push({make: "Mercedes", year: 1975, color: "green"});
cars.push({make: "Fiat", year: 1983, color: "black"});
cars.push({make: "Honda", year: 2001, color: "blue"});
cars.push({make: "Chrysler", year: 2004, color: "orange"});
cars.push({make: "Mercedes", year: 2000, color: "white"});
cars.push({make: "Fiat", year: 1975, color: "yellow"});
cars.sortOn(["year", "make"]);
cars.sortOn("year", Array.DESCENDING);
var bands:Array = ["The Clash",
"The Who",
"Led Zeppelin",
"The Beatles",
"Aerosmith",
"Cream"];
function bandNameSort(band1:String, band2:String):int
{
band1 = band1.toLowerCase();
band2 = band2.toLowerCase();
if(band1.substr(0,4) == "the ") {
band1 = band1.substr(4);
}
if(band2.substr(0,4) == "the ") {
band2 = band2.substr(4);
}
if(band1 < band2) {
return -1;
}
else {
return 1;
}
}
bands.sort(bandNameSort);
var numbers:Array = new Array();
for(var i:int=0;i<20;i++) {
numbers[i] = i;
}
function randomSort(elementA:Object, elementB:Object):Number {
return Math.random() - .5
}
numbers.sort(randomSort);
var scores:Array = [10, 4, 15, 8];
scores.sort(Array.NUMERIC);
trace("Minimum: " + scores[0]);
trace("Maximum: " + scores[scores.length - 1]);
var letters:Array = ["a", "b", "c", "d"];
var lettersPointer:Array = letters;
trace(letters == lettersPointer); // Display: true
var letters1:Array = ["a", "b", "c", "d"];
var letters2:Array = ["a", "b", "c", "d"];
trace(letters1 == letters2); // Display: false
var equivalent:Boolean = true;
for(var i:int = 0; i < letters1.length; i++) {
if(letters1[i] != letters2[i]) {
equivalent = false;
break;
}
}
trace(equivalent); // Display: true
trace(ArrayUtilities.equals(letter1, letters2)); // Display: true
var letters1:Array = ["a", "b", "c", "d"];
var letters2:Array = ["b", "a", "d", "c"];
trace(ArrayUtilities.equals(letters1, letters2));
// Displays: false
trace(ArrayUtilities.equals(letters1, letters2, true));
// Displays: true
public static function equals(arrayA:Array,
arrayB:Array,
bNotOrdered:Boolean):Boolean {
// If the two arrays don't have the same number of elements,
// they obviously are not equivalent.
if(arrayA.length != arrayB.length) {
return false;
}
// Create a copy of each so that anything done to the copies
// doesn't affect the originals.
var arrayACopy:Array = arrayA.concat( );
var arrayBCopy:Array = arrayB.concat( );
// If the order of the elements of the two arrays doesn't
// matter, sort the two copies so the order of the copies
// matches when comparing.
if(bNotOrdered) {
arrayACopy.sort( );
arrayBCopy.sort( );
}
// Loop through each element of the arrays, and compare them.
// If they don't match, delete the copies and return false.
for(var i:int = 0; i < arrayACopy.length; i++) {
if(arrayACopy[i] != arrayBCopy[i]) {
delete arrayACopy;
delete arrayBCopy;
return false;
}
}
// Otherwise the arrays are equivalent.
// So delete the copies and return true.
delete arrayACopy;
delete arrayBCopy;
return true;
}
var members:Object = {a: "A", b: "B", c: "C"};
var members:Object = {};
members.a = "A";
members.b = "B";
members.c = "C";
trace(members.a);
trace(members["a"]);
var members:Object = new Object();
members.a = "A";
members.b = "B";
members.c = "C";
for (var m:String in members) {
trace(members[m]);
}