Unity3D – Generic List – JavaScript

The Generic List (also known as List) is similar to the JS Array and the ArrayList, the significant difference with the Generic List, is that you explicitly specify the type to be used when you declare it – in this case, the type of object that the List will contain.

Once you’ve declared it, you can only add objects of the correct type – and because of this restriction, you get two significant benefits:
– no need to do any type casting of the values when you come to retrieve them.
– performs significantly faster than ArrayList (5x faster)

Sample code:


#pragma strict

// you need to add a line at the top of any script in which you want to use Generic List
import System.Collections.Generic;

var someNumbers = new List.<int>();            // a real-world example of declaring a List of 'ints'
var enemies = new List.<GameObject>();         // a real-world example of declaring a List of 'GameObjects'
var vtcs = new List.<Vector3>(mesh.vertices);  // convert an array to a list

function Start(){

	myList.Add(theItem);                   // add an item to the end of the List
	myList[i] = newItem;                   // change the value in the List at position i
	var thisItem = List[i];                // retrieve the item at position i
	myList.RemoveAt(i);                    // remove the item from position i
	
	numList = nums.ToList();               // convert an array to list
	
}// END Start()

function Update(){
}// END Update()

Original article: http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use?