+ Reply to Thread
Results 1 to 3 of 3

Thread: Flash: Arrays and Vectors

  1. #1
    Join Date
    Mar 2008
    Posts
    7,140
    Rep Power
    86

    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

    Code:
    var arnNums:Array = new Array();
    This creates an empty array. It can hold any type of data you want. Integers, floats, strings, anything.

    To add an item to the end of the array, we use the push function.

    Code:
    arnNums.push(5);
    The array is now this:

    5
    Using push again allows us to see that it adds an item to the end. We will this time add a string.

    Code:
    arnNums.push("j");
    The array is now:

    5 j
    Removing an item is as simple as calling the pop method.

    Example:

    Code:
    arnNums.pop();
    This removes an item from the end of the array.

    The array is now:

    5
    Outputting an array

    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:

    Code:
    trace(arnNums);
    Output:

    5, 3
    The other way is to use a loop to output individual items.

    Example:

    Code:
    for (var i=0;i<arnNums.length;i++) {
             trace(arnNums[i]);
    }
    Output:

    5
    3
    Sorting

    This is as simple as calling the arrays sort method.

    Example:

    Code:
    arnNums.sort();
    trace(arnNums);
    The array is now:

    3, 5
    Using Vectors

    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

    Code:
    var vNums:Vector.<int> = new Vector.<int>();
    The things in the angle brackets you just have to change for the type of data that you want.

    Some more vectors:

    Code:
    var vWords:Vector.<String> = new Vector.<String>();
    var vPeople:Vector.<People> = new Vector.<People>();
    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.

    These vectors are very useful in drawing complicated objects.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Flash: Arrays and Vectors

    Very well done, +rep!

  4. #3
    Join Date
    Jul 2006
    Posts
    16,448
    Blog Entries
    74
    Rep Power
    143

    Re: Flash: Arrays and Vectors

    Good stuff. +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Vectors
    By vasil_9x in forum C and C++
    Replies: 1
    Last Post: 03-07-2011, 11:18 AM
  2. Replies: 4
    Last Post: 10-27-2010, 10:15 PM
  3. C++ Vectors
    By Hunter100 in forum C and C++
    Replies: 8
    Last Post: 06-27-2010, 09:50 PM
  4. Help with Arrays and Vectors!!!
    By PrettyVacant in forum C and C++
    Replies: 2
    Last Post: 11-29-2007, 07:56 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts