To facilitate this, each of the collection objects
in haXe supplies a method called iterator() . This method returns a copy of an iterable object
containing the items of the collection that can be passed to processes such as a for loop:
var myArray : Array < String > = [???item1???, ???item2???, ???item3???];
var arrIter = myArray.iterator();
for ( i in arrIter )
Chapter 4: Controlling the Flow of Information
83
{
trace( i );
}
This example displays each of the items in the Array. However, there is an easier way to do this. As a
convenience, the for loop happens to be able to extract the iterator object directly from any object
exposing a method iterator() with a return type Iterator < T > . Here, the < T > is a placeholder and is
used to specify the type of value held in the iterator, so in this example, the return type of iterator()
will be Iterator < String > :
var myArray = [???item1???, ???item2???, ???item3???];
for ( i in myArray )
{
trace( i );
}
Direct Accessing of Collection Items
The values stored in the iterator object will pass copies of the values contained in any relative collection
if the type of the value is not an object; otherwise a reference is passed.
Pages:
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197