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?
Answer by Eric5h5
Feb 10, 2011 at 05:11 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.)
i'm still not sure why you shouldn't use arraylist - it seems the best way to store instances of gameobjects
@ina: No, because it's slow and not type safe. GameObject is the best way to store instances of gameobjects, assuming you have a fixed number. If not, then List probably is (depending on what you're doing, maybe LinkedList or some other type), but never ArrayList.
Answer by Justin Warner
Feb 10, 2011 at 04:48 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 =).
Thats what I thought. Im still confused on when to use either or? Or does it not matter which one I use?
Answer by Arowx
Mar 03, 2011 at 01:52 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)
This is about Array in JS, which can be resized. Not built-in arrays in Mono. As I said in my answer, Array and ArrayList are basically the same. Probably Array is just a wrapper for ArrayList.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
To help users post good questions and use the site effectively we have posted a user guide. Please check it out.
Answers and Comments
No one has followed this question yet.
ArrayLists giving wrong information? C#
Array sub elements setup question
Populate array on Start() and accessing it on Update()...how to?
Problems with Arrays
Default Null Value for Key Not Found in Dictionary?