Prev | Current Page 108 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

The T type is explained fully later in the book.
Adding and Removing Items from an Array
There are numerous ways of adding and removing items from an Array. Each method provides a way of
manipulating the Array from a specific point: either at the beginning, the end, or somewhere in the
middle of the Array. If you know the location of the item you want to access, you can specify that
location directly and modify it as you would any variable. You do this using the index operators [ and ] :
myArray[3] = 44;
var myInt = myArray[3];
Chapter 3: Learning the Basics
43
Here, the index location 3, which is the fourth item in the Array, is set to the value 44, and then that same
value is passed to the variable myInt . Of course, a problem arises if the fourth item in myArray already
contained a value that you didn ??™ t want overwritten. In this case, you could use the length property of
the Array, which returns the number of items in the Array:
var numItemsInArray : Int = myArray.length;
myArray[numItemsInArray] = 44;
As the items in the Array start at the location 0, the length of the Array is the same value as the next
available space in the Array.


Pages:
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120