使用for循环填充锯齿状数组 [英] jagged array filling using for loop

查看:72
本文介绍了使用for循环填充锯齿状数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想在C#中使用for循环填充锯齿状数组。在这个锯齿状数组中,我们将有768行,每行有9个元素。我想用随机数填充每一行。例如我的第一行将是



row 1 12 18 19 158 147 145 147 155 15

row2 14 17 18 15 19 16 114 15 4



谢谢。

Hi I want to fill jagged array using for loop in C#.In this jagged array we would have 768 rows with 9 elements in each row. I want to fill each row with random number.for example my first row would be

row 1 12 18 19 158 147 145 147 155 15
row2 14 17 18 15 19 16 114 15 4

thank you.

推荐答案

和问题是?试过?



你需要两个循环,然后只需要为每个数组元素添加随机数。试试。
And the issue is? Tried?

You need two for loops and then just get random numbers to add to each array element. Try out.


首先,如果你知道coulmns和行的数量并且它们是固定的,它不是锯齿状的,它只是2维数组。





秒,这实际上很简单,只需在此处查看示例代码(随机范围:0,1000)



First, if you know the number of coulmns and rows and they are fixed it is not jagged, it is just 2 dimentional array.


second, this is actually easy, just see the sample code here (random range: 0, 1000)

public static void Main(string[] atgs)
       {
           //row 1 12 18 19 158 147 145 147 155 15
           //row2 14 17 18 15 19 16 114 15 4

           const int rows = 768;
           const int columns = 9;
           int[,] ai = new int[rows, columns];

           Random r = new Random();

           for (int i = 0; i < rows; ++i)
           {
               for (int j = 0; j < columns; ++j)
               {
                   ai[i, j] = r.Next(0, 1000);
               }
           }


       }


http://stackoverflow.com/questions/468832/why-are-multi-dimensional -arrays-in-net-slow-than-normal-arrays [ ^ ]


这篇关于使用for循环填充锯齿状数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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