如何初始化列表< T>给定的尺寸(相对于容量)? [英] How to initialize a List<T> to a given size (as opposed to capacity)?

查看:194
本文介绍了如何初始化列表< T>给定的尺寸(相对于容量)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET提供了一个通用的列表容器,其性能几乎是相同的(见阵列与清单问题的性能)。然而它们是在初始化很大的不同。

数组是很容易初始化一个默认值,并定义他们已经有一定的规模:

 的String []氩=新的字符串[10];
 

它允许一个安全随机分配项目,说:

 氩[5] =你好;
 

与清单事情比较棘手。我可以看到在做相同的初始化两种方式,这两者都不是,你会什么叫优雅的:

 名单,其中,串> L =新的名单,其中,串>(10);
的for(int i = 0;我小于10;我++)L.Add(空);
 

 的String []氩=新的字符串[10];
名单<字符串> L =新的名单,其中,串>(AR);
 

什么将是一个更清洁的方式?

编辑:到目前为止的答案是指能力,这是别的东西比pre-填充列表。例如,只可容纳10创建的列表上,缺一不可 L [2] =someValue中

编辑2:人们想知道为什么我想用列表这种方式,因为这是没有办法的办法,他们的目的使用。我可以看到两个方面的原因:

  1. 人们可以非常令人信服认为,名单是下一代阵列,增加了灵活性,几乎没有损失。因此,人们应该使用他们默认。我指出他们可能不那么容易初始化。

  2. 什么我目前正在写一个基类产品默认的功能的一个更大的框架的一部分。在默认的功能,我提供的名单的大小是已知的先进,所以我也可以使用数组。不过,我想提供任何基类的机会,​​动态地扩展它,所以我选择列表。

解决方案

我不能说我需要这往往 - 你能不能提供更多的细节,为什么你要这样?我可能把它作为一个静态方法在辅助类:

 公共静态类列表
{
    公共静态列表< T> RepeatedDefault< T>(诠释计数)
    {
        返回重复(默认值(T),计数);
    }

    公共静态列表< T>重复< T>(吨价,诠释计数)
    {
        名单< T> RET =新的名单,其中,T>(计数);
        ret.AddRange(Enumerable.Repeat(价值数));
        返回RET;
    }
}
 

您的可以的使用 Enumerable.Repeat(默认值(T),计数).ToList()但是这将是低效的,由于缓冲区调整

编辑:正如评论指出的那样,你可以把重复使用循环来填充列表,如果你想的话。这将是稍快了。我个人觉得用重复详细描述了code和怀疑,在真实世界中的性能差异将是无关的,但您的里程可能会有所不同。

.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.

Arrays are very easy to initialize with a default value, and by definition they already have certain size:

string[] Ar = new string[10];

Which allows one to safely assign random items, say:

Ar[5]="hello";

with list things are more tricky. I can see two ways of doing the same initialization, neither of which is what you would call elegant:

List<string> L = new List<string>(10);
for (int i=0;i<10;i++) L.Add(null);

or

string[] Ar = new string[10];
List<string> L = new List<string>(Ar);

What would be a cleaner way?

EDIT: The answers so far refer to capacity, which is something else than pre-populating a list. For example, on a list just created with a capacity of 10, one cannot do L[2]="somevalue"

EDIT 2: People wonder why I want to use lists this way, as it is not the way they are intended to be used. I can see two reasons:

  1. One could quite convincingly argue that lists are the "next generation" arrays, adding flexibility with almost no penalty. Therefore one should use them by default. I'm pointing out they might not be as easy to initialize.

  2. What I'm currently writing is a base class offering default functionality as part of a bigger framework. In the default functionality I offer, the size of the List is known in advanced and therefore I could have used an array. However, I want to offer any base class the chance to dynamically extend it and therefore I opt for a list.

解决方案

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.

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.

这篇关于如何初始化列表&LT; T&GT;给定的尺寸(相对于容量)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆