Multidimensional Arrays

Ok, my book really sucks I think.
After struggling with an example in the book, I looked in the MSDN and searched these forums.
I believe my book is wrong when it shows:
int[,] student = new in[4][3];
I tried doing it that way but it errors.
int[,] student = new int[5,4];
gives me no error and works.

I am going to go with that.

Unless someone can tell me that int[,] student = new in[4][3]; is indeed a correct format or what kind of a typing error it could be...


The following also seems to be in the wrong format:
int[][] x = { { 2, 3 }, { 4, 5 }, { 6, 7 } };

I don't think you can do it that way...



Answer this question

Multidimensional Arrays

  • 聖光飛翔

    Ok, but I still get the error array initializers can only be used in a variable or field initializer, try using a new expression instead.

    int[][] x = { { 2, 3 }, { 4, 5 }, { 6, 7 } };

    I tried this inside of main, I tried this as a class property. Still errors.
    What would x[ 1 ] be Should it result in just 4, or will it somehow contain 4,5

  • smrobin

    I prepared answer of your last post but i think you have deleted that now. Any how I would liek to copy /paste in case you need to understand!

    class ParamArrayWithReturn
    {

    public static void Main()
    {

    int[][] returned = F(3, 2, 15);

    foreach (int[] ints in returned)

    {

    foreach (int i in ints)

    {

    Console.Write(i.ToString() + ",");

    }

    Console.WriteLine();

    }

    }

    public static int[][] F(int n, int m, int value)

    {

    int[][] x = new int[3][];

    for (int i = 0; i < n; i++)

    {

    x[ i ] = new int[ m ];

    for (int j = 0; j < m; j++)

    x[ i ][ j ] = value;

    }

    return x;

    }

    "Now why would we return x here And what happens to it when it returns to the method call I am confused by that part of the example."

    you used function definition something like this:

    public static int[][] F(int n, int m, int value)

    int[][] after static and before function name, means that this function must return an int[][]. You create fucnctions to get some done work from it and most of the time it should give back the result so here int[] [] is the result of the function which may be needed by its calls. In this example Main function is its caller and see the modified main function I got the F's returned resutl back in int [][] returned in the main. So i can use it for some purpose in main.

    if you omit the return x at the end of the function F, project will not compile and compiler will give you error "Not all Code paths return value" it means when you have declared in function definitation that i'll return the value then you must have to return the same things which was written in function definitaiotn int[][] in this case...

    Sometimes we create functions and we dont want to get any result back from them and we just want that function should do some processing in itself and should not return any value so we can write function Definitation like this:

    public static void F(int n, int m, int value)

    I have replaced int[][] with void that means this fucnion will not return any thing. Generally Speaking void means emptu or nothing. Now even if you dont write return x, your code wil be compiled just fine. So it depends on your needs, that some times you want some resutl back from function and some times not.

    In you call you the function F you didnot assing the result back from the function to any int[][]; I modified it to see what result came from the function and then printed it similar to the format in which it is stored in the array!

    Remember, a function can return only one value at a time, whether it is an array, any object of class, struct even any reference or value type.

    I hope you understand now! if you face any difficult please feel freee to ask 100 times again!

    Best Regards,

    Rizwan



  • SGriffiths

    You are more than welcome any time!

    Best Regards,



  • Walter30140

    Ahhhhh, Foolios! I beg your pardon I was wrong.

    Listen again:

    int[ , ] arr1 = new int[ , ] { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; // Its Correct

    is a multidimentional array. [ , ] is used for Multidemntioanl arrays in which all rows have same number of elements in the above example all rows have 3 elements.

    If you change the above code to:

    int[ , ] arr2 = new int[ , ] { {1, 2, 3}, {4, 5, 6}, {7, 8, 9, 10} }; // Its Wrong

    Because all should have the same number of elements so this is error!

    on the other hand, if you use:

    int[][] arr1 = { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 }, new int[] { 7, 8, 9 } }; // Its Correct

    You have to tell what to place on each row

    int[][] arr1 = { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 }, new int[] { 7, 8, 9 , 4, 6, 7, 4, 4} }; // Its Correct and can contain different number of elements tan other rows

    int[][] x = { { 2, 3 }, { 4, 5 }, { 6, 7 } }; // Is in correct You have to use new int[] before each set of number in {}

    I hope this will help!

    Best Regards,



  • BhanuKiran.K

    Ok, now I almost have everything figured out with your example except one part I want to know more about.

    int[][] returned = F(3, 2, 15);

    foreach (int[] ints in returned)

    {

    foreach (int i in ints)

    I see how you have a foreach set up to completely cycle through all the elements of the jagged array. I see how it says, foreach outer array index we'll use a temp variable named ints to cycle through the array called returned. I see that outer array as int[] in the first foreach loop, the outer foreach if you will. The inner foreach loop then uses an int variable to cycle through each index of the temporary named ints array which is the inner array.

    See what you guys have all taught me right here on this forum Damned, that's awesome: I honestly thought I had no hope of understanding anything just a couple weeks ago. Thanks so much!


  • Nash Bridges

    Thanks so much for clearing that up.

  • Tailor

    oooh, I didn't understand this the first time I looked at it, but I see now what you are showing me how I can print those values out to see them.

    Ok , I am going to look at that right now...


    EDIT:

    OMG, you are brilliant. I don't know how you managed to understand what I wanted to see, but you nailed it right on the coffin.

    Thank you so much for this. I will look it over some more to understand it and pass along more questions.
    Thanks again!

  • Littletommy

    hi,

    this is multidimension Array

    int [,] x= new int[2,2];

    the best thing you can relate this with a fixed size table (2 rows , 2 columns) you can't add more than that to the table

    this is jagged Array

    int[][] y = new int[2][];

    in this array you specify the number of rows but you don't specify the number of columns

    the best word to discripe this type of arrays is " Array of Arrayes"

    int[][] x = { new int[] { 2, 3, 4 }, new int[] { 5, 6, 7, 8, 9 } };

    every row may have different dimensions like here first row has 3 columns but second row has 5 columns

    hope this helps



  • Murali Krishna K

    Foolios wrote:

    int[][] x = { new int[] { 2, 3, 4 }, new int[] { 5, 6, 7, 8, 9 } };

    ok so

    int[][] x = { { 2, 3, 4 }, { 5, 6, 7, 8, 9 } };

    would not be proper shorthand then right

    Yes it is, and this type of array is called Jagged array, Jagged array is a multidimentional array which has variable number of collumn ins each row as in your example first row contains 3 values and second rown contains 4 values!

    Best Regards,

    Rizwan



  • AlexBB

    From the above explaination It seems you have understood completely!

    foreach loop is a safe way to iterate through a collection without getting out of range from the array (avoiding Exception). It'll iterate automatically to the numbers which are in array not 1 above or less!

    Anything else needed Welcome!

    Best Regards,

    Rizwan



  • Ian Tanner

    int[][] x = { new int[] { 2, 3, 4 }, new int[] { 5, 6, 7, 8, 9 } };

    ok so

    int[][] x = { { 2, 3, 4 }, { 5, 6, 7, 8, 9 } };

    would not be proper shorthand then right


  • Multidimensional Arrays