我有一段代码,我在其中一直在比较 2D 和 1D 数组。你能告诉我如何将结果(两个数组中都存在的元素)存储在另一个 2D 数组中吗?这是我实际上想在新的二维数组中保存“res”的代码......
namespace WindowsFormsApplication1strng_cmp
{
public partial class Form1 : Form
{
private static Dictionary<string, Position> BuildDict(string[,] symbols)
{
Dictionary<string, Position> res = new Dictionary<string, Position>();
for (int i = 0; i < symbols.GetLength(0); i++)
{
for (int j = 0; j < symbols.GetLength(1); j++)
{
res.Add(symbols[i, j], new Position(i, j));
}
}
return res;
}
struct Position
{
public int x;
public int y;
public Position(int x, int y)
{
this.x = x;
this.y = y;
}
}
private static List<string> CompareUsingBrute(string[] text, string[,] symbols)
{
List<string> res = new List<string>();
for (int x = 0; x < symbols.GetLength(0); x++)
{
for (int y = 0; y < symbols.GetLength(1); y++)
{
for (int z = 0; z < text.Length; z++)
{
if (symbols[x, y] == text[z])
res.Add(text[z]);
}
}
}
return res;
}
string[,] symbols = new string[,] { { "if", "else" }, { "for", "foreach" }, { "while", "do" } };
string[] text = new string[] { "for", "int", "in", "if", "then" };
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Dictionary<string, Position> dictionary = BuildDict(symbols);
// IndexElement[] index = BuildIndex(symbols);
foreach (string s in CompareUsingBrute(text, symbols))
{
listBox2.Items.Add("valid");
}
}
}
}