数组获取上限 [英] Array Get Upper bound

查看:73
本文介绍了数组获取上限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿朋友pl帮助数组
我想知道数组的上限我已经定义了数组

Hey Friend pl help in array
i want to know upper bound in array i have define array

int[] Test = new int[100];


然后我正在使用


then i am using

Test.getUpperbound();


但是他们问我关于死因的提法是行不通的
请告诉我如何获取数组中的值?


but it not work they ask me about die mention
pl tell how i get how many value in array?

推荐答案

class Program
    {
        static void Main(string[] args)
        {
            int[] array = new int[100];
            Console.WriteLine("The upper bound for the "+
                         "first dimension of a one dimensional array is:{0} ",
                          array.GetUpperBound(0));
        }
    }


输出应为:
一维数组的第一维的上限是:99

最好的问候
Espen Harlinn


The output should be:
The upper bound for the first dimension of a one dimensional array is:99

Best regards
Espen Harlinn


使用Test.GetUpperBound(0).如您所愿,它将返回99.如果用于不同等级的数组,则此方法的参数:
http://msdn.microsoft.com/en-us/library/system.array. getupperbound.aspx [ ^ ].

例如
Use Test.GetUpperBound(0). It will return 99, as you would expect. The argument of this method if for the arrays of different ranks:
http://msdn.microsoft.com/en-us/library/system.array.getupperbound.aspx[^].

For example
int [,,] array = new int[3, 5, 6];
int first = array.GetUpperBound(0); //returns 2
int second = array.GetUpperBound(1); //returns 4
int third = array.GetUpperBound(2); //returns 5



知道了吗?

下次,请阅读手册.您的问题无效.您知道.NET有多少种不同的方法吗?想象一下,您需要多少时间询问大约1%的问题...

—SA



Getting the idea?

Next time, please read the manual. Your question is not effective. Do you have any idea how many different method .NET has? Imagine how much time would you need to ask about 1% of them or so…

—SA


要添加一些不同的方法,您可以使用以下代码(例如,与先前的答案):
To add a bit different approach, you can calculate for example the number of elements in the array with the following code (basically almost the same as in previous answers):
int?[] Test = new int?[100];
Test[0] = 1;
Test[5] = 2;

System.Diagnostics.Debug.WriteLine("The array has {0} elements", new object[] { Test.GetUpperBound(0) - Test.GetLowerBound(0) + 1 });



但是您也可以使用LINQ来计算同一件事.请考虑以下内容:



But you can also use LINQ to calculate the same thing. Consider the following:

int?[] Test = new int?[100];
Test[0] = 1;
Test[5] = 2;

var query1 = (from item in Test
              select item).Count();
System.Diagnostics.Debug.WriteLine("The array has {0} elements", new object[] { query1 });


两种代码产生相同的答案.


Both of the codes produce the same answer.

The array has 100 elements


但是,如解决方案3所推测的那样,如果您对具有特定条件的元素数量更感兴趣,则LINQ将是避免讨厌循环的好方法.

例如,要获取数组中非null元素的数量,请考虑以下内容:


But as speculated in solution 3, if you''re interested more in the amount of elements with certain criteria, the LINQ would be a nice way to avoid nasty loops.

For example, to get the number of non null elements in the array, consider following:

int?[] Test = new int?[100];
Test[0] = 1;
Test[5] = 2;

var query2 = (from item in Test
              where item != null
              select item).Count();
System.Diagnostics.Debug.WriteLine("The array has {0} elements where the value is not null", new object[] { query2 });


这将导致


This would result to

The array has 2 elements where the value is not null


这篇关于数组获取上限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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