c# – 数组的GetUpperBound()和GetLowerBound()函数

栏目: C# · 发布时间: 5年前

内容简介:翻译自:https://stackoverflow.com/questions/17358139/getupperbound-and-getlowerbound-function-for-array

任何人都可以告诉这两个功能有什么作用?它们采用一个整数参数,它被称为维度.但是这个整数的值如何改变输出?

下面是我跑的一个例子.

int[, ,] intMyArr = {{{ 7, 1, 3, 4 }, { 2, 9, 6, 5 } }, { { 7, 1, 3, 4 }, { 2, 9, 6, 5 }}};
Console.WriteLine(intMyArr.GetUpperBound(0));       // Output is 1
Console.WriteLine(intMyArr.GetUpperBound(1));       // Output is 1
Console.WriteLine(intMyArr.GetUpperBound(2));       // Output is 3

Console.WriteLine(intMyArr.GetLowerBound(0));       // Output is 0
Console.WriteLine(intMyArr.GetLowerBound(1));       // Output is 0
Console.WriteLine(intMyArr.GetLowerBound(2));       // Output is 0

知道为什么GetLowerBound()总是返回0?如果这总是返回0那么我们为什么

需要调用这个方法吗?

可能是一些例子让你明确这个话题

我们使用GetUpperBound()来找出给定维度的数组的上限,

像那样:

int[,,] A = new int[7, 9, 11];
  // Returns 6: 0th dimension has 7 items, and so upper bound is 7 - 1 = 6;
  int upper0 = A.GetUpperBound(0); 
  // Returns 8: 0th dimension has 7 items, 1st - 9 and so upper bound is 9 - 1 = 8;
  int upper1 = A.GetUpperBound(1); 
  // Returns 10: 0th dimension has 7 items, 1st - 9, 2nd - 11 and so upper bound is 11 - 1 = 10;
  int upper2 = A.GetUpperBound(2);

通常,GetLowerBound()返回0,因为默认情况下数组从零开始,

但在极少数情况下,他们不是:

// A is [17..21] array: 5 items starting from 17
  Array A = Array.CreateInstance(typeof(int), new int[] { 5 }, new int[] { 17 });
  // Returns 17
  int lower = A.GetLowerBound(0); 
  // Returns 21
  int upper = A.GetUpperBound(0);

使用GetLowerBound和GetUpperBound的典型循环是

int[] A = ...

  for(int i = A.GetLowerBound(0); i <= A.GetUpperBound(0); ++i) {
    int item = A[i];
    ...
  }

  // ... or multidimension

  int[,,] A = ...;

  for (int i = A.GetLowerBound(0); i <= A.GetUpperBound(0); ++i)
    for (int j = A.GetLowerBound(1); j <= A.GetUpperBound(1); ++j)
      for (int k = A.GetLowerBound(2); k <= A.GetUpperBound(2); ++k) {
        int item = A[i, j, k];
        ...
      }

翻译自:https://stackoverflow.com/questions/17358139/getupperbound-and-getlowerbound-function-for-array


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数据挖掘技术

数据挖掘技术

[美]MichaelJ.A.B / 别荣芳、尹静、邓六爱 / 机械工业 / 2006-7 / 49.00元

本书是数据挖掘领域的经典著作,数年来畅销不衰。全书从技术和应用两个方面,全面、系统地介绍了数据挖掘的商业环境、数据挖掘技术及其在商业环境中的应用。自从1997年本书第1版出版以来,数据挖掘界发生了巨大的变化,其中的大部分核心算法仍然保持不变,但是算法嵌入的软件、应用算法的数据库以及用于解决的商业问题都有所演进。第2版展示如何利用基本的数据挖掘方法和技术,解决常见的商业问题。 本书涵盖核心的数......一起来看看 《数据挖掘技术》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

MD5 加密
MD5 加密

MD5 加密工具