+ Reply to Thread
Results 1 to 3 of 3

Thread: Flash: Arrays and Vectors

  1. #1
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,035

    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. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,751
    Blog Entries
    97

    Re: Flash: Arrays and Vectors

    Very well done, +rep!

  3. #3
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    37
    Posts
    12,912
    Blog Entries
    57

    Re: Flash: Arrays and Vectors

    Good stuff. +rep
    CodeCall Blog | CodeCall Wiki
    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. On Learning the STL: Chapter 1 (Vectors) - Part 1
    By ZekeDragon in forum C Tutorials
    Replies: 12
    Last Post: 08-30-2009, 04:08 AM
  2. Array Sorting Algorithms I
    By whitey6993 in forum C Tutorials
    Replies: 2
    Last Post: 12-30-2008, 09:24 AM
  3. Help with Arrays and Vectors!!!
    By PrettyVacant in forum C and C++
    Replies: 2
    Last Post: 11-29-2007, 07:56 AM