从字典创建字符串数组 [英] Create String Array from Dictionary
本文介绍了从字典创建字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好
我有以下内容:
IDictionary<String, IList<String>> data;
推荐答案
以下内容可能会为您解决此问题或给您一些想法:
The following might either solve this for you or give you some ideas:
using System;
using System.Collections.Generic;
namespace MatrixLambda
{
class Program
{
static void Main(string[] args)
{
// Load test data.
IDictionary<String, IDictionary<String, IList<String>>> data = new Dictionary<String, IDictionary<String, IList<String>>>();
data.Add("A", new Dictionary<String, IList<String>>());
data["A"].Add("AX", new List<String>());
data["A"]["AX"] = new string[] { "Hello", "World" };
data.Add("B", new Dictionary<String, IList<String>>());
data["B"].Add("BY", new List<String>());
data["B"]["BY"] = new string[] { "Good", "Evening" };
// Here is the lambda for the function that determines what goes in each cell given zero-based row and col index.
Func<int, int, string> f = (row, col) => col == 0 ? data["A"]["AX"][row] : data["B"]["BY"][row];
String[,] result = new string[2, 2];
// Apply function to determine what goes in each cell.
for (int row = 0; row <= result.GetUpperBound(0); row++)
{
for (int col = 0; col <= result.GetUpperBound(1); col++)
{
result[row, col] = f(row, col);
}
}
// Print for diagnostic.
for (int row = 0; row <= result.GetUpperBound(0); row++)
{
for (int col = 0; col <= result.GetUpperBound(1); col++)
{
if (col != 0)
Console.Write(' ');
Console.Write(result[row, col]);
}
Console.WriteLine();
}
}
}
}
这篇关于从字典创建字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文