Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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. Like for an array 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';
            }
        }
question from:https://stackoverflow.com/questions/66050333/fill-a-c-sharp-2d-array-with-some-value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
202 views
Welcome To Ask or Share your Answers For Others

1 Answer

The below will create a 5 by 5 char[][] with a single value, J in this case.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...