簡體   English   中英

比較后如何動態地將項目添加到2D數組中? C#Windows窗體

[英]how to add items to a 2D array dynamically after comparing? c# windows forms

我有一段代碼,我一直在比較2D和1D數組..您能告訴我如何將結果(兩​​個數組中都存在的元素)存儲在另一個2D數組中嗎? 這是我實際上想要在新的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");
            }


        }
    }
}

我從您的問題中了解到的是,您正在嘗試比較兩個數組並存儲常見元素-

在下面附加了輸出作為圖像-

class Program
{
    static void Main(string[] args)
    {
        String[] array1 = {"a","b","c","d","e","f"};
        String[,] array2 = new String[,] {{"a","b","c","d"},{"d","e","f","g"}};

        List<String> array1AsList = array1.OfType<String>().ToList();
        List<String> array2AsList = array2.OfType<String>().ToList();

        // referring from **Ed S comments below**. Instead of using OFType(...)
        // alternatively you can use the List<>().. ctor to convert an array to list
        // although not sure for 2D arrays

        var common = array1AsList.Intersect(array2AsList);

    }
}

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM