-1

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.

    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?

5
  • 1
    The term to google is "jagged array" Commented Mar 31, 2019 at 0:56
  • Considered linq?? Commented Mar 31, 2019 at 1:03
  • I googled about it firstly, but i didn't get it therefore i asked here Commented Mar 31, 2019 at 1:04
  • i haven't used linq before, so i am not familiar with it Commented Mar 31, 2019 at 1:05
  • Possible duplicate of C# Creating an array of arrays also, Jagged Arrays (C# Programming Guide) Commented Mar 31, 2019 at 1:08

1 Answer 1

1

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]
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Argument out of index exception

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.