Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Mar 24, 2016 at 01:15 AM by Jason_ioosh for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Jason_ioosh · Mar 23, 2016 at 01:02 PM · arraylist

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
5
Best Answer

Answer by Bunny83 · Mar 23, 2016 at 01:39 PM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jason_ioosh · Mar 24, 2016 at 01:15 AM 0
Share

Hi Bunny83,

Thank you for taking the time to reply to the questions, my problem description is pretty thin as I do not want to bog down the question with too many information, as it would have combined a few different questions into this one post.

Prior to reading this, I've already decided to use Arrays as the same reason you gave:

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

What I'm currently doing is only using a list of 12 prefabs assigned during Edit-time, so Arrays work very well for me. $$anonymous$$y initial problem has been trying to remove an element from the Array, but I overcome that problem by just skipping through the elements which I do not want to use in a for Loop.

Once again, thank you for your reply, it has helped me in my knowledge.

Regards, Jason

PS: Question has been closed

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

How can I make a GameObject face a GameObject with a specific Tag? 1 Answer

Why does List .ToArray throw ArgumentException? 1 Answer

How do you emulate array/List Inspector behavior with an Editor window? 0 Answers

Reference all objects inside of an Array or List? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges