Reputation: 236
I want to assign values to array1,array2,array3.......upto array60.right now i am using following code as i don't know how do do it in one loop ,is there any way to change array name in loop.How to do this in one loop?
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++;
}
Upvotes: 4
Views: 223
Reputation: 9606
Try this
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++;
}
Upvotes: 1
Reputation: 2136
A simple solution:
string[][] arrays = new string[][] { array1, array2, array3 };
foreach (string[] arr in arrays) {
// do staff here
}
Upvotes: 0
Reputation: 2259
with a 2 dimentional array you can use 2 loops to fill your 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
Upvotes: 3