x


Array vs ArrayList

Hey guys,

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?

more ▼

asked Feb 10, 2011 at 04:32 AM

Zach Meyers gravatar image

Zach Meyers
11 4 4 7

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.)

more ▼

answered Feb 10, 2011 at 05:11 AM

Eric5h5 gravatar image

Eric5h5
111k 55 189 673

i'm still not sure why you shouldn't use arraylist - it seems the best way to store instances of gameobjects

May 08, 2011 at 05:16 AM ina

@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.

May 08, 2011 at 05:23 AM Eric5h5
(comments are locked)
10|3000 characters needed characters left

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 =).

more ▼

answered Feb 10, 2011 at 04:48 AM

Justin Warner gravatar image

Justin Warner
6.7k 45 49 95

Thats what I thought. Im still confused on when to use either or? Or does it not matter which one I use?

Feb 10, 2011 at 05:01 AM Zach Meyers
(comments are locked)
10|3000 characters needed characters left

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..

Generally

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)

more ▼

answered Mar 03, 2011 at 01:52 AM

Arowx gravatar image

Arowx
2 9 6 8

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.

Mar 03, 2011 at 02:11 AM Eric5h5
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2235
x115

asked: Feb 10, 2011 at 04:32 AM

Seen: 11210 times

Last Updated: Feb 10, 2011 at 04:32 AM