How to declare an arrayList? And when to use a regular array, VS Array, VS List, VS ArrayList?

Keep getting errors like wasn’t expecting ‘new’, etc.

I read the documentation here:

However when I do (in unityscript):

var someVar: new ArrayList();

it doesnt work.

I also tried:

var someVar: new ArrayList{}; 


private var someVar: new ArrayList(); 


function Start()

var someVar: new ArrayList(); 


var someVar: ArrayList();

While some of these are silly… the question remains… what the heck is happening? I haven’t used these before so bare with me.

Another thing is that it’s really confusing that there’s so many other collections out there (mentioned in the title) - when to use what?


Never use the Array or ArrayList classes as they store objects instead of explicit types, so you’ll need to cast values back and forth so you’ll fall into boxing and unboxing. Either use a regular array, or a generic list. See this. - And don’t freak out when you hear, “LISTS ARE BETTER THAN ARRAYS, YOU SHOULD ALWAYS USE LISTS!!” This drives me crazy. - I see a lot of confusion here when the word ‘array’ is mentioned, people think of Array class when they hear it, not sure why. Regular arrays are faster than generic lists (but no a whole lot)

When to use regular array and when to use a list? - Use a regular array when you know how many elements you need and you don’t expect that number to change (via adding, removing), use it in places where high performance is required. Use a generic list if you need more dynamic acrobatics, like inserting an element at a certain index, removing an element, appending to the end of the list, etc. There’s tons of fun stuff you could do with List.

[regular array]
GameObject[] objects = new GameObject;
objects : GameObject[] = new GameObject;

[generic list]
List<GameObject> objects = new List<GameObject>();
var objects : List.<GameObject> = new List.<GameObject>();

Here’s a nice cheat sheet I found, on the complexity and performance of different data structures performing different operations - Check it out!