Interesting difference here, can the experts explain this?



  • Hello

    First, have a look at this C# code.. I am creating a 3 dimensional array and looping thru all its elements.

                int[, ,] myarray1 = new int[3, 2, 4]
                                    {
                                     { { 1, 2 , 10, 5}, { 3, 4, 0, 9 } }
                                    ,{ { 5, 6,4, 5 }, { 7, 8, 14, 9 }}
                                    ,{ { 6, 6, 2, 58}, { 2, 5, 77, 5 } }
                                    };

                foreach (int x in myarray1)
                    Console.WriteLine(x);



    Seems fine doesnt it? Have a look at it again carefully... and then look at the Java code below. Notice the difference in foreach behavior.


    int[][][] array =     {
                                { { 1, 2 , 10, 5}, { 3, 4, 0, 9 } }
                                ,{ { 5, 6,4, 5 }, { 7, 8, 14, 9 }}
                                ,{ { 6, 6, 2, 58}, { 2, 5, 77, 5 } }
                               };

            for(int[][] x : array)
                for(int[] y : x)
                    for(int number : y)
                        System.out.println(number);




    Can someone explain this? The Java version is more mathematically correct, but then again the C# version is more "clean". Personally, I prefer the Java version because thats how it really should be... can someone tell me how I can achieve the similar thing in C#? And more importantly, what is C# doing "behind the scenes" that is different than Java.
     



  • @GizmoC said:




                foreach (int x in myarray1)
                    Console.WriteLine(x);



            for(int[][] x : array)
                for(int[] y : x)
                    for(int number : y)
                        System.out.println(number);




    Can someone explain this? The Java version is more mathematically correct, but then again the C# version is more "clean". Personally, I prefer the Java version because thats how it really should be... can someone tell me how I can achieve the similar thing in C#? And more importantly, what is C# doing "behind the scenes" that is different than Java.


    IINAC#P but I'd wager the difference lies in how you delimit the set. In your C# snippet you've written foreach (int...) while in the Java version you've written for (int[][]), therefore you've specified different datatypes, or "chunks", to retrieve. Perhaps you can specify a different datatype in C#, maybe something similar to

    foreach (int[,]) 
    foreach (int[])
    foreach (int)
    {
    //something nifty
    };


  • @Mikademus said:

    foreach (int[,]) 
    foreach (int[])
    foreach (int)
    {
    //something nifty
    };

    Tried that already. Syntax error.
     



  • The difference is that C# supports a "true" multidimensional array, while Java does not. In Java, if you want a multidimensional array, you must use an array of arrays (to be more precise: an array of references to an array). That means that while the "base" array is a contiguous list, each element of the array can point to a different location in memory, leading to "scattered" data.

    In a C# multidimensional array every element of the array is laid out contiguously and ordered by dimension, allowing you to iterate over every element.

    Check out this section of the wikipedia Array article:

    Note the types of multidimensional arrays: row-major order, column-major order, and arrays of arrays. Java only supports the array of arrays type. C# supports both the array of arrays type *and* the row-major order. Or perhaps it's column-major order -- I forget which.

    Next, check out this article on the difference between C# and Java arrays:



  • You have not grokked the difference between an n-dimensional array and an array of arrays. The Java array is:

    T myarray, where T=U[], where U = V[], where V = W[], where W = int - note that types T and U are 1-dimensional arrays of objects and V is a 1-dimensional array of ints

    while the C# array is:

    V[,,] (3-dimensional!), where V = int.

    Not that I know C#, but I still can guess :P



  • That's because you can use both in C#

                 int[, ,] myarray1 = new int[3, 2, 4]
                                    {
                                     { { 1, 2 , 10, 5}, { 3, 4, 0, 9 } }
                                    ,{ { 5, 6,4, 5 }, { 7, 8, 14, 9 }}
                                    ,{ { 6, 6, 2, 58}, { 2, 5, 77, 5 } }
                                    };

    and

                int[][][] myarray1 = new int[][][]
                                    {
                                     { { 1, 2 , 10, 5}, { 3, 4, 0, 9 } }
                                    ,{ { 5, 6,4, 5 }, { 7, 8, 14, 9 }}
                                    ,{ { 6, 6, 2, 58}, { 2, 5, 77, 5 } }
                                    };



  • @XIU said:

                int[][][] myarray1 = new int[][][]
                                    {
                                     { { 1, 2 , 10, 5}, { 3, 4, 0, 9 } }
                                    ,{ { 5, 6,4, 5 }, { 7, 8, 14, 9 }}
                                    ,{ { 6, 6, 2, 58}, { 2, 5, 77, 5 } }
                                    };

     Oops that should have been:

    <FONT size=2>

    </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2>[][][] myarray1 = </FONT><FONT color=#0000ff size=2>new</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2>[][][]
    {
    </FONT><FONT color=#0000ff size=2>new</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2>[][] { </FONT><FONT color=#0000ff size=2>new</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2>[] { 1, 2 , 10, 5}, </FONT><FONT color=#0000ff size=2>new</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2>[] { 3, 4, 0, 9 } }
    ,</FONT><FONT color=#0000ff size=2>new</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2>[][] { </FONT><FONT color=#0000ff size=2>new</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2>[] { 5, 6,4, 5 }, </FONT><FONT color=#0000ff size=2>new</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2>[] { 7, 8, 14, 9 }}
    ,</FONT><FONT color=#0000ff size=2>new</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2>[][] { </FONT><FONT color=#0000ff size=2>new</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2>[] { 6, 6, 2, 58}, </FONT><FONT color=#0000ff size=2>new</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2>[] { 2, 5, 77, 5 } }
    };

    </FONT>

    Which is why they use [,,] mostly.



  • I think C# does what C and C++ do (though I don't know for sure and could be wrong):

         [code]int[3][3] a = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };[/code]
    is really the same as:
         [code]int[9] a = {1, 2, 3, 4, 5, 6, 7, 8, 9};[/code]

    except for the way that you index it using [].

     

     



  • @reed said:

    I think C# does what C and C++ do (though I don't know for sure and could be wrong):

         [code]int[3][3] a = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };[/code]
    is really the same as:
         [code]int[9] a = {1, 2, 3, 4, 5, 6, 7, 8, 9};[/code]

    except for the way that you index it using [].

    Er.... that's neither valid C# code, nor valid C/C++ code.  I can see what you meant, though, but I think you're on the wrong track.

    In C#, the elements of a multidimensional array *are* all next to each other, just as if they had been laid out in a single array, and currently they're laid out as you specified (which, incidentally, is row-major order, making it compatible with C/C++). However, C# could easily change to column-major order, and elements would be laid out like this: {1,4,7,2,5,8,3,6,9}, making it compatible with Fortran and Matlab. In fact, MS should consider allowing the programmer to do it either way, since the arithmetic for indexing is pretty much the same either way.

    Regardless, in C#, you *cannot* specify an array as multidimensional (using int [,] a = {...}) and then access it as if it were a single-dimension array (i.e. using a[i]). It won't compile.


Log in to reply