- Home /
Set size of generic list via script?
I just started using lists and its a bit confusing to me. How can I set the size of my list? Count is read only and Capacity doesn't do it either. With arrays you'd do something like:
array1.length = array2.length;
But how do you do it with generic lists? I tried:
list1.Capacity = list2.Count;
or
list1 = new List<int>(list2.Count);
and some other variations, but it doesn't resize it in the inspector; I can do it manually but I wanna do it via script if possible.
I've read through the http://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx but couldn't find anything that would let me do that.
I just started using them too. From my very limited knowledge, you can't set a permanent size. link text
Yes I went through that too and nothing. Lets wait and see if anyone else can tell us if we can. xD $$anonymous$$aybe we can make an empty array of some length and convert it into a list. I'm not sure, I can't find anything on that either.
I'm liking lists tho, not faster than builtin arrays, but faster than javascript arrays, and have type casting, and more functions that builtin arrays don't.
Answer by Eric5h5 · Mar 09, 2014 at 02:23 AM
You don't explicitly set the size of a List; the size comes from the number of items in it. (Capacity is only for the internal size of the List and something you would rarely if ever use.) If you want a List with a particular number of items you can convert an array to a List.
list1 = new List<int>(new int[list2.Count]);
Aaand there's the answer I was expecting! xD I'm following a tutorial which uses javascript arrays, and doesn't use #pragma strict which makes it not work on mobile platforms, me thinks. So I decided to use lists ins$$anonymous$$d and I'm trying to follow it as closely as possible because I know I'm bound to run into speed bumps at every step. xD
Thank you!
Bleh, nothing should ever use Javascript arrays. ;) Especially not a tutorial, since that's likely to confuse people who aren't aware of the problems with them.
It's an about 2 years old tutorial by Eteeski on AI pathfinding. I've been struggling for days and sleepless nights to do some simple A* or node based or raycast or whatever kind of AI pathfinding to no avail. I understand it all and how A* works but I don't know how to write it into code and then I get confused and lost, and end up rocking in my chair back and forth, laughing, and crying. Ok that last part didn't actually happen. xD
I'm aware of Aron Granberg's pathfinding solution and other similar ones, but I really wanna learn to do it, not use someone elses code without understanding and knowing how to do the basics of it. But the lack of tutorials on it, and 97% of them being in C# is killing me. xD
Well, at least it's straightforward enough in most cases to convert Javascript arrays to generic Lists...if you get stuck on anything just post another question.
Answer by JoeStrout · May 03, 2017 at 03:56 PM
This page comes up pretty early in searches for how to resize a list. So, here's my solution — just stick this in a (possibly static) class somewhere, and it adds a Resize(n) method to the generic List.
public static void Resize<T>(this List<T> list, int newCount) {
if (newCount <= 0) {
list.Clear();
} else {
while (list.Count > newCount) list.RemoveAt(list.Count-1);
while (list.Count < newCount) list.Add(default(T));
}
}
Cleaner solution is to use Linq: http://stackoverflow.com/questions/31656460/c-sharp-enumerable-take-with-default-value
Are you referring to .Take? That can only shorten the list, and it does so by making a new list, which is wasteful. The Resize extension method above can both shorten and extend, and does so very efficiently (no copies).
(That SO page includes some extension methods to address this, some of which are functionally the same as my solution above.)
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How do I cut off a section of my terrain? 2 Answers
Remove and resize List 1 Answer
How to convert builtin arrays to generic lists in boo? 1 Answer
Generic List In Custom Editor Window 2 Answers