user6048670 user6048670 - 1 year ago 120
C# Question

How can I initialize a 2-d C# array to all of the same value?

What I want to do is described in the comment below. How can I do, efficiently?

using System;
using System.Collections.Generic;
using System.IO;
class Solution {
static void Main(String[] args) {
int n = Int32.Parse(Console.ReadLine());
bool[][] flags = new bool[26][n]; // I want this to be a 26 x n array of false values
for(int k = 0; k < n; ++k)
string line = Console.WriteLine();
for(int i = 0; i < line.Length; ++i)
flags[(int)line[i] - (int)'a'] = true;
int gems = flags.Count(arr => arr.Count(j => j == true) == arr.Length);

Answer Source

Looks like you are confused between Multidimensional arrays and Jagged Array (Array of Arrays).

If you are looking for 2d Array, you could simply do this.

bool[,] flags = new bool[26,n]; 
bool[,] flags = new bool[,]; 
bool[,] flags = new bool[26,]; 

If it is Jagged Arrays, you could do this.

bool[][] flags1 = new bool[26][];
flags1[0] = new bool[n];  // since you want n elements 
flags1[1] = new bool[n];

// initialization
flags1[0] = new bool[] {true, false};
flags1[1] = new bool[] {false};