Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
3
Question by m4s4m0r1 · Aug 03, 2013 at 05:10 AM · arraylist

How to Create a list with initial capacity

As a Tittle, How to Create a list with initial capacity? i make something simple like this

 using System.Collection.Generic;
 
 public List<GameObject> ObjectList = new List<GameObject>();
 public List<float> ObjectValue;
 void Start()
 {
    GameObject[] something = GameObject.FindGameObjectsWithTag("something");
    foreach(GameObject GO in something)
    {
       ObjectList.Add(GO);
    }
    ObjectValue = new List<float>(ObjectList.Count);
 }

but "ObjectValue" doesn`t have any Size/Capacity. i want to make "ObjectValue" size is same with "ObjectList" size.i`m new in C# and i`m still need learning about that language. Thanks.

Comment
Add comment · Show 7
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 m4s4m0r1 · Aug 03, 2013 at 05:25 AM 0
Share

can you give me an example script? thanks for fast answer

avatar image m4s4m0r1 · Aug 03, 2013 at 06:20 AM 0
Share

i think its a static size, but how to make dynamic Size for the List?

avatar image robertbu · Aug 03, 2013 at 06:22 AM 1
Share

Nope, that is not static size. That is the initial size of the list. It will grow if it needs to. The text in the manual for this constructor says:

Initializes a new instance of the List class that is empty and has the specified initial capacity.

avatar image robertbu · Aug 03, 2013 at 06:27 AM 1
Share

And Capacity (which @cdrandin point out) is read and write, so you can also do:

 public List ObjectList = new List();
 ObjectList.Capacity = 100;
avatar image m4s4m0r1 · Aug 03, 2013 at 06:35 AM 0
Share

hmm, thanks. one more question, if i make like this :

 public GameObject[] justGameObject = new GameObject[10];


i see the size of Array is 10 but when i try this:

 public List<GameObject> ObjectList = new List<GameObject>(100);


i dont see anything (the size is 0).

Show more comments

5 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by Bunny83 · Aug 03, 2013 at 08:19 AM

Like others said you can pass an initial capacity to the constructor of a List, but keep in mind that will only be for the internally used array. To "assign" values to the reserved array slots you still have to use the Add function. Since the capacity equals the size of your other list you can add that much elements without reallocating a new internal array.

 ObjectValue = new List<float>(ObjectList.Count);
 for (int i = 0; i<ObjectList.Count; i++)
     ObjectValue.Add(0f);
Comment
Add comment · Show 2 · 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 Bunny83 · Aug 03, 2013 at 08:22 AM 1
Share

It's a pain to write code on a tablet. The keyboard really needs code completion for some program$$anonymous$$g languages :-)

avatar image m4s4m0r1 · Aug 03, 2013 at 10:44 AM 0
Share

Why i`m not think like that? Oh my god, it`s so easy. Thanks mate,

And Thanks All for helping

avatar image
1

Answer by cdrandin · Aug 03, 2013 at 05:12 AM

http://msdn.microsoft.com/en-us/library/y52x03h2.aspx

Comment
Add comment · 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
0

Answer by robertbu · Aug 03, 2013 at 06:08 AM

Just pass the size you want into the constructor. For 100 element initial size:

 public List<GameObject> ObjectList = new List<GameObject>(100);
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 PixelFireXY · Apr 30, 2021 at 08:40 AM 0
Share

Actually, like this, you just set the capacity of the list, if you are going to get a value in a specific index like:

 ObjectList[34] = someValue

you get ArgumentOutOfRangeException error.

avatar image
0

Answer by SwapnilRane · Dec 08, 2019 at 05:34 AM

for (int i = 0; i < T.Count; i++) ObjectValue.Add(default(T)); Where T is any DataType ie: int, float, etc

Comment
Add comment · 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
0

Answer by jonaslindberg · May 18, 2020 at 09:51 AM

My preferred way is to just add an empty array of the correct length using AddRange

 public List<GameObject> ObjectList = new List<GameObject>();
     public List<float> ObjectValue;
     void Start()
     {
         GameObject[] something = GameObject.FindGameObjectsWithTag("something");
         foreach (GameObject GO in something)
         {
             ObjectList.Add(GO);
         }
         ObjectValue.AddRange(new float[ObjectList.Count]);
     }

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Different timer in a list of gameobject 2 Answers

Selection list from Array Unity - Random - GameObjects array 1 Answer

How to see what element is the current one 2 Answers

Problem with adding 1 element of an array to a list 1 Answer


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