在Java和C#多维数组 [英] Multidimensional arrays in Java and C#

查看:170
本文介绍了在Java和C#多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中有2种方式来创建mutlidimensional数组。

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];

我知道第一方法创建一个1维阵列内部,而第二种方法产生数组的数组(较慢访问)。

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 [,] ,我不知道这是否是equivilant到数组1 ?或者是它仍然数组的数组?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆