tailieunhanh - Creating Arrays

Nếu Kelly cho bạn biết cô ấy không thể làm cho nó vào bên của bạn, ngoài việc bị khó chịu rằng chỉ có ba người sẽ được tham dự, bạn sẽ phải thay đổi tên của các biến dưới tên của mình và chuyển chúng lên danh sách của bạn. Bạn có thể đơn giản hóa công việc tẻ nhạt này, cũng như nhiều mục khác. | Creating Arrays Suppose you have a guest list for a party You can store the information in ActionScript with variables like these var name1 String John Smith var name2 String Kelly McAvoy var name3 String Joyce Rendir var name4 String Tripp Carter If Kelly tells you she can t make it to your party aside from being upset that only three people will be attending you ll have to rename the variables beneath her name and shift them up your list. You can simplify this tedious task as well as many other similar data storage and manipulation chores by using arrays. Think of arrays as supervariables While a regular variable can only contain a single value an array can contain multiple values which means you could store that entire guest list in a single array. However you must create an array in order to use it. Because arrays are objects instances of the Array class you use the Array class constructor method to create them. Here s the syntax to create an array var myArray Array new Array You can populate an array with values separated by commas when you create it like this var guestList Array new Array John Smith Kelly McAvoy Joyce Rendir Tripp Carter Or you can use this syntax var guestList Array John Smith Kelly McAvoy Joyce Rendir Tripp Carter Each value in an array is identified by an index number 0 1 2 and so on that denotes its position in the array. In the array we just created John Smith has an index number of 0 Kelly McAvoy has an index number of 1 and so on. To access a value in an array you would use this syntax var myFavoriteGuest String guestList 1 Here the variable myFavoriteGuest is assigned a value of Kelly McAvoy because this is the value that exists at index position 1 in our guestList array. The guestList array was created with four elements. You can add or modify elements at any time by referencing the specific element in the array. For example this script will update the value at index 2 from Joyce Rendir to John Rendir guestList 2 John Rendir An array