简体   繁体   English

用一些值填充 C# 二维数组

[英]Fill a C# 2d array with some value

I've got this program where I'm creating a character array that's filed with a given character, and I was wondering if there is a faster way to do so than the nested for loops that I've been using, like the numpy.zeros command in python.我有这个程序,我正在创建一个用给定字符归档的字符数组,我想知道是否有比我一直使用的嵌套 for 循环更快的方法,比如 numpy。 python 中的归零命令。 Like for an array arr,就像数组 arr 一样,

char[,] arr = new char array[3, 4]

Is there a faster way to fill it than:有没有比以下更快的方法来填充它:

        for (int i=0; i<3; i++)
        {
            for (int j=0; j<4; j++)
            {
                arr[i, k] = 'a';
            }
        }

The below will create a 5 by 5 char[][] with a single value, J in this case.下面将创建一个具有单个值的 5 x 5 char[][] ,在本例中为J

char[][] result = Enumerable.Repeat(Enumerable.Repeat('J', 5).ToArray(),5).ToArray();

If your array is just 3 by 4 and you really need something faster than a nested loop this would do the trick.如果您的数组只有 3 x 4,并且您确实需要比嵌套循环更快的东西,那么这可以解决问题。

arr = new char[3, 4] { { 'a', 'a', 'a', 'a' }, { 'a', 'a', 'a', 'a' }, { 'a', 'a', 'a', 'a' }};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM