I have a string containing numbers as a 2D matrix. I'm trying to use Split
function to split the contents of a string
into an array. So, when I do:
String[] subStrs = new String[20];
subStrs = str.Split('\n');
The code above works fine. However, when I try to create a 2D array and try to populate sub-arrays using the same way:
String[,] numbers = new String[20,20];
for (int i = 0; i < subStrs.Length; i++ )
{
numbers[i] = subStrs[i].Split(' '); //Error
}
I get the following compiler error:
Wrong number of indices inside []; expected 2.
If 2D array is really an array of arrays, then why is the statement numbers[i] = subStrs[i].Split(' ');
illegal?
PS : I do know that I can use a nested loop instead to populate numbers
. I'm just curious why can't I use the above method?
If 2D array is really an array of arrays
It's not. A 2D array is just that, a 2D array.
An array of arrays is an array of arrays:
string[][]
If you have an array of arrays, then the item at each index of the outer array is another array. If you have a 2D array then both dimensions are needed to get at a value, which is itself the value of the array, not another dimension.
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.