49

I like to create an int[] with length X and value it with [0,1,2....X]

e.g.

public int[] CreateAA(int X){}

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

Eric Yin
  • 8,737
  • 19
  • 77
  • 118
  • Possible duplicate of [How to create array with sequence of integers in C#?](http://stackoverflow.com/questions/4588787/how-to-create-array-with-sequence-of-integers-in-c) – Michael Freidgeim Aug 04 '16 at 01:56

6 Answers6

104

You can avail the functionality of 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.

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)

V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
9

Using Enumerable.Range(0, 10).ToArray() is very concise but if you want to create a very large array the ToArray extension 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).

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.Range does. So avoiding the array of consecutive numbers in the first place may be even better than thinking about how to create it.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
7

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 value and 10 (X + 1) is the length of array

So a general one applicable to all

int[] AA = Enumerable.Range(0, X + 1).ToArray();
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
2

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;
}
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Thanatos
  • 1,176
  • 8
  • 18
2

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

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;
}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
2

To initialize try this

int x = 10;
Enumerable.Range(0, x)
          .Select((v, i) => v + i).ToArray();
Asif Mushtaq
  • 13,010
  • 3
  • 33
  • 42
  • This answer is wrong, but still led me to a nice solution. This code will return [0,2,4,... 18]. The `Select()` is not necessary, Range itself already returns [0,1,2,3,.. 9] – N4ppeL Jan 21 '22 at 14:48