I would like to know if I can create a 2D array from two arrays, something like the following:
string[] array1 = new string[] { "test", "test2" };
string[] array2 = new string[] { "TEST", "TEST2" };
string[,] array3 = new string[,] { array1, array2};
Is there any way to do this?
Considering that you're using a two-dimensional array the closest you can get is by doing:
string[,] array3 = new string[,] {{"test","test2" }, {"TEST","TEST2"}};
Though this might seem tempting:
string[,] array3 = new string[,] { array1, array2 };
it simply would not compile. if that's what you want then you'll need to use a jagged array rather than a multidimensional array.
No I don't think so but you can do it manually like this:
string[] array1 = { "test", "Test2" };
string[] array2 = { "TEST", "TEST2" };
int arrayCount = 2;
string[,] array3 = new string[2, array1.Length];
for (int i = 0; i < array1.Length; i++)
{
array3[0, i] = array1[i];
array3[1, i] = array2[i];
}
This only works if all arrays have the same lenght.
Or you can use a jagged array:
string[] array1 = { "test", "Test2" };
string[] array2 = { "TEST", "TEST2" };
string[][] array3 = { array1, array2 };
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.