Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C中复制列表成员?_C#_List - Fatal编程技术网

C# 如何在C中复制列表成员?

C# 如何在C中复制列表成员?,c#,list,C#,List,我希望列表是:a b a b a。。。直到list.Count=x。我如何实现这一点?您可以使用LINQ轻松实现: int x = 9; List<string> list = new List<string> {"a", "b"}; 或不使用查询表达式: List<string> result = (from ignored in Enumerable.Range(0, int.MaxValue) from

我希望列表是:a b a b a。。。直到list.Count=x。我如何实现这一点?

您可以使用LINQ轻松实现:

int x = 9; 
List<string> list = new List<string> {"a", "b"};
或不使用查询表达式:

List<string> result = (from ignored in Enumerable.Range(0, int.MaxValue)
                       from item in list
                       select item).Take(count).ToList();

这里使用Enumerable.Range只是为了强制重复——就像Ani使用Enumerable.Repeat的方法一样,这当然也会起作用。

您可以轻松地使用LINQ:

int x = 9; 
List<string> list = new List<string> {"a", "b"};
或不使用查询表达式:

List<string> result = (from ignored in Enumerable.Range(0, int.MaxValue)
                       from item in list
                       select item).Take(count).ToList();

这里使用Enumerable.Range只是为了强制重复,就像Ani使用Enumerable.Repeat的方法一样,这当然也会起作用。

类似的方法应该会起作用。我没有检查它,让它成为你的练习:

List<string> result = Enumerable.Range(0, int.MaxValue)
                                .SelectMany(ignored => list)
                                .Take(count)
                                .ToList();

这样的办法应该行得通。我没有检查它,让它成为你的练习:

List<string> result = Enumerable.Range(0, int.MaxValue)
                                .SelectMany(ignored => list)
                                .Take(count)
                                .ToList();
那么:

int currentCount = list.Count;

for (int i=0; i<x; ++i)
{
     list.Add(list[i%currentCount]);  
}
那么:

int currentCount = list.Count;

for (int i=0; i<x; ++i)
{
     list.Add(list[i%currentCount]);  
}


错误:当前数据库中不存在名称“count”context@Bonaca:你需要运用一些常识。我使用count作为一个比你在问题中使用的x更具描述性的名称。如果您确实有一个名为x的变量,表示要生成多少项的计数,只需将我的代码更改为使用x而不是count。错误:当前列表中不存在名称“count”context@Bonaca:你需要运用一些常识。我使用count作为一个比你在问题中使用的x更具描述性的名称。如果您确实有一个名为x的变量,表示要生成多少项的计数,只需将我的代码更改为使用x而不是count。我不能这样做,因为我的列表不总是a、b。下一次我的列表是1,2-或其他什么。那么用一个可变的方法参数替换数组,也许?然后根据需要填充。我不能这样做,因为我的列表不总是a,b。下一次我的列表是1,2-或其他什么。那么用一个可变的方法参数替换数组,也许?然后根据需要填充。我不能这样做,因为我的列表不总是a,b。下次我的单子是1,2-或者其他什么。你是什么意思?你能举个例子说明你想要什么样的输出吗?我不能这样做,因为我的列表不总是a,b。下次我的单子是1,2-或者其他什么。你是什么意思?你能举一个例子说明你想要什么样的输出吗?不是下行表决器,但至少你需要运行循环直到x-currentcountBonaca:哦,对不起。我应该为int I=0写;iPS。Stackoverflow更像是一种指导,至少它应该特别适合与您同级的人。您不应该期望其他人为您编写代码。否则你永远也学不到……迈克尔,谢谢你。现在可以了。我希望——我会成为像你一样的程序员。谢谢你关于x