How to use a loop to call arrays with different names in C #

I want to assign values ​​to array1, array2, array3 ....... upto array60.right now I use the following code, since I do not know how to do this in one loop, is there any way to change the name of the array in the loop. How to do it in one cycle?

            while (array[p] != " ")
            {
                array1[p1] = array[p];
                p++;
                p1++;

            }

            while (array[p] != " ")
            {
                array2[p2] = array[p];
                p++;
                p2++;
            }

            while (array[p] != " ")
            {
                array3[p3] = array[p];
                p++;
                p3++;
            }

            while (array[p] != " ")
            {
                array4[p4] = array[p];
                p++;
                p4++;
            } 
+4
source share
3 answers

with a 2-dimensional array you can use 2 loops to populate the arrays:

int[,] arrays = new int[60,100];
for(int arraynumber = 0; arraynumber < 60; arraynumber++)
{
    for(int i = 0; i< 100;i++)
    {
        arrays[arraynumber,i] = arrays[0,i];
    }
}

you can also use an array of arrays

+3
source

try it

var sourceArray = new string[]{"1","2","3","4","5","6"};
var destArrays = new string[4,sourceArray.Length];
int innerIndex = 0;
int outerIndex = 0;

while(outerIndex<destArrays.GetLength(0))
{
    while (innerIndex<sourceArray.Length && sourceArray[innerIndex] != " ")
  {
      destArrays[outerIndex,innerIndex] = sourceArray[innerIndex];
      innerIndex++;
  }
  innerIndex = 0;
  outerIndex++;
}
+1
source

A simple solution:

string[][] arrays = new string[][] { array1, array2, array3 };

foreach (string[] arr in arrays) {
    // do staff here
}
0
source

All Articles