简体   繁体   中英

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

i hav a piece of code in which i have been comparing 2D and 1D arrays.. can u please tell me how can i store the results (the elements present in both of the arrays) in another 2D array? here is the code i actually want to sav "res" in a new 2D array...

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");
            }


        }
    }
}

What I understand "from your question" is that you are trying to compare two arrays and storing the common elements -

Have attached output below as an image-

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);

    }
}

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM