HaxeDoc2

(やまだぃちぅ) #1

10.2.2 Vector


AVectoris an optimized fixed-lengthcollectionof elements. Much like Array (10.2.1), it has one
type parameter (3.2) and all elements of a vector must be of the specified type, it can beiterated
overusing a for loop (5.13) and accessed using array access syntax (2.8.3). However, unlikeArray
andList, vector length is specified on creation and cannot be changed later.
1 class Main {
2 static function main() {
3 var vec = new haxe.ds.Vector(10);
4
5 for (i in 0...vec.length){
6 vec[i]=i;
7 }
8
9 trace(vec[0]); // 0
10 trace(vec[5]); // 5
11 trace(vec[9]); // 9
12 }
13 }


haxe.ds.Vectoris implemented as an abstract type (2.8) over a native array implementa-
tion for given target and can be faster for fixed-size collections, because the memory for storing
its elements is pre-allocated.

10.2.3 List


AListis acollectionfor storing elements. On the surface, a list is similar to anArray(Sec-
tion 10.2.1). However, the underlying implementation is very different. This results in several
functional differences:
1.A list can not be indexed using square brackets, i.e.[0].

2.A list can not be initialized.
3.There are no list comprehensions.

4.A list can freely modify/add/remove elements while iterating over them.
See theList APIfor details about the list methods. A simple example for working with lists:
1 class ListExample {
2 static public function main() {
3 var myList = new List<Int>();
4 for (ii in 0...5)
5 myList.add(ii);
6 trace(myList); //{0, 1, 2, 3,4}
7 }
8 }

10.2.4 GenericStack


AGenericStack, likeArrayandListis a container for storing elements. It has one type
parameter (3.2) and all elements of the stack must be of the specified type. See theGenericStack
APIfor details about its methods. Here is a small example program for initializing and working
with aGenericStack.
Free download pdf