Search Unity

Arrays and Lists and ArraLists

Discussion in 'Scripting' started by MitchStan, Dec 1, 2010.

  1. MitchStan

    MitchStan

    Joined:
    Feb 26, 2007
    Posts:
    568
    I've been using arrays and bulitin arrays using unityscript - I would like to learn more about using Lists and ArrayLists. I've been searching the Unity docs - not much on Lists and ArrayLists. The forum search and google helped, but can someone post a link that might be helpful to me. Thanks.

    Or if someone feels generous - perhaps offer up sone words of programmer wisdom as to the differences between these variable types? Thanks for any help.

    Mitch
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    ArrayList and List is just .NET, so you can read the MSDN docs. There's not much reason to use ArrayList vs. Array (it's slightly faster but otherwise mostly the same), but you should use Lists where possible, since they have compile-time type safety and they're a lot faster.

    --Eric
     
  3. MitchStan

    MitchStan

    Joined:
    Feb 26, 2007
    Posts:
    568
    Thanks as always, Eric.
     
  4. tonyd

    tonyd

    Joined:
    Jun 2, 2009
    Posts:
    1,224
  5. mightymao

    mightymao

    Joined:
    Oct 21, 2009
    Posts:
    108
    Yep! Unity built-in array as well as .NET array are fast. As you know, these arrays cannot not be re-sized. Also, there is a JavaScript Array class.

    In order to use the generic List<T>, you need to include additional namespace System.Collections.Generic. The List<T> also displays in the Property Inspector.

    Here are some of the formats:

    Code (csharp):
    1.  
    2. //UnityScript
    3.  
    4. var players: string[];
    5.  
    6. //JavaScript Array
    7.  
    8. var players = new Array();
    9.  
    10. //C#
    11.  
    12. string[] Players = new string[5];
    13.  
    14. List<string> players = new List<string>();
    15.  
    16. ArrayList players = new ArrayList();
    17.  
    18.  
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Those are the same thing.

    Javascript includes the namespace automatically.

    --Eric
     
  7. mightymao

    mightymao

    Joined:
    Oct 21, 2009
    Posts:
    108
    What are the same thing?

    Are we talking about JavaScript or C# regarding the generic List<T>?
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Built-in arrays and non-resizable .NET arrays. Those are the same thing.

    Read what you quoted again. :)

    --Eric
     
  9. mightymao

    mightymao

    Joined:
    Oct 21, 2009
    Posts:
    108
    Yes, I read it over again. It's still asking you a question.

    Are we talking about JavaScript or C# regarding the generic List<T>?

    Is something I am missing here? All I said was when you use the generic List<T>, you need to add additional namespace System.Collections.Generic. As you said, "List is just .NET"; therefore, when you are coding in C#, you need to add this namespace yourself. When I wrote my original response (to MitchStan), I provided additional information that may useful to other users.
     
  10. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    I think he's saying that Unity's JavaScript compiler automatically (yuck) includes the System.Collections.Generic namespace so you do not need to explicitly include it yourself. In C# you need to include it or declare your List using the full namespace.
     
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You:

    "In order to use the generic List<T>, you need to include additional namespace System.Collections.Generic."

    Me:

    "Javascript includes the namespace automatically."

    I'm afraid I'm at a loss as to how that could be unclear. I'm not sure what else to say without just repeating myself.

    .NET is not C#. This topic is about Array / ArrayList / List, and since Array isn't in C#, I think it should be fairly clear that we're focussing on Javascript here. Let's try not to confuse the issue too much (probably too late for that ;) ).

    --Eric
     
  12. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    It's not confusing at all! You see, built-in arrays in JavaScript are the same as arrays in C#. But Arrays in JavaScript aren't the same as arrays in C#, and in fact such Arrays do not even exist in C#. Perhaps one could consider them as ArrayLists, but if you were to use an ArrayList in JavaScript you might as well use an Array instead. I imagine that using ArrayLists are just wrappers around arrays, and so Arrays too in JavaScript are wrappers around built-in arrays which are just arrays anyway. Then you get the List class which definitely wraps an array and automatically resizes that fixed array as needed. The great thing about Lists and arrays, but not ArrayLists or Arrays, is that they are fully typesafe. In JavaScript, Arrays are not typesafe but arrays are. ArrayLists are not typesafe but Lists are. In C#, since it doesn't have Arrays, the only non-typesafe one it has are ArrayLists, and thus arrays, and Lists are typesafe. Ultimately in JavaScript, ArrayList isn't necessary, you may as well use Arrays. But in C# you should not use ArrayLists and instead stick to Lists, and if you want an List to be non-typesafe just type the generic argument of it as Object, but not the built-in UnityEngine.Object but instead System.Object. This is probably because C# has arrays and Lists, but not Arrays. If you want to use dynamically sizing typesafe arrays in JavaScript, use Lists, but if it's a fixed size then just use arrays.

    There, that should be clear enough.
     
  13. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yeah. If (dynamic size) {use List} else {use built-in array}.

    --Eric
     
  14. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    Eric, I get the following errors:

    Assets/NewBehaviourScript.js(1,13): BCE0044: expecting ), found 'size'.
    Assets/NewBehaviourScript.js(1,17): BCE0043: Unexpected token: ).
    Assets/NewBehaviourScript.js(1,24): BCE0044: expecting :, found 'List'.

    can u fix plz kthxbai
     
  15. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Just click the "Fix All Script Errors" button. It's right next to the "Make MMO" button.

    --Eric
     
  16. mightymao

    mightymao

    Joined:
    Oct 21, 2009
    Posts:
    108
    Well, now I am more confused. FizixMan, are you saying there are ArrayLists and Lists in the JavaScript (UnityScript)? How do you convert the following statements to work in JavaScript in Unity?

    Code (csharp):
    1.  
    2. //C#
    3.  
    4. List<string> players = new List<string>();
    5.  
    6. ArrayList players = new ArrayList();
    7.  
    8.  
    Thanks!
     
  17. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Oh no!

    Code (csharp):
    1. var players = new List.<String>();
    2.  
    3. var players = new ArrayList();
    ArrayLists were always possible; generic Lists were added to JS in Unity 3. If you need to specify the type, they would be List.<String> and ArrayList respectively.

    Edit: I should say that Lists were possible in JS previously, but to use them you would have to use return values from C# functions because the syntax for declaring them directly wasn't implemented. As I mentioned, .NET is not C#. C# is a language that uses .NET features, and Javascript (in Unity) is a language that uses .NET features. So is Boo for that matter.

    --Eric
     
    Last edited: Dec 1, 2010
  18. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    lol sorry! It wasn't my intention to confuse anybody! >.>
     
  19. mightymao

    mightymao

    Joined:
    Oct 21, 2009
    Posts:
    108
    Well, got it. Part of my confusion, did not know that JavaScript in Unity has the generic List and ArrayList. Then again, most of my coding is done in C#.

    Thanks! Peace!
     
  20. MitchStan

    MitchStan

    Joined:
    Feb 26, 2007
    Posts:
    568
    Wow - this will keep me up for a few hours! :)
     
  21. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Actually this is wrong...you can use List without importing the System.Collections.Generic namespace, but not other things, like Dictionary.

    --Eric