创建 c# int[] 值为 0,1,2,3... 长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10681882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create c# int[] with value as 0,1,2,3... length
提问by Eric Yin
I like to create an int[]with length Xand value it with [0,1,2....X]
我喜欢创建一个int[]长度X并用 [0,1,2....X] 来评价它
e.g.
例如
public int[] CreateAA(int X){}
public int[] CreateAA(int X){}
int[] AA = CreateAA(9) => [0,1,2,3,4,5,6,7,8,9]
int[] AA = CreateAA(9) => [0,1,2,3,4,5,6,7,8,9]
is there any easy method? Or have to loop and init value
有什么简单的方法吗?或者必须循环和初始化值
采纳答案by V4Vendetta
You can avail the functionality of IEnumerable.
您可以利用 IEnumerable 的功能。
int[] arr = Enumerable.Range(0, X+1).ToArray();
This will create a IEnumerable List for you and .ToArray()will satisfy your int array need.
这将为您创建一个 IEnumerable 列表,.ToArray()并将满足您的 int 数组需求。
So for X=9 in your case it would generate the array for [0,1,2,3,4,5,6,7,8,9](as you need)
因此,对于 X=9 在您的情况下,它会为[0,1,2,3,4,5,6,7,8,9](根据需要)生成数组
回答by Nikhil Agrawal
Why make a function when it is already there.
为什么要在函数已经存在的情况下创建它。
For this specific example, use
对于此特定示例,请使用
int[] AA = Enumerable.Range(0, 10).ToArray();
where 0 is the starting valueand 10 (X + 1) is the length of array
哪里0 is the starting value和10 (X + 1) is the length of array
So a general one applicable to all
所以一个通用的适用于所有人
int[] AA = Enumerable.Range(0, X + 1).ToArray();
回答by Thanatos
with function and loop:
带函数和循环:
static int[] f(int X)
{
int[] a = new int[X+1];
for(int i = 0; i < a.Length; i++)
a[i] = i;
return a;
}
回答by Guffa
For completeness, here is a function that creates an array.
为了完整起见,这是一个创建数组的函数。
I made it a bit more versatile by having parameters for the min and max value, i.e. CreateArray(0, 9)returns {0,1,2,3,4,5,6,7,8,9}.
我通过设置最小值和最大值的参数(即CreateArray(0, 9)返回)使其更加通用{0,1,2,3,4,5,6,7,8,9}。
static int[] CreateArray(int min, int max) {
int[] a = new int[max - min + 1];
for (int i = 0; i < a.Length; i++) {
a[i] = min + i;
}
return a;
}
回答by Martin Liversage
Using Enumerable.Range(0, 10).ToArray()is very concise but if you want to create a very large array the ToArrayextension method will have to collect the numbers into a buffer that will have to be reallocated multiple times. On each reallocation the contents of the buffer is copied to the new larger buffer. .NET uses a strategy where the size of the buffer is doubled on each reallocation (and the initial buffer has four items).
使用Enumerable.Range(0, 10).ToArray()非常简洁,但如果你想创建一个非常大的数组,ToArray扩展方法必须将数字收集到一个缓冲区中,该缓冲区必须多次重新分配。每次重新分配时,缓冲区的内容都会复制到新的更大的缓冲区中。.NET 使用的策略是每次重新分配时缓冲区的大小加倍(并且初始缓冲区有四个项目)。
So if you want to avoid multiple reallocations of the buffer you need to create the array in advance:
因此,如果您想避免多次重新分配缓冲区,您需要提前创建数组:
int[] aa = new int[10];
for (var i = 0; i < aa.Length; i += 1)
aa[i] = i;
This is the most efficient way of initializing the array.
这是初始化数组的最有效方法。
However, if you need an array of say 100,000,000 consecutive numbers then you should look at a design where you don't have to keep all the numbers in an array to avoid the impact of the memory requirement. IEnumerable<int>is very useful for this purpose because you don't have to allocate the entire sequence but can produce it while you iterate and that is exactly what Enumerable.Rangedoes. So avoiding the array of consecutive numbers in the first place may be even better than thinking about how to create it.
但是,如果您需要一个包含 100,000,000 个连续数字的数组,那么您应该考虑这样一种设计,即您不必将所有数字都保留在一个数组中以避免内存要求的影响。IEnumerable<int>为此目的非常有用,因为您不必分配整个序列,而是可以在迭代时生成它,而这正是Enumerable.Range它的作用。因此,首先避免连续数字的数组可能比考虑如何创建它更好。
回答by Asif Mushtaq
To initialize try this
要初始化试试这个
int x = 10;
Enumerable.Range(0, x)
.Select((v, i) => v + i).ToArray();

