4

I am trying to define an array with single element... so,

var arr:Array = new Array(1,2,3,4) // arr[0] = 1 
 // but
var arr:Array = new Array(1) // arr[0] = undefined 

//Also, 

var arr:Array = new Array([1]) // arr[0] = 1 , << ILLUSION
//Because, arr[0] is NOT A NUMBER, IT ITSELF IS OF TYPE=> ARRAY. 

var arr:Array = [1] //arr[0]=1 but i think it's AS1.0 notation..

So, is their any AS3.0 way of defining array with single element ?

ToddBFisher
  • 11,370
  • 8
  • 38
  • 54
Vishwas
  • 1,533
  • 2
  • 19
  • 40
  • It's good that some things in a programming language remain the same from version to version. `var arr:Array = [1];` is perfectly fine.. why would you want a special AS3 notation? – bummzack Dec 12 '11 at 21:38
  • many things from AS1.0 and 2.0 are still used in AS3.0, for probably because of backward compatibility. But they have also provided faster alternatives in AS3.0, so just wanted to check out. – Vishwas Dec 13 '11 at 07:20

4 Answers4

10

var arr:Array = [1]; //arr[0]=1 but i think it's AS1.0 notation..

Why? This is perfectly legal shorthand array initialization, and it's exactly the way to do it.

weltraumpirat
  • 22,544
  • 5
  • 40
  • 54
  • 1
    Although you are right, using terms like "legal" when it comes to actionscript... I think doesn't carry much weight. I mean cmon... it's actionscript lol –  Dec 12 '11 at 21:31
  • 2
    Not that it matters much, but down voting without explanation is bad practice. – weltraumpirat Dec 12 '11 at 22:24
5

Lol, I remember dealing with this a year or 2 back, the way I did it was with 2 lines.

var arr:Array = new Array();
arr[0] = "the element";

This is because the constructor for Array accepts the size of the array as an argument if you are passing a single integer value. http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html#Array()

ToddBFisher
  • 11,370
  • 8
  • 38
  • 54
1

weltraumpirat is right only in that the code will compile, it's still actionscript 1/2 notation (AVM1). You said you want to know the "AS3 way".... and one major difference and benefit of AS3 (AVM2) over AS1/AS2 (AVM1) is strict typing. Hence the creation of the Vector object, aka a strictly typed array (and it's faster because of that strict typing). Here is the proper way to initialize a typed array with 1 or more defined objects:

var vector:Vector.<String> = Vector.<String>(["v1", "v2", "v3"]);

See more here:

http://www.daveoncode.com/2009/04/06/actionscript-vector-class-initialization-with-a-source-array/

Edit
For all the people who don't know what they're talking about:

http://www.mikechambers.com/blog/2008/09/24/actioscript-3-vector-array-performance-comparison/

Simple test, vector == 40% faster than array

http://www.masonchang.com/blog/2011/4/21/tamarin-on-llvm-more-numbers.html

Summary of tamarin JIT tests, typed variables performing 20% or more faster than un-typed in every scenario.

For the people who REALLY don't know what they're talking about, Tamarin IS the flash virtual machine (at least the open source component, the core minus the UI and other things).

Edit... again.. sigh
For people who do not understand what "context" is... when I say that the vector is FASTER... I'm speaking of the overall performance of the object in the virtual machine. This is not my own claim, it comes from adobe themselves and there are benchmarks from a flash platform evangelist included in my answer ( or rather a link to ). Maybe the people who are arguing with me don't have English as their first language.....

  • 1
    By the way, there is even faster way to initialize Vector with elements: `var vec:Vector. = new ["123", "456"]`. – skozin Dec 12 '11 at 21:57
  • 1
    it might not be faster, but using Vectors for typed arrays is very much a best practice. – Chunky Chunk Dec 12 '11 at 22:11
  • @weltraumpirat it's not about the length of the vector. The fact is, that strongly typed variables perform faster that untyped. It's a basic understanding in software development. Just google "untyped vs typed performance" you'll find enough articles to read to last you a lifetime. And when you're working in the flash runtime, which performs (when compiled with the actionscript compiler not LLVM) at a maximum of 1% native C code, every optimization you can do is not a matter of debate, it's a must. –  Dec 12 '11 at 22:20
  • 1
    I've updated my answer to educate all of the people who are arguing without knowing what they're talking about. –  Dec 12 '11 at 22:28
  • @weltraumpirat I don't believe I did "overshoot", he asked how to initialize an array the AS3 way. I told him. Vectors perform better, period. And you're mistaken about the initialization overhead. It's WORSE with untyped variables, because you're forcing the VM do to runtime type checking on both the container and every single one of it's elements. That's a huge overhead. When you specify the type explicitly the VM doesn't need to do any of this, which is why typed variables always perform faster. It's also why AS3 (AVM2) performs exponentially faster than AS1/AS2 (AVM1). –  Dec 12 '11 at 22:42
  • Again you're wrong and seem to not understand the vector class. The VM is not going to type check the array in the vectors constructor. It expects it to be of the type specified by the template type argument in the vector. If it is not, a compile time exception should be generated and even if not, a runtime one will be. And who cares if there is no sortOn method. You can either A) stop being a baby write your own sorting algorithm, or B) use vector.toArray().sortOn() and re-cast to a vector (the result) with minimal overhead, because sortOn is a native method it's uber fast. –  Dec 12 '11 at 23:01
  • If you wanna talk more create a chat room and invite me. I don't want the site getting polluted here with you arguing with me. It's quite obvious you must be upset that I pointed out a better solution or criticized yours, because even staring a whole list of facts in the face you keep saying "I know BUT..." and I haven't received so much as a single upvote from you. If you want to keep arguing in vain with me, lets do it in a chat room. –  Dec 12 '11 at 23:04
  • @weltraumpirat no my statement doesn't make your point. calling toArray to use sorton will do nothing. The array is already initialized and strictly typed, it isn't being re-created at toArray so there is no additional overhead and you still get the advantage of the native speed of sortOn. Further, do you really think the individual is creating and ENTIRE ARRAY in order to store 1 element? No, that's logically incoherent to think that way. He wants to INITIALIZE it with 1 element. He obviously is creating a COLLECTION (array) in order to store a COLLECTION of items, not 1. –  Dec 13 '11 at 01:55
  • Woops, I made a mistake, there is no toArray method built in but creating one is trivial. You busted me! Now to address your question why the initialization takes longer with parameters than it does without parameters........... are you serious? –  Dec 13 '11 at 04:39
  • I'm a little confused I think I got it wrong in my last comment. Are you saying that passing an existing array into the constructor takes more time than pushing back the typed value into an existing vector without anything passed to the constructor? If that is the case my question still stands........ are you serious? Lets take it up in a chat room. –  Dec 13 '11 at 04:41
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5793/discussion-between-ascension-systems-and-weltraumpirat) –  Dec 13 '11 at 04:41
0
var myArray:Array = new Array();
myArray.push(1);

trace(myArray[0]); //1
Chunky Chunk
  • 16,553
  • 15
  • 84
  • 162