none
C#二维数组排列 RRS feed

  • 问题

  • 二维字符串数组如下:string[,] JianChuShiErZhi = new string[,]{
               {"建","黑"},{"除","黄"},{"满","黑"},{"平","黑"},{"定","黄"},{"执","黄"},{"破","不相当"},{"危","黄"},{"成","可用"},{"收","黑"},{"开","可用"},{"闭","不相当"}}

    现在给定二维字符串数组中的某一字符,如:平,现在我要根据这个字符 “平”,查询数组JianChuShiErZhi ,把带有 “平”的这个数组作为这个二维数组的第一个部分,并对这个字符串数组进行排列,并赋值给JianChuShiErZhi_1,排列结果如下:

    string[,] JianChuShiErZhi_1 = new string[,]{
               {"平","黑"},{"定","黄"},{"执","黄"},{"破","不相当"},{"危","黄"},{"成","可用"},{"收","黑"},{"开","可用"},{"闭","不相当"},{"建","黑"},{"除","黄"},{"满","黑"}}

    这其中的查询过程,排列过程要怎么才能实现。

    谢谢。

    2017年12月10日 16:12

全部回复

  • Hi,

    欢迎在MSDN论坛发帖。

    我觉得你可以尝试使用两个for 循环进行遍历,然后在最后一个for循环里面通过循环一维数组里面进行比较,是否含有 "平"字。 


                string[,] array = new string[3, 2];
                array[0, 0] = "平1";
                array[1, 0] = "平2";
                array[2, 0] = "平3";
                array[0, 1] = "平4";
                array[1, 1] = "2平";
                array[2, 1] = "平6";
    
                Console.WriteLine("数组的长度为{0}", array.Length);
                /*
                Console.WriteLine(array.GetLength(0));
                Console.WriteLine(array.GetLength(1));
                */
                for (int i = 0; i < array.GetLength(0); i++)
                {
                    for (int j = 0; j < array.GetLength(1); j++)
                    {
                        Console.WriteLine(array[i, j]);
                    }
                }

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年12月11日 6:16
    版主
  • 这个方法只想循环打印数组,并不能对数组进行组合。

    2018年1月1日 15:49