Im relatively new to programming, especially in UnityScript. I am confused on when to use the Array function and the ArrayList function. I am not even sure of what an ArryList is. I do know that an Array is a container in which variables can be placed, with the first variable having position 0. When should i use an Array and when should i use an ArrayList?
asked Feb 10, 2011 at 04:32 AM
You shouldn't use either Array or ArrayList; they're basically the same thing. You should use fixed-length built-in arrays (int, float, etc.) where possible, but if you need to be able to change the length of the array (insert/delete elements, etc.), use generic Lists. The one thing Array/ArrayList can do that the other kinds of arrays can't is store multiple different types of objects in the same array. But you almost never actually need to do that, and the performance cost of doing this is relatively high, so stick with built-in arrays and Lists as appropriate. (In the same vein, don't use Hashtable, use Dictionary instead, for basically the same reason.)
answered Feb 10, 2011 at 05:11 AM
Um, ArrayList is more adaptable... Like, you can add and remove objects with ease through an ArrayList, while an array, once a size, is always that size...
Also, people around here use List more so than ArrayList (I personally have always used ArrayList in school).
www.Google.com can help you further about what the differences are =).
answered Feb 10, 2011 at 04:48 AM
ArrayList can be resized and therefore is slower, Arrays can't be resized* but are very fast.
Choosing your container depends on how you use it and what you use it for..
Stack/Queue - When you just need the next item to work with one at a time List - Slow to add to but good for looping through Array - Get fast access to items at an index Hashmap - Use unique keys to get items
So the answer is it depends on what and how you intend to use the container...
PS Using the right container for the right job can make your code faster and easier! * You can make a new larger one but if you need to keep doing this your probably using the wrong container ;o)
answered Mar 03, 2011 at 01:52 AM