- Home /
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.
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.
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.
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.
@karlhulme Yep, you are completely correct that it really isn't very different from C#
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 theList
can store before resizing is required, whereasCount
is the number of elements that are actually in theList
.
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
.
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
Follow this Question
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