1
votes

Does flex allow empty, or missing, keys within an array collection? For example, would the following code be okay:

var myAC:ArrayCollection = new ArrayCollection;
myAC.addItemAt("hi", 0);
myAC.addItemAt("hola", 4);
myAC.addItemAt("bye", 17);
myAC.addItemAt("adios", 32);

Here's why I ask. My application is being fed an XML list full of student names. Each student has a unique ID number. These ID numbers are not always going to be sequential. I have a function where you pass in the students ID number, and you are return their name. With hundreds of students in the list, a simple for each loop that sees if the passed in ID matches the one currently being looked at while looping through the array collection is wasteful. I would like to populate the array collection and have the students ID be the key. This way, i can do the following to return the name:

return myAC.getItemAt(sID);

Is this possible? I thought it would be smarter to ask first and then try it out...

2
@mrk You should undelete your answer to this question; as I believe it is the correct solution. The comment added to your solution stated incorrect information; mainly that your suggested approach would not work. It does and was very similar to my own answer. - JeffryHouser

2 Answers

3
votes

Does flex allow empty, or missing, keys within an array collection?

In Flex, an Array or ArrayCollection do not use keys. Arrays by their very nature are index based. Can you have an item in the array that is null or some other 'inactive' value'? Yes, I don't see why not.

If you want to access elements via a key, you should use a generic Object, or a Dictionary. But, you lose any sort of ordering provided by the array if you do that. Your code might be rewritten like this to use a Dictionary:

var myAC:Dictionary= new Dictionary();
myAC["0"] = "hi";
myAC["4"] = "hola";
myAC["17"] = "bye";
myAC["32"] = "adios";

return myAC[sID];

In the comments it was mentioned that the above code would b/c the key for the dictionary can't use numbers. However, here is a full sample demonstrating that is not the case:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
            public var dict : Dictionary;

            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {
                dict = new Dictionary();
                dict["0"] = "hi";
                dict["4"] = "hola";
                dict["17"] = "bye";
                dict["32"] = "adios";


                trace(dict["0"]);
                trace(dict["4"]);
                trace(dict["17"]);
                trace(dict["32"]);


            var obj : Object = new Object();
            obj["0"] = "hi";
            obj["4"] = "hola";
            obj["17"] = "bye";
            obj["32"] = "adios";

            trace(obj["0"]);
            trace(obj["4"]);
            trace(obj["17"]);
            trace(obj["32"]);


            }

        ]]>
    </fx:Script>
</s:Application>

If you run the sample ind debug mode, you can see that the dictionary vales are set and traced out.

2
votes

Is there a reason you need it to be in an array if you're not doing looping or sequential lookup? Would be much easier to use a dictionary:

var dict:Dictionary = new Dictionary();
dict[studentId] = studentObject;