Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Using List<T> without a class

Discussion in 'Scripting' started by soxroxr, Oct 26, 2014.

  1. soxroxr

    soxroxr

    Joined:
    Jul 17, 2012
    Posts:
    60
    Is there a possible way to use a list without declaring a class? I just need to store ints in it, nothing fancy. I would use an array, but I need to do some randomization where items can't be repeated, and I figure this is a lot faster than indexing the thing repeatedly and retrying when it fails.

    I do see arraylists, but all over everywhere I've heard that you should never use them unless you're specifically targeting really old systems. They seem to do what I need quite well. I'd like to release to Windows Phone though which would be ruled out if I use arraylists.

    Aside from that, I'm open to other solutions to my problem!

    Thank you muchly.
     
  2. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
  3. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
    Code (CSharp):
    1. List<int> IntList = new List<int>();
    2. IntList.Add(1);
    3. IntList.Add(2);
    4. IntList.Add(3);
    5.  
    6. foreach(int Int in IntList)
    7. Debug.Log (Int.ToString());
    What's the problem?

    Also, I believe in C# a 1-dimensional array actually implements List behind the scenes, so there's little difference. Might as well use the List.

    You are going to have a problem with duplication because the List doesn't prevent duplication. To my knowledge, the only IEnumerable collection that checks for duplicates is a Dictionary, and it does that only for Keys, not Values.
     
    soxroxr likes this.
  4. soxroxr

    soxroxr

    Joined:
    Jul 17, 2012
    Posts:
    60
    Ahh okay! 100% of the examples I saw around the internet, and on MSDN, Unity Answers, and the Unity documentation, they use classes to handle it. Thank you!

    I plan to handle that part separately though, don't worry. :)

    Thank you for responding so fast too, normally it takes a while for me to get an answer, I didn't expect it! And as such, sorry for responding so slowly, haha.
     
    Last edited: Nov 6, 2014
  5. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
    Whenever you see <T> just use whatever type you like.
     
    soxroxr likes this.
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    I think it's the other way around
    List<T> implements T[] in the background
     
  7. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
    Hmm.
     
  8. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    An Array implements the 'interface' IList. The use of these words is in the context of typing... an Array is considered the type IList and has defined all the methods that make up an IList.

    List implements itself using an Array underneath, wrapping said array with the interface in the shape of a List. In this case implements means that the actual code behind the List class is using an Array.

    This confusing dual meaning of the words interface and implements comes from .Net reusing the words to define its interface structure. Where as the concepts of implementing and interfaces existed before hand.

    Conceptually the definitions are as follows (language independent... this is an OOP concept):
    interface - this is outward facing collection of methods and properties of a class. They allow access to the internals. It facilitates the concept of 'encapsulation'.

    implements - the actual guts inside the class. You have a method call 'Run' being the interface, and you have the code that is operated on 'Run' which is the implementation.

    .Net/Mono definitions of keywords are as follows:
    interface - a structure for defining the methods and properties that a class may implement. This acts as a contract for type safety. It ensures that the object, with out knowing its actual type, has the methods and properties defined in the interface

    implements - declaring that some class obeys the aforementioned interface (the keyword doesn't actually exist in C#, but does in VB.Net... the keyword in C# is a colon/:, it's the same keyword for inherits)


    I like the analogy of "you screw a screw with a screwdriver" showing you could use the same word multiple times to mean different things.


    In the end the Array is the simplest of structures here. But also has the more limitations. Namely in resizing of the Array. Lists are technically more complex, and wrap around an array, but it streamlines the resizing process to make it faster. Basically it maintains an array inside itself that is larger than the collection needs to be so that every add just takes up a new slot in the existing array instead of creating a new array. The array is only resized if you add more elements than the array in the list can hold. At which point it grows larger with a buffer so it can continute efficiently adding. In the end it has a larger than necessary memory foot print, to have better speed during add.
     
    Last edited: Oct 27, 2014