Multidimensional arrays in Java and C#

后端 未结 6 1450
清歌不尽
清歌不尽 2020-12-05 20:24

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]         


        
6条回答
  •  甜味超标
    2020-12-05 21:06

    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.)

提交回复
热议问题