简体   繁体   中英

convert two dimensional string array to two dimensional int array

I'm trying to convert a two dimensional string array to a two dimensional int array:

int[][] inner = new int[4][];

string[][] arr = new string[4][]
{
    new string[] {"11"},
    new string[] {"12"},
    new string[] {"21"},
    new string[] {"22"}
};

for (int i = 0; i < arr.Length; i++)
{
    string name = string.Join(".", arr[i]);
    for (int j = 0; j < name.Length; j++)
    {
        inner[i][j] = Convert.ToInt32(name.Substring(j,1));
    }
}

But I'm getting the following exception:

Object reference not set to an instance of an object

at:

inner[i][j] = Convert.ToInt32(name.Substring(j,1));

将您的“内部”变量的声明更改为

int[,] inner = new int[4,2];

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM