C#数组中的Rank,GetUpperBound(), GetLength()
Rank-数组的秩,一维数组的Rank=1;二维数组的Rank=2;
GetUpperBound()--获取每一维的索引的上限,
比如int[4,5], 那么GetUpperBound(0) = 3; GetUpperBound(1) = 4 ;
所以
对于二维数组来说
GetUpperBound(0)+1=行数;
GetUpperBound(1)+1=列数
然后就是
GetLength(0) = 行数,
GetLength(1) = 列数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//数组的合并和拆分
namespace ArrayMergeSplit
{
internal class Program
{
static void Main(string[] args)
{
//将两个一维数组合并成维一个一维数组
int[] arr1 = new int[] { 1, 2, 3, 4, 5 };
int[] arr2 = new int[] { 6, 7, 8, 9, 10 };
Console.WriteLine("原始的两个数组:");
Console.WriteLine("数组arr1的元素为:");
foreach (int n1 in arr1)
Console.Write(n1 + " ");
Console.WriteLine();
Console.WriteLine("数组arr2的元素为:");
foreach (int n2 in arr2)
Console.Write(n2 + " ");
Console.WriteLine();
int[] arr3 = new int[arr1.Length + arr2.Length];
for (int i = 0; i < arr3.Length; i++)
{
if (i < arr1.Length)
arr3[i] = arr1[i];
else if (i >= arr1.Length)
arr3[i] = arr2[i - arr1.Length];
}
Console.WriteLine("合并后的数组为:");
foreach (int n3 in arr3)
Console.Write(n3 + " ");
Console.WriteLine();
//将两个一维数组合并成一个二位数组
int k = arr1.Length;
int m = arr2.Length;
int row = 2;
int col = 0;
if (k > m)
{
col = k;
}
else
col = m;
int[,] arr4 = new int[row, col];
for (int x = 0; x < arr4.GetLength(0); x++)
{
for (int y = 0; y < arr4.GetLength(1); y++)
{
if (y < arr1.Length && x == 0)
arr4[x, y] = arr1[y];
else if (y < arr2.Length && x == 1)
arr4[x, y] = arr2[y];
}
}
Console.WriteLine("合成的二维数组是:");
for (int ii = 0; ii < arr4.GetLength(0); ii++)
{
for (int jj = 0; jj < arr4.GetLength(1); jj++)
{
Console.Write(arr4[ii, jj] + " ");
}
Console.WriteLine();
}
//另外一种实现方法
int[,] arr5 = new int[row, col];
for (int i = 0; i < arr5.Rank; i++)
{//Array.Rank属性返回数组的维数,也就是行数
switch (i)
{
case 0:
{
for (int j = 0; j < arr1.Length; j++)
arr5[i, j] = arr1[j];
break;
}
case 1:
{
for (int j = 0; j < arr2.Length; j++)
arr5[i, j] = arr2[j];
break;
}
}
}
Console.WriteLine("合并后的二维数组为:");
for (int i = 0; i < arr5.Rank; i++)
{//Rank获取数组的维数
//arr5.GetUpperBound(arr5.Rank-1)获取数组指定维度的上限
for (int j = 0; j <= arr5.GetUpperBound(arr5.Rank-1); j++)
{
Console.Write(arr5[i, j] + " ");
}
Console.WriteLine();
}
//GetUpperBound() 方法在 C# 中用于获取数组维度的上限
//对于一维数组,它基本上返回数组的上限索引,即数组的长度减去1。
//对于多维数组,它返回指定维度的上限。
Console.WriteLine("数组的维数:");
Console.WriteLine(arr5.Rank);//结果是2
Console.WriteLine("GetUpperBound()的用法:");
//Console.WriteLine(arr5.GetUpperBound(arr5.Rank));
//IndexOutOfRange异常
//用GetUpperBond()获去一维数组上限索引
int[] arr11 = new int[] { 1, 2, 3, 4, 5 };
int[] arr22 = new int[] { 6, 7, 8, 9, 10 };
Console.WriteLine("一维数组的秩维:{0}", arr1.Rank);
Console.WriteLine("获取一维数组的上限:");
Console.WriteLine(arr11.GetUpperBound(0));
Console.WriteLine(arr22.GetUpperBound(0));
//GetUpperBound()获得多维数组指定维度的上限
Console.WriteLine("获取二维数组arr5的GetUpperBound()值");
for(int i=arr5.Rank-1; i>=0; i--)
{
Console.WriteLine("arr5.GetUpperBound({0})= {1}", i, arr5.GetUpperBound(i));
}
Console.WriteLine();
Console.WriteLine("数组arr5的元素为:");
for(int i =0; i<arr5.GetLength(0); i++)
{
for(int j=0; j<arr5.GetLength(1); j++)
{
Console.Write(arr5[i, j]+ " ");
}
Console.WriteLine() ;
}
Console.WriteLine(arr5.GetUpperBound(0));//arr5[2,5],所以此为1
Console.WriteLine(arr5.GetUpperBound(1));//arr5[2,5],所以此为4
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
//测试2
int[,] multiArray = new int[3, 5];
Console.WriteLine("二维数组的秩是多少?" + multiArray.Rank);
int dim0UpperBound = multiArray.GetUpperBound(0); // 返回 2
int dim1UpperBound = multiArray.GetUpperBound(1); // 返回 4
//GetUpperBound()获得每一维索引的上限
Console.WriteLine(dim0UpperBound);
Console.WriteLine(dim1UpperBound);
//所以对于二维数组来说,arr5.GetUpperBound(0)就是第一维索引的上限,也就是行数-1
//arr5.GetUpperBound(1)就是第二维的索引的上限,也就是列数-1;
}
}
}