Declare 3d array with dynamic 3th dimension in C# - c#

I need to declare 3d array variable but can't.
int[][][] ary = new int[5][2][];
ary[0,0] = new int[20];
ary[0,1] = new int[3];
Could you please help me!
Thanks in advance
Hamid

int[,][] ary = new int[5,2][];
declares a 2D array of int[] objects and initializes it. Use
ary[0, 0] = new int[10];
ary[0, 0][0] = 42;
to access elements.
Note that in C#, multidimensional arrays are different from arrays of arrays. That is, int[][][] is a single dimensional array of single dimensional arrays of single dimensional arrays of integers while int[,,] is a three dimensional array of integers.

Related

Why am I getting "Cannot implicitly convert type `char' to `char[][]'" here? [duplicate]

This question already has answers here:
Differences between a multidimensional array "[,]" and an array of arrays "[][]" in C#?
(12 answers)
Closed 9 years ago.
double[][] ServicePoint = new double[10][9]; // <-- gives an error (1)
double[,] ServicePoint = new double[10,9]; // <-- ok (2)
What's their difference? (1) yields an error, what's the reason?
And
double d = new double[9]
ServicePoint[0] = d;
using (2) will prompt an error. Why?
One is an array of arrays, and one is a 2d array. The former can be jagged, the latter is uniform.
That is, a double[][] can validly be:
double[][] x = new double[5][];
x[0] = new double[10];
x[1] = new double[5];
x[2] = new double[3];
x[3] = new double[100];
x[4] = new double[1];
Because each entry in the array is a reference to an array of double. With a jagged array, you can do an assignment to an array like you want in your second example:
x[0] = new double[13];
On the second item, because it is a uniform 2d array, you can't assign a 1d array to a row or column, because you must index both the row and column, which gets you down to a single double:
double[,] ServicePoint = new double[10,9];
ServicePoint[0]... // <-- meaningless, a 2d array can't use just one index.
UPDATE:
To clarify based on your question, the reason your #1 had a syntax error is because you had this:
double[][] ServicePoint = new double[10][9];
And you can't specify the second index at the time of construction. The key is that ServicePoint is not a 2d array, but an 1d array (of arrays) and thus since you are creating a 1d array (of arrays), you specify only one index:
double[][] ServicePoint = new double[10][];
Then, when you create each item in the array, each of those are also arrays, so then you can specify their dimensions (which can be different, hence the term jagged array):
ServicePoint[0] = new double[13];
ServicePoint[1] = new double[20];
In the first instance you are trying to create what is called a jagged array.
double[][] ServicePoint = new double[10][9].
The above statement would have worked if it was defined like below.
double[][] ServicePoint = new double[10][]
what this means is you are creating an array of size 10 ,that can store 10 differently sized arrays inside it.In simple terms an Array of arrays.see the below image,which signifies a jagged array.
http://msdn.microsoft.com/en-us/library/2s05feca(v=vs.80).aspx
The second one is basically a two dimensional array and the syntax is correct and acceptable.
double[,] ServicePoint = new double[10,9];//<-ok (2)
And to access or modify a two dimensional array you have to pass both the dimensions,but in your case you are passing just a single dimension,thats why the error
Correct usage would be
ServicePoint[0][2] ,Refers to an item on the first row ,third column.
Pictorial rep of your two dimensional array
double[][] are called jagged arrays , The inner dimensions aren’t specified in the declaration. Unlike a rectangular array, each inner array can be an arbitrary length. Each inner array is implicitly initialized to null rather than an empty array. Each inner array must be created manually: Reference [C# 4.0 in nutshell The definitive Reference]
for (int i = 0; i < matrix.Length; i++)
{
matrix[i] = new int [3]; // Create inner array
for (int j = 0; j < matrix[i].Length; j++)
matrix[i][j] = i * 3 + j;
}
double[,] are called rectangular arrays, which are declared using commas to separate each dimension. The following piece of code declares a rectangular 3-by-3 two-dimensional array, initializing it with numbers from 0 to 8:
int [,] matrix = new int [3, 3];
for (int i = 0; i < matrix.GetLength(0); i++)
for (int j = 0; j < matrix.GetLength(1); j++)
matrix [i, j] = i * 3 + j;
double[,] is a 2d array (matrix) while double[][] is an array of arrays (jagged arrays) and the syntax is:
double[][] ServicePoint = new double[10][];
double[][] is an array of arrays and double[,] is a matrix. If you want to initialize an array of array, you will need to do this:
double[][] ServicePoint = new double[10][]
for(var i=0;i<ServicePoint.Length;i++)
ServicePoint[i] = new double[9];
Take in account that using arrays of arrays will let you have arrays of different lengths:
ServicePoint[0] = new double[10];
ServicePoint[1] = new double[3];
ServicePoint[2] = new double[5];
//and so on...

Multi-Dimensional Array - Array of arrays?

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.

Determining size of an array of arrays in C#?

I am creating an array of arrays such that:
var arrNewArray = new string[arrOldArray.Length][7];
arrOldArray is an array of arrays such that it's [X][4], meaning the length of the 1st array or "outside" array can change, but the length of the "inside" array is ALWAYS 4, or hold 4 strings ([0][1][2][3]).
Why won't the compiler accept my statement above?
Essentially, I'm trying to take arrOldArray and expand it, or add a few more "columns" by increasing the [4] in the old array to a [7] in the new array and then copy the contents over. Perhaps I'm not doing it the best/efficient way, so any guidance would be appreciated thanks.
I think you want a two dimensional array:
var arrNewArray = new string[arrOldArray.Length, 7];
You would access it like this: arrNewArray[x, y].
This is better than a jagged array, because it clearly communicates that the number of "columns" is the same for every row.
If you want to continue using a jagged array, you need to do it like this:
var arrNewArray = new string[arrOldArray.Length][];
for(int i = 0; i < arrOldArray.Length; ++i)
arrNewArray[i] = new string[7];
The reason for this convoluted way is: With a jagged array, each "row" can have a different number of "columns". A short-hand syntax for the case where each "row" has the same number of "columns" doesn't exist. That's why your code doesn't compile.
A jagged array is essential an array of arrays, so you need to create a new array instance for each "row" of the outer array and explicitly assign it. That's what the for loop is doing.
You can't use Array.Copy with jagged arrays. Each child-array is it's own instance and Array.Copy doesn't make a deep copy, it merely copies the references from one array to another. The effect would be, that both arrays would point to the same items and changing an item in one array would be seen from the other.
You are not creating the jagged array properly. The proper way is to create the first dimension of the jagged array and then loop through the items of the first dimension to create the nested arrays and copy the data from the old arrays. Here's an example:
int newSize = 7;
string[][] newArray = new string[oldArray.Length][];
for (int i = 0; i < oldArray.Length; i++)
{
newArray[i] = new string[newSize];
Array.Copy(oldArray[i], newArray[i], oldArray[i].Length);
}
You would be wanting
var arrNewArray = new string[arrOldArray.Length, 7];
var arrNewArray = new[] {new string[7]};//array of arrays
var arrNewArray = new string[arrOldArray.Length, 7];//two-dimensional array
Using Linq:
int[][] jaggedArray2 = new int[][]
{
new int[] {1,3,5,7,9},
new int[] {0,2,4,6},
new int[] {11,22}
};
int length = jaggedArray.Sum(a => a.Length);
I don't believe what you're asking is directly possible. Because the syntax that you are using is for a jagged array, and what you are doing is effectively asking it to create a multi-dimensional array.
The syntax is confusing since it reads like what you really want is a multi-dimensional array (although I'm aware that's not the case.)
I don't believe you could store your arrays in the newly allocated array either due to a size change. You would need to build a custom copy method to move the data into the larger array.

Multidimensional Array [][] vs [,] [duplicate]

This question already has answers here:
Differences between a multidimensional array "[,]" and an array of arrays "[][]" in C#?
(12 answers)
Closed 9 years ago.
double[][] ServicePoint = new double[10][9]; // <-- gives an error (1)
double[,] ServicePoint = new double[10,9]; // <-- ok (2)
What's their difference? (1) yields an error, what's the reason?
And
double d = new double[9]
ServicePoint[0] = d;
using (2) will prompt an error. Why?
One is an array of arrays, and one is a 2d array. The former can be jagged, the latter is uniform.
That is, a double[][] can validly be:
double[][] x = new double[5][];
x[0] = new double[10];
x[1] = new double[5];
x[2] = new double[3];
x[3] = new double[100];
x[4] = new double[1];
Because each entry in the array is a reference to an array of double. With a jagged array, you can do an assignment to an array like you want in your second example:
x[0] = new double[13];
On the second item, because it is a uniform 2d array, you can't assign a 1d array to a row or column, because you must index both the row and column, which gets you down to a single double:
double[,] ServicePoint = new double[10,9];
ServicePoint[0]... // <-- meaningless, a 2d array can't use just one index.
UPDATE:
To clarify based on your question, the reason your #1 had a syntax error is because you had this:
double[][] ServicePoint = new double[10][9];
And you can't specify the second index at the time of construction. The key is that ServicePoint is not a 2d array, but an 1d array (of arrays) and thus since you are creating a 1d array (of arrays), you specify only one index:
double[][] ServicePoint = new double[10][];
Then, when you create each item in the array, each of those are also arrays, so then you can specify their dimensions (which can be different, hence the term jagged array):
ServicePoint[0] = new double[13];
ServicePoint[1] = new double[20];
In the first instance you are trying to create what is called a jagged array.
double[][] ServicePoint = new double[10][9].
The above statement would have worked if it was defined like below.
double[][] ServicePoint = new double[10][]
what this means is you are creating an array of size 10 ,that can store 10 differently sized arrays inside it.In simple terms an Array of arrays.see the below image,which signifies a jagged array.
http://msdn.microsoft.com/en-us/library/2s05feca(v=vs.80).aspx
The second one is basically a two dimensional array and the syntax is correct and acceptable.
double[,] ServicePoint = new double[10,9];//<-ok (2)
And to access or modify a two dimensional array you have to pass both the dimensions,but in your case you are passing just a single dimension,thats why the error
Correct usage would be
ServicePoint[0][2] ,Refers to an item on the first row ,third column.
Pictorial rep of your two dimensional array
double[][] are called jagged arrays , The inner dimensions aren’t specified in the declaration. Unlike a rectangular array, each inner array can be an arbitrary length. Each inner array is implicitly initialized to null rather than an empty array. Each inner array must be created manually: Reference [C# 4.0 in nutshell The definitive Reference]
for (int i = 0; i < matrix.Length; i++)
{
matrix[i] = new int [3]; // Create inner array
for (int j = 0; j < matrix[i].Length; j++)
matrix[i][j] = i * 3 + j;
}
double[,] are called rectangular arrays, which are declared using commas to separate each dimension. The following piece of code declares a rectangular 3-by-3 two-dimensional array, initializing it with numbers from 0 to 8:
int [,] matrix = new int [3, 3];
for (int i = 0; i < matrix.GetLength(0); i++)
for (int j = 0; j < matrix.GetLength(1); j++)
matrix [i, j] = i * 3 + j;
double[,] is a 2d array (matrix) while double[][] is an array of arrays (jagged arrays) and the syntax is:
double[][] ServicePoint = new double[10][];
double[][] is an array of arrays and double[,] is a matrix. If you want to initialize an array of array, you will need to do this:
double[][] ServicePoint = new double[10][]
for(var i=0;i<ServicePoint.Length;i++)
ServicePoint[i] = new double[9];
Take in account that using arrays of arrays will let you have arrays of different lengths:
ServicePoint[0] = new double[10];
ServicePoint[1] = new double[3];
ServicePoint[2] = new double[5];
//and so on...

What is the difference between these two ways of declaring an array? [duplicate]

This question already has answers here:
Differences between a multidimensional array "[,]" and an array of arrays "[][]" in C#?
(12 answers)
Closed 1 year ago.
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.
I found the best way to understand it was to see a graphical representation of it =)
int[][] jagged = new int[3][];
jagged[0] = new int[1];
jagged[1] = new int[2];
jagged[2] = new int[4];
will look like this
[0] - [0]
[1] - [0][1]
[2] - [0][1][2][3]
while a two+ dimensional
int[,] twodimensional = new int[3,4]
will look like this
[0,0][0,1][0,2][0,3]
[1,0][1,1][1,2][1,3]
[2,0][2,1][2,2][2,3]
The first is a jagged array, the other multi-dimensional - the difference is that the elements of a jagged array can be of different dimensions and sizes.
Jagged array:
int [][] myArray;
Rectangular array:
int [,] myOtherArray;
Quote Comparing Rectangular and Jagged Arrays :
The structure of rectangular and
jagged arrays is significantly
different.
One-dimensional arrays have specific
instructions in the CIL that allow
them to be optimized for performance.
Rectangular arrays do not have these
instructions, and are not optimized to
the same level. Because of this, it
can sometimes be more efficient to use
jagged arrays of one-dimensional
arrays—which can be optimized—than
rectangular arrays, which cannot. On
the other hand, the programming
complexity can be less for a
rectangular array because it can be
treated as a single unit, rather than
an array of arrays.
This has to be a duplicate. One is a jagged array, one is a two-dimensional array. You should be able to take it from there.
Both statements declare uninitialized multi-dimensional arrays of ints. The first is a jagged array and the second is 2-dimensional.
You can initialize the 2-dimensional array at the same time as you declare it as follows:
int[,] array4 = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
OR,
int[,] array = new int[4, 2];
Refer to the official documentation:

Categories

Resources