Check if array key exists

Hi everyone!

Let’s say I have an array like this:

var myArray:Array<Int>;
myArray = new Array<Int>();
myArray[3] = 4;

First of all, is this ok? Am I allowed to fill in random positions in a simple array such as this, while leaving some undefined?

If so, how can I check if a key exists? I’ve tried this:

if (myArray[7] == null) { ... }

and this:

if (myArray.exists(7)) { ... }

and it doesn’t work. Any ideas? :slight_smile:

I would recommend Map for key-based access :slight_smile:

var map = new Map<Int, Int> ();
map[3] = 4;

if (map.exists (3)) {
    trace (map[3]);
}
1 Like

Cool, thanks! You’re super-super fast :smile:

1 Like