Java和c#中的多维数组

时间:2022-05-28 01:28:19

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

在c#中有两种创建多维数组的方法。

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:

但是在Java中,没有[,]这样的东西,我看到多维数组是这样声明的:

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?

由于c#中这种语法是非法的,并且Java没有int[,],我想知道这是否与array1相同?还是数组的数组?

6 个解决方案

#1


7  

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

你是不正确的;交错(嵌套)数组更快。(CLR为它们优化)

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.

Java不支持真正的多维数组;这是一个多重数组。Java语法自动创建所有内部数组;在c#中,这需要一个单独的循环。

#2


7  

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:

由于人们对。net中的多维数组和交错数组的性能感到担忧,我进行了一些测试,并对8k * 8k元素的结果进行了基准测试:

The tests were:

测试:

  1. Multi-dimensional 2D array
  2. 多维二维数组
  3. Multi-dimensional with indices backwards (y first)
  4. 多维,指数向后(y优先)
  5. Multi-dimensional with GetLength(x) instead of integer bound
  6. 使用GetLength(x)而不是整数绑定的多维度
  7. Staggered with backwards indicies
  8. 与向后indicies交错
  9. Staggered
  10. 交错
  11. One dimensional (size x size) with multiplication in index
  12. 一维(尺寸为x大小)的乘法索引。
  13. One dimensional with increment index
  14. 一维增量指标

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.

为了好玩,我也在WP7模拟器上运行了它们,得到了类似的数字。

Code of the test function is here.

测试函数的代码在这里。

#3


6  

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:

它仍然是数组的数组。在c#中,你必须在循环中创建每个子数组。所以这个Java:

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

is equivalent to this C#:

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

(正如Slaks所说,在。net中,交错数组通常比矩形数组要快。但它们在内存方面效率较低。

#4


3  

In Java you are declaring an array of arrays.

在Java中,您正在声明数组的数组。

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:

[][] arr =新的[3][3];只是缩写:

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

#5


1  

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

我翻译了一些Java代码到c# -下面是我如何处理交错数组

    //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


0  

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.

它是一个数组,具有与c#相同的性能权衡。如果您知道数组的数组不会有锯齿,那么您可以将它封装在一个类中,以便在一个一维支持数组上获得二维索引。

#1


7  

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

你是不正确的;交错(嵌套)数组更快。(CLR为它们优化)

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.

Java不支持真正的多维数组;这是一个多重数组。Java语法自动创建所有内部数组;在c#中,这需要一个单独的循环。

#2


7  

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:

由于人们对。net中的多维数组和交错数组的性能感到担忧,我进行了一些测试,并对8k * 8k元素的结果进行了基准测试:

The tests were:

测试:

  1. Multi-dimensional 2D array
  2. 多维二维数组
  3. Multi-dimensional with indices backwards (y first)
  4. 多维,指数向后(y优先)
  5. Multi-dimensional with GetLength(x) instead of integer bound
  6. 使用GetLength(x)而不是整数绑定的多维度
  7. Staggered with backwards indicies
  8. 与向后indicies交错
  9. Staggered
  10. 交错
  11. One dimensional (size x size) with multiplication in index
  12. 一维(尺寸为x大小)的乘法索引。
  13. One dimensional with increment index
  14. 一维增量指标

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.

为了好玩,我也在WP7模拟器上运行了它们,得到了类似的数字。

Code of the test function is here.

测试函数的代码在这里。

#3


6  

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:

它仍然是数组的数组。在c#中,你必须在循环中创建每个子数组。所以这个Java:

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

is equivalent to this C#:

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

(正如Slaks所说,在。net中,交错数组通常比矩形数组要快。但它们在内存方面效率较低。

#4


3  

In Java you are declaring an array of arrays.

在Java中,您正在声明数组的数组。

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:

[][] arr =新的[3][3];只是缩写:

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

#5


1  

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

我翻译了一些Java代码到c# -下面是我如何处理交错数组

    //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


0  

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.

它是一个数组,具有与c#相同的性能权衡。如果您知道数组的数组不会有锯齿,那么您可以将它封装在一个类中,以便在一个一维支持数组上获得二维索引。