- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
谁能告诉我这两个函数的作用是什么?他们采用一个整数参数,该参数被告知是维度。但是这个整数的值如何改变输出?
下面是我运行的一个例子。
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];
...
}
关于c# - 数组的 GetUpperBound() 和 GetLowerBound() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17358139/
我想知道什么是首选... Dim sLines() As String = s.Split(NewLine) 对于每个: For Each sLines_item As String
谁能告诉我这两个函数的作用是什么?他们采用一个整数参数,该参数被告知是维度。但是这个整数的值如何改变输出? 下面是我运行的一个例子。 int[, ,] intMyArr = {{{ 7, 1, 3,
本文整理了Java中com.sun.mirror.type.WildcardType.getUpperBounds()方法的一些代码示例,展示了WildcardType.getUpperBounds(
本文整理了Java中com.twosigma.beakerx.chart.xychart.plotitem.YAxis.getUpperBound()方法的一些代码示例,展示了YAxis.getUpp
这两种方法有什么区别,什么时候你会使用一种而不是另一种? int[,] array = new int[4,3]; int length0 = array.GetLength(0); int uppe
我是一名优秀的程序员,十分优秀!