Arrays

From ETC Public Wiki
Jump to: navigation, search

see: http://robotduck.wordpress.com/2009/11/04/88/

see: http://www.unifycommunity.com/wiki/index.php?title=JavascriptMultiDimArrays

Declare a 2D array

 var a : float[,];   //** Test this one


Built-in Array

Has a fixed size but are very fast

Using arrays that are filled in in the inspector

Declare an array of ints, floats, and strings

 var ai : int[];
 var af : float[];
 var as : String[];

Normal use

 // declaration
 var myArray = new TheType[lengthOfArray];     
 
 // declaration example using ints
 var myNumbers = new int[10];                   
 
 // declaration example using GameObjects
 var enemies = new GameObject[16];              
 
 // get the length of the array
 var howBig = enemies.Length;               
 
 // set a value at position i
 myArray[i] = newValue;                     
 
 // get a value from position i
 var thisValue = myArray[i]
 
 // iterate through the array
   for (var value in values) {
       print(value);
   }

Javascript Arrays

dynamic size and can store any mixed types but slower

 // declaration
 var myArray = new Array();     
 
 // add an item to the end of the array
 myArray.Add(anItem);           
 
 // retrieve an item from position i
 var thisItem = myArray[i];     
 
 // removes an item from position i
 myArray.RemoveAt(i);           
 
 // get the length of the Array
 var howBig = myArray.length;
 
 // iterate through the array
   for (var value : String in arr) {
       print(value);
   }
 
 // create an array with items in it
 var applePie = Array("apple", "brown sugar", "butter", "pie crust");

ArrayLists

dynamic and untyped also slower but some different features

 // declaration
 var myArrayList = new ArrayList();    
 
 // add an item to the end of the array
 myArrayList.Add(anItem);              
 
 // change the value stored at position i
 myArrayList[i] = newValue;            
 
 // retrieve an item from position i
 var thisItem : TheType = myArray[i];  (note the required casting!)
 
 // remove an item from position i
 myArray.RemoveAt(i);                  
 
 // get the length of the array
 var howBig = myArray.Count;