I’ve created a multidimensional array and want to set the entire inner array equal to a separate (single dimensional) array. How can I do this, besides going through each position in the arrays and setting grid[row][val] = inputNums[val]?
int[,] grid = new int[20,20];
// read a row of space-deliminated integers, split it into its components
// then add it to my grid
string rowInput = "";
for (int row = 0; (rowInput = problemInput.ReadLine()) != null; row++) {
int[] inputNums = Array.ConvertAll(rowInput.Split(' '), (value) => Convert.ToInt32(value))
grid.SetValue(inputNums , row); // THIS LINE DOESN'T WORK
}
The specific error I’m getting is:
“Arguement Exception Handled: Array was not a one-dimensional array.”
You are mixing “jagged” arrays (arrays of arrays) with multidimensional arrays. What you want to use is probably jagged arrays (because no one in his right mind would want to use md arrays 🙂 )
A md array is a single monolithical “object” with many cells. Arrays of arrays are instead many objects: for a 2d jagged array, one object is for the row “structure” (the external container) and one is for each “row”. So in the end with a jagged array you must do a single
new int[20, 20], with a jagged array you must do anew int[20][]that will create 20 rows and 20myArray[x] = new int[20](with x = 0…19) one for each row. Ah… I was forgetting: a jagged array can be “jagged”: each “row” can have a different number of “columns”. (everything I told you is valid even for 3d and *d arrays 🙂 You only have to scale it up)