我如何获得内部数组的长度在C#中的二维数组? [英] How do I get the inner array's length in a 2d array in C#?

查看:191
本文介绍了我如何获得内部数组的长度在C#中的二维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我创建了一个锯齿形二维数组,像这样:

Let's say I create a jagged 2d array like so:

public static char[,] phoneLetterMapping = { {'0'}, {'1'}, {'A', 'B', 'C'} };

现在,给定阵列的第一索引,我想确定的内阵列的长度

Now, given the first index of the array, I would like to determine the length of the inner array.

我希望能够做这样的事情:

I would like to be able to do something like:

phoneLetterMapping[2].length

我能做到这一点在Java中。但是,当我在[] []二维数组的第一个括号中键入智能感知菜单没有返回数组的正常成员。

I can do that in Java. But the Intellisense menu doesn't return the normal members of an array when I type in the first bracket of the [][] 2d array.

我如何得到我的二维数组的数组内的长度在C#?

How do I get the inner array lengths of my 2d array in C#?

推荐答案

在贴甚至不会在C#中,这是你的主要问题,编译声明。

The declaration you posted won't even compile in C#, which is your main issue.

您已经声明,使用的char [,] A 2D,非交错数组,但你要实例化的右侧交错数组=用 {{0},{1},{'A','B','C'}}

You've declared, using char[,] a 2D, non-jagged array, but you're trying to instantiate a jagged array on the right side of the "=" by using { {'0'}, {'1'}, {'A', 'B', 'C'} }.

现在,如果你声明它是不同的:

Now, if you declare it differently:

public static char[][] phoneLetterMapping = { new[] { '0' }, new[] { '1' }, new[] { 'A', 'B', 'C' } };

然后,你可以简单地调用是这样的:

Then you can simply call something like:

phoneLetterMapping[0].Length;

code你张贴在第一线,但是,也不会在C#编译。

The line of code you posted at first, however, won't compile in C#.

这篇关于我如何获得内部数组的长度在C#中的二维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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