空数组的定义长度

时间:2015-10-07 15:22:24

标签: c# .net arrays

我正在尝试创建一个包含100个元素的数组,其中每个元素都由随机生成的整数填充。

        int[] NumArray1 = { };
        int[] NumArray2 = { };
        Random Ran = new Random();
        int ran1, ran2;
        for (int x = 0; x <= 99; ++x)
        {
            ran1 = Ran.Next(0, 10);
            NumArray1[x] = ran1;
        }
        for (int y = 0; y <= 99; ++y)
        {
            ran2 = Ran.Next(0, 10);
            NumArray2[y] = ran2;
        }

除非我希望NumArray1和NumArray2长100个元素。

1 个答案:

答案 0 :(得分:2)

int[] arr = new int[100];
Random ran = new Random();
for (int x = 0; x < 100; x++)
{
    arr[x] = ran.Next(0, 10);
}