- Home /
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.
can you give me an example script? thanks for fast answer
i think its a static size, but how to make dynamic Size for the List?
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.
And Capacity (which @cdrandin point out) is read and write, so you can also do:
public List ObjectList = new List();
ObjectList.Capacity = 100;
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).
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);
It's a pain to write code on a tablet. The keyboard really needs code completion for some program$$anonymous$$g languages :-)
Why i`m not think like that? Oh my god, it`s so easy. Thanks mate,
And Thanks All for helping
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);
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.
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
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]);
}