21视频教程网3月28日整理
var oneArray:Array = new Array("a", "b", "c");//初值化
var twoArray:Array = oneArray; // 转递数组.
twoArray[0] = "z"; //赋值twoArray[0].
trace(oneArray); // 输出: z,b,c.
package {
import flash.display.Sprite;
public class Array_Array extends Sprite {
public function Array_Array() {
var myArr:Array = new Array();
trace(myArr.length); // 0
}
}
}
package {
import flash.display.Sprite;
public class Array_Array_2 extends Sprite {
public function Array_Array_2() {
var myArr:Array = new Array(5);
trace(myArr.length); // 5
myArr[0] = "one";
myArr.push("six");
trace(myArr); // one,,,,,six
trace(myArr.length); // 6
}
}
}
package {
import flash.display.Sprite;
public class Array_Array_3 extends Sprite {
public function Array_Array_3() {
var myArr:Array = new Array("one", "two", "three");
trace(myArr.length); // 3
trace(myArr); // one,two,three
}
}
}
var numbers:Array = new Array(1, 2, 3);
var letters:Array = new Array("a", "b", "c");
var numbersAndLetters:Array = numbers.concat(letters);
var lettersAndNumbers:Array = letters.concat(numbers);
trace(numbers); // 1,2,3
trace(letters); // a,b,c
trace(numbersAndLetters); // 1,2,3,a,b,c
trace(lettersAndNumbers); // a,b,c,1,2,3
isNumeric函数--检测是否全部为Number
import flash.display.Sprite;
public class Array_every extends Sprite {
public function Array_every() {
var arr1:Array = new Array(1, 2, 4);
var res1:Boolean = arr1.every(isNumeric);
trace("isNumeric:", res1); // true
var arr2:Array = new Array(1, 2, "ham");
var res2:Boolean = arr2.every(isNumeric);
trace("isNumeric:", res2); // false
}
private function isNumeric(element:*, index:int, arr:Array):Boolean {
return (element is Number);
}
}
}
package {
import flash.display.Sprite;
public class Array_filter extends Sprite {
public function Array_filter() {
var employees:Array = new Array();//创建employees数组
employees.push({name:"Employee 1", manager:false});//调用push函数添加二维元素
employees.push({name:"Employee 2", manager:true});
employees.push({name:"Employee 3", manager:false});
trace("Employees:");
employees.forEach(traceEmployee);//调用执行函数traceEmployee(?forEach是啥?看下面)
var managers:Array = employees.filter(isManager);//调用挑选函数isManager
trace("Managers:");
managers.forEach(traceEmployee);//调用执行函数traceEmployee
}
private function isManager(element:*, index:int, arr:Array):Boolean {
return (element.manager == true);//返回判断布尔值
}
private function traceEmployee(element:*, index:int, arr:Array):void {
trace("\t" + element.name + ((element.manager) ? " (manager)" : ""));//判断manager是否为true,如果是输出空格
}
}
}
package {
import flash.display.Sprite;
public class Array_forEach extends Sprite {
public function Array_forEach() {
var employees:Array = new Array();//创建数组
employees.push({name:"Employee 1", manager:false});//调用push添加二维元素
employees.push({name:"Employee 2", manager:true});
employees.push({name:"Employee 3", manager:false});
trace(employees);//输出数组
employees.forEach(traceEmployee);//调用执行函数traceEmployee
}
private function traceEmployee(element:*, index:int, arr:Array):void {
trace(element.name + " (" + element.manager + ")");//输出:name (manager)
}
}
}
package {
import flash.display.Sprite;
public class Array_forEach_2 extends Sprite {
public function Array_forEach_2() {
var employeeXML:XML = <employees>
<employee name="Steven" manager="false" />
<employee name="Bruce" manager="true" />
<employee name="Rob" manager="false" />
</employees>;
var employeesList:XMLList = employeeXML.employee;//读取节点
var employeesArray:Array = new Array();//创建employeesArray数组
for each (var tempXML:XML in employeesList) {
employeesArray.push(tempXML);//调用push函数添加元素
}
employeesArray.sortOn("@name");//?sorton是啥,看下文
employeesArray.forEach(traceEmployee);//调用执行函数traceEmployee
}
private function traceEmployee(element:*, index:Number, arr:Array):void {
trace(element.@name + ((element.@manager == "true") ? " (manager)" : ""));//判断manager是否为true,为真输出name(manager)为否输出name
}
}
}
package {
import flash.display.Sprite;
public class Array_indexOf extends Sprite {
public function Array_indexOf() {
var arr:Array = new Array(123,45,6789);//创建数组arr
arr.push("123-45-6789");//添加数组元素(字符串)
arr.push("987-65-4321");//添加数组元素(字符串)
var index:int = arr.indexOf("123");//查寻字符串"123"
trace(index); // -1
var index2:int = arr.indexOf(123);//查寻123
trace(index2); // 0
var index2:int = arr.indexOf("123-45-6789");//查寻字符串"123-45-6789"
trace(index2); // 0
}
}
}
var myArr:Array = new Array("one", "two", "three");//不要告诉我看不懂...
var myStr:String = myArr.join(" and ");
trace(myArr); // one,two,three
trace(myStr); // one and two and three
var phoneString:String = "(888) 867-5309";
var specialChars:Array = new Array("(", ")", "-", " ");
var myStr:String = phoneString;
var ln:uint = specialChars.length;//定义数组specialChars长度
for(var i:uint; i < ln; i++) {
myStr = myStr.split(specialChars[i]).join("");//分割字符串phoneString并重建数组,再调用join重组字符口串
}
var phoneNumber:Number = new Number(myStr);
trace(phoneString); // (888) 867-5309
trace(phoneNumber); // 8888675309
package {
import flash.display.Sprite;
public class Array_lastIndexOf extends Sprite {
public function Array_lastIndexOf() {
var arr:Array = new Array(123,45,6789,123,984,323,123,32);
var index:int = arr.indexOf(123);
trace(index); // 0
var index2:int = arr.lastIndexOf(123);
trace(index2); // 6
}
}
}
package {
import flash.display.Sprite;
public class Array_map extends Sprite {
public function Array_map() {
var arr:Array = new Array("one", "two", "Three");
trace(arr); // one,two,Three
var upperArr:Array = arr.map(toUpper);//创建upperArr数组并调用执行toUpper函数
trace(upperArr); // ONE,TWO,THREE
}
private function toUpper(element:*, index:int, arr:Array):String {
return String(element).toUpperCase();//小写转大字
}
}
}
var letters:Array = new Array("a", "b", "c");
trace(letters); // a,b,c
var letter:String = letters.pop();
trace(letters); // a,b
trace(letter); // c
11.push 方法
var letters:Array = new Array();
letters.push("a");
letters.push("b");
letters.push("c");
trace(letters.toString()); // a,b,c
var letters:Array = new Array("a");
var count:uint = letters.push("b", "c");
trace(letters); // a,b,c
trace(count); // 3
var letters:Array = new Array("a", "b", "c");
trace(letters); // a,b,c
letters.reverse();
trace(letters); // c,b,a
var letters:Array = new Array("a", "b", "c");
var firstLetter:String = letters.shift();
trace(letters); // b,c
trace(firstLetter); // a
var letters:Array = new Array("a", "b", "c", "d", "e", "f");
var someLetters:Array = letters.slice(1,3);
trace(letters); // a,b,c,d,e,f
trace(someLetters); // b,c
var letters:Array = new Array("a", "b", "c", "d", "e", "f");
var someLetters:Array = letters.slice(2);
trace(letters); // a,b,c,d,e,f
trace(someLetters); // c,d,e,f
var letters:Array = new Array("a", "b", "c", "d", "e", "f");
var someLetters:Array = letters.slice(-2);
trace(letters); // a,b,c,d,e,f
trace(someLetters); // e,f
package {
import flash.display.Sprite;
public class Array_some extends Sprite {
public function Array_some() {
var arr:Array = new Array();
arr[0] = "one";
arr[1] = "two";
arr[3] = "four";
var isUndef:Boolean = arr.some(isUndefined);//调用执行函数isUndefined
if (isUndef) {
trace("array contains undefined values: " + arr);
} else {
trace("array contains no undefined values.");
}
}
private function isUndefined(element:*, index:int, arr:Array):Boolean {
return (element == undefined);
}
}
}
var vegetables:Array = new Array("spinach",
"green pepper",
"cilantro",
"onion",
"avocado");
trace(vegetables); // spinach,green pepper,cilantro,onion,avocado
vegetables.sort();
trace(vegetables); // avocado,cilantro,green pepper,onion,spinach
var vegetables:Array = new Array("spinach",
"green pepper",
"Cilantro",
"Onion",
"Avocado");
vegetables.sort();
trace(vegetables); // Avocado,Cilantro,Onion,green pepper,spinach
vegetables.sort(Array.CASEINSENSITIVE);
trace(vegetables); // Avocado,Cilantro,green pepper,Onion,spinach
var vegetables:Array = new Array();
vegetables.push(new Vegetable("lettuce", 1.49));
vegetables.push(new Vegetable("spinach", 1.89));
vegetables.push(new Vegetable("asparagus", 3.99));
vegetables.push(new Vegetable("celery", 1.29));
vegetables.push(new Vegetable("squash", 1.44));
trace(vegetables);
// lettuce:1.49, spinach:1.89, asparagus:3.99, celery:1.29, squash:1.44
vegetables.sort();
trace(vegetables);
// asparagus:3.99, celery:1.29, lettuce:1.49, spinach:1.89, squash:1.44
//The following code defines the Vegetable class
class Vegetable {
private var name:String;
private var price:Number;
public function Vegetable(name:String, price:Number) {
this.name = name;
this.price = price;
}
public function toString():String {
return " " + name + ":" + price;
}
}
var vegetables:Array = new Array();
vegetables.push(new Vegetable("lettuce", 1.49));
vegetables.push(new Vegetable("spinach", 1.89));
vegetables.push(new Vegetable("asparagus", 3.99));
vegetables.push(new Vegetable("celery", 1.29));
vegetables.push(new Vegetable("squash", 1.44));
trace(vegetables);
// lettuce:1.49, spinach:1.89, asparagus:3.99, celery:1.29, squash:1.44
vegetables.sort(sortOnPrice);
trace(vegetables);
// celery:1.29, squash:1.44, lettuce:1.49, spinach:1.89, asparagus:3.99
function sortOnPrice(a:Vegetable, b:Vegetable):Number {
var aPrice:Number = a.getPrice();
var bPrice:Number = b.getPrice();
if(aPrice > bPrice) {
return 1;
} else if(aPrice < bPrice) {
return -1;
} else {
//aPrice == bPrice
return 0;
}
}
// The following code defines the Vegetable class and should be in a separate package.
class Vegetable {
private var name:String;
private var price:Number;
public function Vegetable(name:String, price:Number) {
this.name = name;
this.price = price;
}
public function getPrice():Number {
return price;
}
public function toString():String {
return " " + name + ":" + price;
}
}
var vegetables:Array = new Array();
vegetables.push(new Vegetable("lettuce", 1.49));
vegetables.push(new Vegetable("spinach", 1.89));
vegetables.push(new Vegetable("asparagus", 3.99));
vegetables.push(new Vegetable("celery", 1.29));
vegetables.push(new Vegetable("squash", 1.44));
trace(vegetables);
// lettuce:1.49, spinach:1.89, asparagus:3.99, celery:1.29, squash:1.44
vegetables.sortOn("name");
trace(vegetables);
// asparagus:3.99, celery:1.29, lettuce:1.49, spinach:1.89, squash:1.44
vegetables.sortOn("price", Array.NUMERIC | Array.DESCENDING);
trace(vegetables);
// asparagus:3.99, spinach:1.89, lettuce:1.49, squash:1.44, celery:1.29
class Vegetable {
public var name:String;
public var price:Number;
public function Vegetable(name:String, price:Number) {
this.name = name;
this.price = price;
}
public function toString():String {
return " " + name + ":" + price;
}
}
var records:Array = new Array();
records.push({name:"john", city:"omaha", zip:68144});
records.push({name:"john", city:"kansas city", zip:72345});
records.push({name:"bob", city:"omaha", zip:94010});
for(var i:uint = 0; i < records.length; i++) {
trace(records[i].name + ", " + records[i].city);
}
// Results:
// john, omaha
// john, kansas city
// bob, omaha
trace("records.sortOn('name', 'city');");
records.sortOn(["name", "city"]);
for(var i:uint = 0; i < records.length; i++) {
trace(records[i].name + ", " + records[i].city);
}
// Results:
// bob, omaha
// john, kansas city
// john, omaha
trace("records.sortOn('city', 'name');");
records.sortOn(["city", "name"]);
for(var i:uint = 0; i < records.length; i++) {
trace(records[i].name + ", " + records[i].city);
}
// Results:
// john, kansas city
// bob, omaha
// john, omaha
class User {
public var name:String;
public var age:Number;
public function User(name:String, age:uint) {
this.name = name;
this.age = age;
}
public function toString():String {
return this.name + ":" + this.age;
}
}
var users:Array = new Array();
users.push(new User("Bob", 3));
users.push(new User("barb", 35));
users.push(new User("abcd", 3));
users.push(new User("catchy", 4));
trace(users); // Bob:3,barb:35,abcd:3,catchy:4
users.sortOn("name");
trace(users); // Bob:3,abcd:3,barb:35,catchy:4
users.sortOn("name", Array.CASEINSENSITIVE);
trace(users); // abcd:3,barb:35,Bob:3,catchy:4
users.sortOn("name", Array.CASEINSENSITIVE | Array.DESCENDING);
trace(users); // catchy:4,Bob:3,barb:35,abcd:3
users.sortOn("age");
trace(users); // abcd:3,Bob:3,barb:35,catchy:4
users.sortOn("age", Array.NUMERIC);
trace(users); // Bob:3,abcd:3,catchy:4,barb:35
users.sortOn("age", Array.DESCENDING | Array.NUMERIC);
trace(users); // barb:35,catchy:4,Bob:3,abcd:3
var indices:Array = users.sortOn("age", Array.NUMERIC | Array.RETURNINDEXEDARRAY);
var index:uint;
for(var i:uint = 0; i < indices.length; i++) {
index = indices[i];
trace(users[index].name, ": " + users[index].age);
}
// Results:
// Bob : 3
// abcd : 3
// catchy : 4
// barb : 35
var vegetables:Array = new Array("spinach",
"green pepper",
"cilantro",
"onion",
"avocado");
var spliced:Array = vegetables.splice(2, 2);
trace(vegetables); // spinach,green pepper,avocado
trace(spliced); // cilantro,onion
vegetables.splice(1, 0, spliced);
trace(vegetables); // spinach,cilantro,onion,green pepper,avocado
var vegetables:Array = new Array();
vegetables.push(1);
vegetables.push(2);
vegetables.push(3);
vegetables.push(4);
vegetables.push(5);
var vegnums:String = vegetables.toString();
trace(vegnums+",6");
// 1,2,3,4,5,6
var names:Array = new Array();
names.push("Bill");
names.push("Jeff");
trace(names); // Bill,Jeff
names.unshift("Alfred");
names.unshift("Kyle");
trace(names); // Kyle,Alfred,Bill,Jeff
Flash教程
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |