C # delete the specified element in the two-dimensional array and change the length of the array

@ 好好 zhang 先生
Regarding the handling of C # two-dimensional arrays, I searched for information and found that it did not match with my own project.
I encountered such a problem during the LED sorting project. It is necessary to delete all (0,0) data in the two-dimensional array pre-scanned by the chip and change the reading of the array. Popularly speaking, such as Arrya [10,2] , 10 rows and 2 columns of data, assuming there are 5 (0,0) data in the array; you need to delete the 5 data and sort the original data in the original order and generate an Arryc [5,2] length array.
The following summarizes my ideas as follows:
First, the distribution of (0,0) in the original Arrya [10,2] array of known length is random. I assume that the odd index is (0,0), and the even index is not ( 0,0)
Then determine the non-zero data in the Arrya array by Arryb [10,2] number, and accept, the remaining position is temporarily vacant, and calculate the number of non-zero data
j is redefined by the calculated j value Arryc [j, 2] is an array of length j and the non-zero data in the Arryb array is transferred to Arryc [j, 2], which achieves the purpose

The original array Arrya [10,2]
Mr. Zhang
intermediate array Arryb [10,2]
is responsible for receiving the non-zero data in the original array Arrya [10,2]. Assuming that 5 are received, the remaining positions are still the values ​​at initialization. Both are (0,0)
Insert picture description here
final array Arryc [j, 2]. Through the calculation in the previous step, j = 5, then the Arryc length is defined as 5 to
Insert picture description here
this point, and the required functions are completed .
The following shows 源代码片段.

static void Main(string[] args)
        {
            //定义一个长度为10的数组Arrya, 偶数索引为0,奇数索引为非0
            int ArryLength = 10;//数组长度
            int[,] Arrya = new int[ArryLength, 2];
            Console.WriteLine("*********  Arrya数组 ****************");
            for (int i = 0; i < ArryLength; i++)
            {
                if(i%2==0)
                {
                    Arrya[i, 0] = i ;
                    Arrya[i, 1] = i+1;
                }
                else
                {
                    Arrya[i, 0] = 0;
                    Arrya[i, 1] = 0;
                }
                Console.Write("第{0}行:  ", i);
                Console.Write(Arrya[i,0] + ",");
                Console.Write(Arrya[i,1]);
                Console.WriteLine("\n");//换行输出Arryb
            }

            //定义一个数组Arryb,将Arrya中不为0的数据按顺序接收并计算为(0,0)的个数
            int j = 0 ;
            int[,] Arryb = new int[ArryLength, 2];
            Console.WriteLine("*********  Arryb数组 ****************");
            for (int i = 0; i < ArryLength; i++)
            {             
                if (Arrya[i,0] != 0 || Arrya[i,1]!= 0)
                {
                    j = j + 1;
                    Arryb[j - 1, 0] = Arrya[i, 0];
                    Arryb[j - 1, 1] = Arrya[i, 1];
                }             
            }
            for (int i = 0; i < ArryLength; i++)
            {
                Console.Write("第{0}行:   ", i);
                Console.Write(Arryb[i, 0] + ",");
                Console.Write(Arryb[i, 1]);
                Console.WriteLine("\n");
            }

            Console.WriteLine("Arrya数组的(0,0)位置是{0}: ", j);
            //通过以上的计算Arrya中不为0 的个数j个,定义长度为j的数组
            int[,] Arryc = new int[j, 2];
            Console.WriteLine("*********  Arryc数组 ****************");
            for (int i = 0; i < j; i++)
            {
                Arryc[i, 0] = Arryb[i, 0];
                Arryc[i, 1] = Arryb[i, 1];
                Console.Write("第{0}行:   " , i);
                Console.Write(Arryc[i, 0] + ",");
                Console.Write(Arryc[i, 1]);
                Console.WriteLine("\n");//换行
            }
            Console.WriteLine("Arryc数组的长度是{0}: ", j);
            Console.ReadKey();
        }
    }
Published 18 original articles · Likes0 · Visits 234

Guess you like

Origin blog.csdn.net/qq_39217004/article/details/105218345