How to initialize a List to a given size (as opposed to capacity)?

前端 未结 15 2167
礼貌的吻别
礼貌的吻别 2020-11-28 06:41

.NET offers a generic list container whose performance is almost identical (see Performance of Arrays vs. Lists question). However they are quite different in initialization

相关标签:
15条回答
  • 2020-11-28 06:53
    List<string> L = new List<string> ( new string[10] );
    
    0 讨论(0)
  • 2020-11-28 06:54
    string [] temp = new string[] {"1","2","3"};
    List<string> temp2 = temp.ToList();
    
    0 讨论(0)
  • 2020-11-28 07:01

    Initializing the contents of a list like that isn't really what lists are for. Lists are designed to hold objects. If you want to map particular numbers to particular objects, consider using a key-value pair structure like a hash table or dictionary instead of a list.

    0 讨论(0)
  • 2020-11-28 07:02

    Why are you using a List if you want to initialize it with a fixed value ? I can understand that -for the sake of performance- you want to give it an initial capacity, but isn't one of the advantages of a list over a regular array that it can grow when needed ?

    When you do this:

    List<int> = new List<int>(100);
    

    You create a list whose capacity is 100 integers. This means that your List won't need to 'grow' until you add the 101th item. The underlying array of the list will be initialized with a length of 100.

    0 讨论(0)
  • 2020-11-28 07:02

    A bit late but first solution you proposed seems far cleaner to me : you dont allocate memory twice. Even List constrcutor needs to loop through array in order to copy it; it doesn't even know by advance there is only null elements inside.

    1. - allocate N - loop N Cost: 1 * allocate(N) + N * loop_iteration

    2. - allocate N - allocate N + loop () Cost : 2 * allocate(N) + N * loop_iteration

    However List's allocation an loops might be faster since List is a built-in class, but C# is jit-compiled sooo...

    0 讨论(0)
  • 2020-11-28 07:04

    I can't say I need this very often - could you give more details as to why you want this? I'd probably put it as a static method in a helper class:

    public static class Lists
    {
        public static List<T> RepeatedDefault<T>(int count)
        {
            return Repeated(default(T), count);
        }
    
        public static List<T> Repeated<T>(T value, int count)
        {
            List<T> ret = new List<T>(count);
            ret.AddRange(Enumerable.Repeat(value, count));
            return ret;
        }
    }
    

    You could use Enumerable.Repeat(default(T), count).ToList() but that would be inefficient due to buffer resizing.

    Note that if T is a reference type, it will store count copies of the reference passed for the value parameter - so they will all refer to the same object. That may or may not be what you want, depending on your use case.

    EDIT: As noted in comments, you could make Repeated use a loop to populate the list if you wanted to. That would be slightly faster too. Personally I find the code using Repeat more descriptive, and suspect that in the real world the performance difference would be irrelevant, but your mileage may vary.

    0 讨论(0)
提交回复
热议问题