Should I use a List or [Array]

Hi guys, I was looking up earlier on how to create a random spawner and I’m stumped as to whether to use List or [Array], was just theory crafting~

So here’s what I needed to do, would be glad for any inputs to my questions.

I have 12 pieces of puzzle and I need to remove X number of them so that the player can try to solve the puzzle. After which, I would loop through the remaining pieces left in the array so that I can spawn them onto the scene as the pieces already played.

I was looking through google earlier and 3 out of 5 questions I’ve encountered, the answer recommended that the poster used instead. So for my case should I use [Array] or List?

You should first learn what the difference between those two collection types are. There’s this verbose wiki article on that topic.

However the main differences between a generic List and a built-in array are:

  • The access of built-in arrays is slightly faster than when using a List. By slightly i mean that when iterating 1 million items each frame will will see a tiny difference.
  • Built-in arrays have a little bit less memory overhead than a generic List. A List needs some extra data like the current item count and the reserve capacity unless Capacity == Count.
  • Built-in arrays have a fix size. So once created you can’t add or remove elements.
  • Generic Lists are just wrapper classes around built-in arrays. However they allow adding and removing of items by holding a seperate count variable which tells the collection how many items in the internal array are used. It automatically creates a new array if the old one hasn’t enough free elements in the old array.

In most cases you can exchange an array with a List. However it should only by used when you actually need any of the features that a List provides. So if you have a collection that is going to change in size, use a List. On the other hand if you have some data that doesn’t change, use an array. For example an array of prefabs. You assign them at edit time and you never add or remove elements at runtime, you just access / read them.

Keep in mind that even a List seems so easy to use, if used wrong it can produce quite a performance hit. For example if you know that you’re going to add a lot of elements to a List, you should initialize the capacity of the list with the end lenght or a good estimate. Each time the internal array runs out of elements, a new array is created which usually is double the size of the initial array. The initial array is usually 8 elements long. If you create a for loop and adding “1025” elements to a new List<EType>() you will create arrays of length 8, 16, 32, 64, 128, 256, 512, 1024, 2048. The final array is almost double the size it has to be. If you initialize the list with a capacity new List<EType>(1025) the internal array will have 1025 elements from the beginning.

Since your description of your problems contains words like “remove” you probably want to use a List instead of an array. However your problem description is pretty thin.