What is the difference between:
int [][] myArray;
and
int [,] myOtherArray;
The first is a jagged array: an array where each item in the array is another array
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];
The second is a multidimensional array, aka a matrix.
int[,] array = new int[4, 2]; // create a 4 by 2 matrix
myArray
is a jagged array, or an array of arrays. Each element of myArray
is itself an int[]
.
myOtherArray
is a rectangular (or multidimensional) array - a single object containing all the data directly.
Which you should use really depends on the situation. Sometimes it can be handy to have an array for each "row" of data (with the ability to replace whole rows, and have rows with different lengths), whereas at other times it makes sense to force a uniform layout.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With