Very well done, +rep!
Thread: Flash: Arrays and Vectors |
Flash Arrays and Vectors
Both arrays and vectors are structures that provide fast 0(1) access to any item in the array. They are direct-access containers. If you are familiar with arrays in other languages they are a bit different in ActionScript.
In C++, we create an array that is a fixed length structure. It also has a type. So we create an array of say size 5 to hold ints. It can only hold integers. A course in data structures shows that an array has serious problems where the programmer doesn't know the size of the data available. This leads to the vector class in the STL.
ActionScript doesn't introduce these limitations. The array is not a typed structure. It also is not limited by a size. This introduces problems when we constantly re-size the array. As of this writing, there is no linked structures in ActionScript. If you need a linked list, you will have to implement it yourself. I can imagine that in a future release, ActionScript will have some equivalent of the STL.
Using arrays
Creating an array
This creates an empty array. It can hold any type of data you want. Integers, floats, strings, anything.Code:var arnNums:Array = new Array();
To add an item to the end of the array, we use the push function.
The array is now this:Code:arnNums.push(5);
Using push again allows us to see that it adds an item to the end. We will this time add a string.5
The array is now:Code:arnNums.push("j");
Removing an item is as simple as calling the pop method.5 j
Example:
This removes an item from the end of the array.Code:arnNums.pop();
The array is now:
Outputting an array5
There are two ways to output the array to the output window. One is to trace the entire array. This prints the array out item by item separated by commas.
Example:
Output:Code:trace(arnNums);
The other way is to use a loop to output individual items.5, 3
Example:
Output:Code:for (var i=0;i<arnNums.length;i++) { trace(arnNums[i]); }
Sorting5
3
This is as simple as calling the arrays sort method.
Example:
The array is now:Code:arnNums.sort(); trace(arnNums);
Using Vectors3, 5
The only difference between arrays and vectors, is that the vector is a typed array. This is done with the concepts of generics. This is the only generic class that exists so far, and you can't make your own generic classes yet. I imagine that this will change in a future release. I don't imagine ActionScript becoming like C++ but more like Java. At least syntactically, it won't let you do things that you can in C++. Meaning, no pointers, and other things like that. Flash will still remain easy to use for animations (where as Java is not). This language is more like JavaScript syntactically but I can imagine it becoming more like Java.
I am just going to show you how to create a vector. All the methods work in the exact same way as the array.
Creating a Vector
The things in the angle brackets you just have to change for the type of data that you want.Code:var vNums:Vector.<int> = new Vector.<int>();
Some more vectors:
The last example is assuming that a People class exists. This class works the exact same as the array. Why? Simple, it is one. Just the compiler ensures that you add data of a defined type to the structure. If you try to add data of a different type, a default value of the predefined type is added.Code:var vWords:Vector.<String> = new Vector.<String>(); var vPeople:Vector.<People> = new Vector.<People>();
These vectors are very useful in drawing complicated objects.
Very well done, +rep!
Good stuff. +rep
CodeCall Blog | CodeCall Wiki
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
There are currently 1 users browsing this thread. (0 members and 1 guests)