Multidimensional arrays in Java and C#

…衆ロ難τιáo~ 提交于 2019-11-26 20:39:48

问题


In C# there are 2 ways to create mutlidimensional arrays.

int[,] array1 = new int[32,32];

int[][] array2 = new int[32][];
for(int i=0;i<32;i++) array2[i] = new int[32];

I know that the first method creates a 1-dimensional array internally, and that the second method creates an array of arrays (slower access).

However in Java, there is no such thing as [,], and I see multidimensional arrays declared like this:

int[][] array3 = new int[32][32];

Since such syntax is illegal in C#, and Java has no int[,], I'm wondering if this is equivilant to array1? Or is it still an array of arrays?


回答1:


You are incorrect; jagged (nested) arrays are faster. (the CLR is optimized for them)

Java does not support true multi-dimensional arrays; that's a jagged array.
The Java syntax automatically creates all of the inner arrays; in C#, that would need a separate loop.




回答2:


It's still an array of arrays. It's just that in C# you'd have to create each subarray in a loop. So this Java:

// Java
int[][] array3 = new int[32][32];

is equivalent to this C#:

// C#
int[][] array3 = new int[32][];
for (int i = 0; i < array3.Length; i++)
{
    array3[i] = new int[32];
}

(As Slaks says, jagged arrays are generally faster in .NET than rectangular arrays. They're less efficient in terms of memory though.)




回答3:


Because people were concerned about the performance of multi-dimension vs staggered arrays in .NET, I implemented a few tests and benchmarked the results on 8k by 8k elements:

The tests were:

  1. Multi-dimensional 2D array
  2. Multi-dimensional with indices backwards (y first)
  3. Multi-dimensional with GetLength(x) instead of integer bound
  4. Staggered with backwards indicies
  5. Staggered
  6. One dimensional (size x size) with multiplication in index
  7. One dimensional with increment index

And the results:

one <> Elapsed Time: 0.543558s
two <> Elapsed Time: 0.8911516s
three <> Elapsed Time: 0.8908123s
four <> Elapsed Time: 1.1367238s
five <> Elapsed Time: 0.3039648s
six <> Elapsed Time: 0.8110969s
seven <> Elapsed Time: 0.2629394s

For fun I ran them on the WP7 emulator as well, and got similar numbers.

Code of the test function is here.




回答4:


In Java you are declaring an array of arrays.

You can see this by the following code:

int[][] arrOfArr = new int[5][];
arrOfArr[0] = new int[5];
arrOfArr[1] = new int[1];
arrOfArr[2] = new int[9];
...

int[][] arr = new int[3][3]; is just shorthand for:

int[][] arr = new int[3][];
arr[0] = new int[3];
arr[1] = new int[3];
arr[2] = new int[3];



回答5:


I was translating some Java code to C# - here is how I did the Jagged array

    //Java
    private static int grad3[][] = {{1,1,0},{-1,1,0},{1,-1,0},{-1,-1,0},{1,0,1},{-1,0,1},{1,0,-1},{-1,0,-1},{0,1,1},{0,-1,1},{0,1,-1},{0,-1,-1}};

    //C#
    private static int[,] grad3setup = { { 1, 1, 0 }, { -1, 1, 0 }, { 1, -1, 0 }, { -1, -1, 0 }, { 1, 0, 1 }, { -1, 0, 1 }, { 1, 0, -1 }, { -1, 0, -1 }, 
                                  { 0, 1, 1 }, { 0, -1, 1 }, { 0, 1, -1 }, { 0, -1, -1 } };

    private static int[][] grad3
    {
        get
        {
            int[][] grad3 = new int[12][];
            for (int i = 0; i < grad3.Length; i++)
            {
                grad3[i] = new int[3] { grad3setup[i, 0], grad3setup[i, 1], grad3setup[i, 2] };
            }
            return grad3;
        }
    }



回答6:


It is an array of arrays with the same performance tradeoffs as in C#. If you know that your array of arrays is not going to be jagged, then you can wrap it in a class to get 2-d indexing on a 1-d backing array.



来源:https://stackoverflow.com/questions/5313832/multidimensional-arrays-in-java-and-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!