How to define an array with one element in it?

I am trying to define a single element array ... so that

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 AS1.0 notation.. 

So, is there any AS3.0 way to define an array with one element?

+4
source share
4 answers

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

Why? This is a completely legal initialization of an array of shorthand strings, and this is exactly how to do it.

+10
source

Lol, I remember that I did this a year or 2 ago, as I did, there were 2 lines.

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

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

+5
source

weltraumpirat is only right that the code will be compiled, this is still actioncript 1/2 (AVM1). You said you want to know the “AS3 method” .... and one significant difference and advantage of AS3 (AVM2) over AS1 / AS2 (AVM1) is strict typing. Therefore, creating a Vector object, the so-called strictly typed array (and this is faster because of this strict typing). Here is the correct way to initialize a typed array with 1 or more defined objects:

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

More details here:

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

Edit
For all 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 that execute 20% or more faster than not typed in each scenario.

For people who REALLY don't know what they are talking about, Tamarin is a Flash virtual machine (at least an open source component, a kernel minus the user interface and other things).

Change ... again .. sigh
For people who don’t understand what “context” is ... when I say that the FASTER vector ... I’m talking about the overall performance of an object in a virtual machine. This is not my own statement, it comes from adobe itself, and in my answer (or rather, in the link) there are landmarks from the evangelist of the flash platform. Perhaps people who argue with me do not have English as their first language .....

+1
source
 var myArray:Array = new Array(); myArray.push(1); trace(myArray[0]); //1 
0
source

Source: https://habr.com/ru/post/1385975/


All Articles