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
0
Question by superluigi · Feb 23, 2014 at 11:12 PM · generic

Help understanding this generic list in JS.

I'm having touble finding info on generic lists for js online. The scripting reference has nothing on the and the tut video are for c# and the js tab under he tut video for js says that js is too different from the video, thus making a translation unusable. Somewhere else it said that js can't use generic lists(might of been the scripting reference). However I finally found some code and understood what most of it does. I just need help understanding like 4 lines. Truth be told, I believe it's a generic list but I'm not even sure.

 #pragma strict
 import System.Collections.Generic;
 
 function Start ()
 
 {
     var list : List.<String>;
 
     list = new List.<String>();
 
     //Capacity is the number of elements that the List<T> can store before resizing is required
 
     //Count is the number of elements that are actually in the List<T>.
 
     Debug.Log("list capacity is " +list.Capacity);
 
     Debug.Log("list count is " +list.Count);
 
     Debug.Log("Adding 4 entries");
   
 
     list.Add("Generics");
 
     list.Add(" work");
 
     list.Add(" in");
 
     list.Add(" js");
 
     list.Add(" too.");
       
 
     Debug.Log("Note that now capacity is bigger than count"); 
  
 
     Debug.Log("list capacity is " +list.Capacity);
 
     Debug.Log("list count is " +list.Count);
 
     
     //js cannot have foreach but can use for in
 
     var sentence:String;
 
  
     for( var word:String in list)
 
     {
 
         sentence +=word;
 
     }
 
     Debug.Log(sentence);
 
     Debug.Log("list.Contains(\"generics\") is " + list.Contains("generics"));
 
     Debug.Log("list.Insert(5, \" Unity js rocks !\");" +" inserts a new item at the end of the list");
 
     list.Insert(5, " Unity js rocks !");
 
     Debug.Log("Let's check it");
 
     sentence = "";
 
     
     for( var word:String in list)
 
     {
 
        sentence +=word;
 
     }
     
     Debug.Log(sentence);
 
     //For more methods of List check out http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx
 
     //Or google c sharp List
 }

line#9: Why does he need to create a new list when he already created it on the line above?

line#19: He says adding 4 entries but he adds 5, is it just a typo?

line#33: Exactly, capacity end up being 8 and count ends up being 5, but why? If they both started at 0, and then he added 5 things, why does capacity jump up to 8?

line#60: What does the 5 do? Does it add something after the 5 thing in the list which would be "too", or does it add something that's 5 entries long?

line#64: I commented this out and debuglog printed "Generics work in js too" twice. So I know what it does I just dont understand why.

Sorry for all the questions. I used to do that before but now I usually stick to figuring out everything on my own. The problem is that the info on this is scarce so I could use the help.

Comment
Add comment · Show 5
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 getyour411 · Feb 23, 2014 at 11:26 PM 0
Share

9 - Object definition/declaration vs. Instantiation.

19 - yes, typo

33 - capacity is more related to memory management and list restructuring, it's unlikely you'll actually use it

60 - often we don't care where in the List something is added, but in this test case it will be added at List index pos 5

Since I don't use JS these are best guesses so I'll leave it as a comment for someone to validate/post a JS answer.

avatar image karlhulme · Feb 23, 2014 at 11:31 PM 0
Share

hi,

line 9 - The line above declares a variable of type 'generic list'. But is currently null. Line 9 actually creates the list.

line 19 - agreed looks like a typo

line 33 - capacity increases in chunks so that the memory for the list doesn't have to be reallocated every time an item is added. This is why a list can offer performance benefits over and above an array.

line 60 - this inserts a new entry at index 5. Since the indexing is zero based, and there are currently 5 entries, this should just insert it on the end of the list.

line 64 - lines 46-52 (ish) build up the sentence by accessing the items in the list. line 64 then clears the result of the processing so that the process can be repeated on lines 67-71. only the second time round, we have the extra item thanks to line 60.

I don't develop in unityscript but this example doesn't seem to differ from c# beyond $$anonymous$$or syntax. Hopefully someone else will confirm/correct me.

in the mean-time, hope this helps.

avatar image Linus · Feb 24, 2014 at 12:36 AM 0
Share

http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?#Generic_List

Best resource for lists/array, i tend to stick with Generic List now, easy to use and dont seem to cause problems. You can pretty much use the $$anonymous$$SDN for documentation on usage.

avatar image superluigi · Feb 24, 2014 at 01:15 AM 0
Share

Thank you everyone.

avatar image Benproductions1 · Feb 24, 2014 at 10:50 AM 0
Share

@karlhulme Yep, you are completely correct that it really isn't very different from C#

1 Reply

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

Answer by Benproductions1 · Feb 24, 2014 at 11:19 AM

Line: 9

As stated before by @getyour411, the difference between line 9 and line 7 are that line 7 declares a variable (`list` of type List.), while line 9 creates a new instance of a list and assigns that instance to list. Because List is a reference type, one that cannot be passed by value, it needs an instance to work.

Ideally, because the type is obvious, those lines should be written as simply:

 var list = List.<String>();

Or if you love declaring types:

 var list:List.<String> = List.<String>();

For more information on types, references, objects, structures etc. Do some research on OO concepts.

Line: 19

I think we've established thats a typo

Line: 33

Remember that List is a .NET type, so it's documentation is intuitively on msdn:

Capacity is the number of elements that the List can store before resizing is required, whereas Count is the number of elements that are actually in the List.

Capacity is always greater than or equal to Count. If Count exceeds Capacity while adding elements, the capacity is increased by automatically reallocating the internal array before copying the old elements and adding the new elements.
If the capacity is significantly larger than the count and you want to reduce the memory used by the List, you can decrease capacity by calling the TrimExcess method or by setting the Capacity property explicitly. When the value of Capacity is set explicitly, the internal array is also reallocated to accommodate the specified capacity, and all the elements are copied. A good thing to note is that List uses a built-in array internally. ##Line: 60 Again, this can be seen in the .NET documentation:

Inserts an element into the List at the specified index.

Arguments: - index:int, The zero-based index at which item should be inserted. - item:T, The object to insert. The value can be null for reference types.

Line: 64

By simply desk-checking (running through the program in our head) we can see that at line 64, sentence will be a construction made up of all the elements in list, concatenated into a single String. If we were to run the code following line 64, the next time sentence is logged, it will have two sentences in one String. As this is not the required behaviour, sentence is set back to an empty String.

To make it more obvious line 43 could also be written as:

 var sentence = "";

Or again, with typing:

 var sentence:String = "";

Final

Just as a final note, if you're ending for loops like this:

 statement) {

Then they should also start with the same spacing:

 for (statement

not:

 for( statement

Remember: the ( is wrapping the statement, not trailing the for.

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 superluigi · Feb 25, 2014 at 06:45 AM 0
Share

Ok lol now I get line 64. I thought "" did something in code but now I see what it does. Basically the sentence equals nothing, because nothing is inside the quotation marks.

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

24 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

Related Questions

JS: Dictionary is not a generic definition error... 3 Answers

Script from JS to C# 1 Answer

Array problem,different result 1 Answer

Teleport Charactor 2 Answers

GUI text Issues 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