Java 和 C# 中的多维数组 [英] Multidimensional arrays in Java and C#
问题描述
在 C# 中有两种方法可以创建多维数组.
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).
但是在Java中,没有[,]这样的东西,我看到多维数组是这样声明的:
However in Java, there is no such thing as [,], and I see multidimensional arrays declared like this:
int[][] array3 = new int[32][32];
由于这种语法在 C# 中是非法的,而 Java 没有 int[,]
,我想知道这是否与 array1
等效?还是还是数组数组?
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?
推荐答案
你错了;锯齿状(嵌套)数组更快.(CLR 已针对它们进行了优化)
You are incorrect; jagged (nested) arrays are faster. (the CLR is optimized for them)
Java 不支持真正的多维数组;这是一个锯齿状的数组.
Java 语法自动创建所有内部数组;在 C# 中,这需要一个单独的循环.
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 和 C# 中的多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!