简体   繁体   English

访问C#的数组数组

[英]Array of arrays accessing C#

I have four arrays each of size two, and i want to put them all in one array, so the when it J is = 1, it should use the first array, if J = 2, it should access the second array and so on. 我有四个大小均为2的数组,我想将它们全部放在一个数组中,所以当J为= 1时,它应该使用第一个数组,如果J = 2,则应该访问第二个数组,依此类推。 。

    char[] p1Arr = new char[2];
            char[] p2Arr = new char[2];
            char[] p3Arr = new char[2];
            char[] p4Arr = new char[2];
            char[][] pArrays = new char[][] {p1Arr, p2Arr, p3Arr, p4Arr};   //i tried to create this array of the four arrays

//The four arrays values        
            p1First = p1.Substring(0, 1);
            p1Arr[0] = Convert.ToChar(p1First);
            p1Second = p1.Substring(1, 1);
            p1Arr[1] = Convert.ToChar(p1Second);

            p2First = p2.Substring(0, 1);
            p2Arr[0] = Convert.ToChar(p2First);
            p2Second = p2.Substring(1, 1);
            p2Arr[1] = Convert.ToChar(p2Second);


            p3First = p3.Substring(0, 1);
            p3Arr[0] = Convert.ToChar(p3First);
            p3Second = p3.Substring(1, 1);
            p3Arr[1] = Convert.ToChar(p3Second);

            p4First = p4.Substring(0, 1);
            p4Arr[0] = Convert.ToChar(p4First);
            p4Second = p4.Substring(1, 1);
            p4Arr[1] = Convert.ToChar(p4Second);

           //Loop on the four arrays and their two values
            for (int j = 0; j < 4; j++)
            {
                pArrays[][] = pArrays[0][]; //my problem is here, how can i access each array??

                //get the first array....fourth.
                for (int i = 0; i < 2; i++)
                {
                    //loop on the values of the first array...fourth.
                    if (i == 0)
                    {
                        if (pArrays[j][i] == 0)
                        {
                            row = 0;
                        }
                   ........
                  }

I can't get to access each array in the array of arrays, how can i fix that? 我无法访问数组数组中的每个数组,我该如何解决?

You could probably simplify what you are doing 您可能可以简化您的工作

char[][] pArrays =
   {
      p1.ToCharArray(0, 2),
      p2.ToCharArray(0, 2),
      p3.ToCharArray(0, 2),
      p4.ToCharArray(0, 2)
   };

// to looped the arrays
for (int j = 0; j < 4; j++)
{
   // to access the original array
   var originalArray = pArrays[j];

   // to loop the elements
   for (int i = 0; i < 2; i++)
   {
      // to access the elements
      // pArrays[j][i]
      // originalArray[I]
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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