C # 中的数组大小(长度)

如何确定 C # 中数组的大小(长度/项目数) ?

329405 次浏览

yourArray.Length:)

使用 Length属性。

int[] foo = new int[10];
int n = foo.Length; // n == 10

如果是一维数组 a,

a.Length

将给出 a的元素数。

如果 b是一个矩形多维数组(例如,int[,] b = new int[3, 5];)

b.Rank

将给出维数(2)和

b.GetLength(dimensionIndex)

将获得任何给定维度的长度(维度的索引基于0-因此 b.GetLength(0)为3,b.GetLength(1)为5)。

有关更多信息,请参见 System.Array 文档

正如@Lucero 在评论中指出的,有一个“锯齿数组”的概念,它实际上不过是一个由数组组成的单维数组(通常是单维数组)。

例如,一个人可以拥有以下内容:

int[][] c = new int[3][];
c[0] = new int[] {1, 2, 3};
c[1] = new int[] {3, 14};
c[2] = new int[] {1, 1, 2, 3, 5, 8, 13};

请注意,c的3个成员都有不同的长度。 在这种情况下,与以前一样,c.Length将表示 c、(3)和 c[0].Lengthc[1].Lengthc[2].Length的元素数分别为3、2和7。

您可以查看 数组文件来找出这个问题的答案。

在这种特殊情况下,你可能需要 长度:

int sizeOfArray = array.Length;

但是因为这是一个基本的问题,你肯定有更多类似的问题,而不是仅仅告诉你答案,我更愿意告诉你如何自己找到答案。

Visual Studio 智能感知

当您键入一个变量的名称并按下 .键时,它会显示该对象上可用的所有方法、属性、事件等的列表。当您突出显示一个成员时,它会简要描述它的功能。

按 F1

如果您找到了一个方法或属性,它可以执行您想要的操作,但是您不确定,您可以将光标移动到它上面,然后按 F1获得帮助。在这里你可以得到更详细的描述和相关信息的链接。

搜索

The search terms size of array in C# gives many links that tells you the answer to your question and much more. One of the most important skills a programmer must learn is how to find information. It is often faster to find the answer yourself, especially if the same question has been asked before.

使用教程

如果你刚刚开始学习 C # ,你会发现学习一个教程会更容易。我可以推荐 MSDN 上的 C# tutorials。如果你想要一本书,我推荐 必备 C #

堆栈溢出

如果您无法自己找到答案,请随时将问题发布在 Stack Overflow 上。但是如果你能证明你已经付出了努力首先自己找到了答案,我们将不胜感激。

对于单维数组,使用 Length属性:

int size = theArray.Length;

For multiple dimension arrays the Length property returns the total number of items in the array. You can use the GetLength method to get the size of one of the dimensions:

int size0 = theArray.GetLength(0);

for 1 dimensional array

int[] listItems = new int[] {2,4,8};
int length = listItems.Length;

用于多维数组

int length = listItems.Rank;

获取一维的大小

int length =  listItems.GetLength(0);

是这样的: 1D:

 type[] name=new type[size]  //or =new type[]{.....elements...}

2D:

 type[][]name=new type[size][] //second brackets are emtpy

当你使用这个数组时:

 name[i]=new type[size_of_sec.Dim]

或者你可以声明类似矩阵的东西

type[ , ] name=new type [size1,size2]

在大多数一般情况下,使用“长度”和“计数”。

数组:

int[] myArray = new int[size];
int noOfElements = myArray.Length;

类型列表数组:

List <int> myArray = new List<int>();
int noOfElements = myArray.Count;

到目前为止,我忽略了一件让我突然感到恼火的事:

如何知道数组中的项数? .Length等于列表的 .Count吗?

The answer is: the amount of items of type X which have been put into an array of type X created with new X[number] 你得自己扛起来!

例如,对数组的每个赋值使用计数器: int countItemsInArray = 0countItemsInArray++

(刚刚用 new X[number]创建的数组具有已经分配的 X 类型的 number项(引用)的所有空间,你可以在第一次赋值时将其分配到任何位置,例如(如果 number = 100,变量名 = a) a[50] = new X();

我不知道 C # 在创建时是否指定了数组中每个位置的初始值,如果没有,或者你无法比较的初始值(因为它可能是你自己放入数组中的值) ,如果你没有从 0开始顺序分配(在这种情况下,所有小于 countItemsInArray的位置都会被分配) ,你也必须跟踪数组中你已经分配的位置

在你的问题 size of an array (length / number of items)中,取决于 /是代表“另类”还是“除以”,后者仍然需要被覆盖(我刚才给出的“项目数量”是“项目数量”,其他人给出的 .Length与我上面代码中的 number值相对应) :

C # 有一个 sizeof操作符(https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/sizeof)。对于内置类型(例如 int)使用它是安全的(并且只对类型(而不是变量)进行操作)。因此,以字节为单位的 int类型的数组 b的大小应该是 b.Length * sizeof(int)

(由于创建时已经分配了数组的所有空间,如上所述,而且 sizeof只能处理类型,所以像 sizeof(variable)/sizeof(type)这样的代码不会工作,也不会产生没有跟踪的项数量。)