- Home /
Understanding Lists & arrays
Hello, I'd like to understand something with the various collection type over unity
If i want to have an array, containing various strings like this: myArray[0]={"foo","bar"}; myArray[1]={"bar"}; myArray[3]={"foo"};
The list method seems to be fine, bu it needs to have a declared length first. But in my example we see that the various arrays does not have the same length. What the good way to declare this kind of arrays/lists ?
Answer by meat5000 · Oct 17, 2015 at 10:09 AM
http://wiki.unity3d.com/index.php/Choosing_the_right_collection_type
General Consensus based on user experience is that "Generic List" is by far the most useful for most uses in Unity.
Note that it is not the fastest and if you really need to perform multiple operations on a FIXED size LARGE collection of data, List is not always the way to go.
You describe a Jagged Array. In response to whether you can create a Jagged List, I found this
http://answers.unity3d.com/questions/42716/can-you-define-a-jagged-generic-list-in-unityscrip.html
http://answers.unity3d.com/questions/426666/statically-typed-jagged-lists-in-c.html
C# List of Lists was stated in the comments by @NewPath. Looking on t'internet warns to keep an eye on the amount of memory required when using List of Lists.
thanks, okay for the lists, but in the examples that I saw in the link, But I still don't see how to declare the myArray as a list of strings contained in an array as in my example ?
var myList = new List { "foo", "bar", "baz" };
Lists in C# are very lightweight and a good alternative to an array if you don't know the size of a dataset beforehand. The main shortco$$anonymous$$g of List is that because of the internal optimizations, it is not easily inheritable, so for custom collections you typically want to derive from something else.
Yes, just make a list of lists.
var jagList = new List<List<string>>();
I got some trouble with js declarations ;) how can I declare it in c# ?
I'm trying multiple declaration, but I cannot succeed
Answer by Matheuz · Oct 17, 2015 at 04:04 PM
In C#, if it's in method scope, you can declare as NewPath declared above. If it's in class scope:
List<List<string>> jagList = new List<List<string>>();
and what's the correct syntax to populate such a nested list ?
I mean, you could simply look up List on $$anonymous$$SDN and see the full documentation, which would probably help you considerably, but here are a few options for the ter$$anonymous$$ally lazy:
var list jagList = new List<List<string>>();
var innerList1 = new List<string>();
innerList1.Add("foo");
innerList1.Add("bar");
var innerList2 = new List<string>();
innerList2.Add("baz");
jagList.Add(innerList1);
jagList.Add(innerList2);
Or, assignment during declaration:
var jagList = new List<List<string>>()
{
new List<string> { "foo", "bar" },
new List<string> { "baz" }
};
You're right, but i like to work with examples, I found it (for my part) more easy to learn to get the full documentation and have some examples to go with, rather than the raw doc without examples (like the Lists on $$anonymous$$SDN), maybe because coding isn't my fulltime job (I'm a nurse) Anyway, thanks again for your help, it has helped me quite a lot :)
Your answer
Follow this Question
Related Questions
ArrayList Transform type! 2 Answers
Global array list c# 1 Answer
Best way for texture animation 1 Answer
How could I show buttons if I am using array list? 0 Answers
Adding Waypoints using a List 2 Answers