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 /
avatar image
0
Question by Eidern · Oct 17, 2015 at 10:01 AM · arraylist

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 ?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 8 · 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 Eidern · Oct 17, 2015 at 10:52 AM 0
Share

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 ?

avatar image meat5000 ♦ Eidern · Oct 17, 2015 at 12:13 PM 1
Share

Do you mean this?

https://www.google.co.uk/search?q=jagged+array&ie=utf-8&oe=utf-8&gws_rd=cr&ei=PTsiVq-TA8zXUZHlh9AC

avatar image NewPath Eidern · Oct 17, 2015 at 01:54 PM 0
Share

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.

avatar image Eidern · Oct 17, 2015 at 02:37 PM 0
Share

exactly @meat5000, does List can work as jagged arrays?

avatar image NewPath Eidern · Oct 17, 2015 at 02:52 PM 1
Share

Yes, just make a list of lists.

 var jagList = new List<List<string>>();

avatar image Eidern NewPath · Oct 17, 2015 at 03:59 PM 0
Share

I got some trouble with js declarations ;) how can I declare it in c# ?

I'm trying multiple declaration, but I cannot succeed

Show more comments
avatar image
1

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>>();

Comment
Add comment · Show 4 · 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 Eidern · Oct 17, 2015 at 05:32 PM 0
Share

and what's the correct syntax to populate such a nested list ?

avatar image NewPath Eidern · Oct 17, 2015 at 05:43 PM 0
Share

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" }
 };


avatar image Eidern NewPath · Oct 17, 2015 at 06:03 PM 0
Share

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 :)

Show more comments

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

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

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


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