目录

一、使用的方法

1.Array.GetUpperBound(Int32) 方法

(1)定义

(2)示例

2.Array.GetLength(Int32) 方法

(1)定义

(2)示例

二、实例

1.源码:

2.生成效果: 


        Array类是公共语言运行库中所有数组的基类,提供了创建、操作、搜索和排序数组的方法 

        可以用Array类的GetUpperBound方法,获取数组的行数与列数。同样地,也可以用Array类的GetLength方法,获取数组的行数与列数。

一、使用的方法

1.Array.GetUpperBound(Int32) 方法

        获取数组中指定维度最后一个元素的索引。

(1)定义

public int GetUpperBound(int dimension)

参数
dimension    Int32
数组的从零开始的维度,其上限需要确定。

返回
Int32
数组中指定维度最后一个元素的索引,或 -1(如果指定维度为空)。

例外
IndexOutOfRangeException
dimension 小于零。
或 -
dimension 等于或大于 Rank。

说明:在C#中,使用GetUpperBound(0)+1获取数组的行数,使用GetUpperBound(1)+1获取数组的列数。

(2)示例

// 用Array.GetUpperBound方法获取数组的行数与列数
namespace _093_2
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            ArgumentNullException.ThrowIfNull(args);

            int[,] matrix = new int[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

            int rows = matrix.GetUpperBound(0) + 1;
            int columns = matrix.GetUpperBound(1) + 1;

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    Console.Write(matrix[i, j] + " ");
                }
                Console.WriteLine();
            }

            Console.WriteLine("Total elements: " + matrix.Length);
        }
    }
}
//运行结果:
/*
1 2 3
4 5 6
7 8 9
Total elements: 9

 */

2.Array.GetLength(Int32) 方法

        获取一个 32 位整数,该整数表示 Array 的指定维中的元素数。

(1)定义

public int GetLength (int dimension);

参数
dimension    Int32
Array 的从零开始的维度,其长度需要确定。

返回
Int32
一个 32 位整数,它表示指定维中的元素数。

例外
IndexOutOfRangeException
dimension 小于零。
或 -
dimension 等于或大于 Rank。

(2)示例

// 用GetLength方法获取数组的行数和列数
namespace _093_1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            ArgumentNullException.ThrowIfNull(args);

            int[,] matrix = new int[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

            int totalElements = matrix.Length;
            int rows = matrix.GetLength(0);
            int columns = matrix.GetLength(1);

            Console.WriteLine("Total elements: " + totalElements);
            Console.WriteLine("Rows: " + rows);
            Console.WriteLine("Columns: " + columns);
            Console.WriteLine();

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    Console.Write(matrix[i, j] + " ");
                }
                Console.WriteLine();
            }
        }
    }
}
//运行结果:
/*
Total elements: 9
Rows: 3
Columns: 3

1 2 3
4 5 6
7 8 9

 */

二、实例

        本文使用了两种方法完成了设计目的:

//方法1:使用GetUpperBound获取行列数
int row = str_array.GetUpperBound(0) + 1;
int column = str_array.GetUpperBound(1) + 1;

//方法2:使用GetLength获取行列数
int row = str_array.GetLength(0);
int column = str_array.GetLength(1);

1.源码:

//用Array类的GetUpperBound方法获取数组的行数与列数
namespace _093
{
    public partial class Form1 : Form
    {
        private Button? button1;
        private TextBox? textBox1;
        private Label? label1;
        private string[,]? str_array;//定义数组类型变量
        private readonly Random? Random_Num = new();//生成随机数对象

        public Form1()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
            Load += Form1_Load;
        }

        private void Form1_Load(object? sender, EventArgs e)
        {
            // 
            // button1
            // 
            button1 = new Button
            {
                Location = new Point(12, 12),
                Name = "button1",
                Size = new Size(75, 23),
                TabIndex = 0,
                Text = "获取数组",
                UseVisualStyleBackColor = true
            };
            button1.Click += Button1_Click;
            // 
            // textBox1
            // 
            textBox1 = new TextBox
            {
                Location = new Point(1, 41),
                Multiline = true,
                Name = "textBox1",
                Size = new Size(300, 170),
                TabIndex = 1
            };
            // 
            // label1
            // 
            label1 = new Label
            {
                AutoSize = true,
                Location = new Point(102, 18),
                Name = "label1",
                Size = new Size(0, 17),
                TabIndex = 2
            };
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(7F, 17F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(304, 212);
            Controls.Add(label1);
            Controls.Add(textBox1);
            Controls.Add(button1);
            Name = "Form1";
            Text = "获取二维数组的行数与列数";
        }

        /// <summary>
        /// 获取数组的行数,获取数组的列数
        /// </summary>
        private void Button1_Click(object? sender, EventArgs e)
        {
            textBox1!.Clear();
            str_array = new string[
                Random_Num!.Next(2, 10),
                Random_Num.Next(2, 10)];
            //方法1:使用GetUpperBound获取行列数
            //int row = str_array.GetUpperBound(0) + 1;
            //int column = str_array.GetUpperBound(1) + 1;

            //方法2:使用GetLength获取行列数
            int row = str_array.GetLength(0);
            int column = str_array.GetLength(1);
            label1!.Text = string.Format("生成了 {0} 行 {1 }列 的数组",row,column);

            DisplayArray(row,column);
        }
        /// <summary>
        /// 使用循环赋值,使用循环输出
        /// </summary>
        private void DisplayArray(int row,int column)
        {
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < column; j++)
                {
                    str_array![i, j] = i.ToString() + "," + j.ToString() + "  ";
                }
            }

            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < column; j++)
                {
                    textBox1!.Text += str_array![i, j];
                }
                textBox1!.Text += Environment.NewLine;  //每行一回车
            }
        }
    }
}

2.生成效果: 

 C#获取多维数组的行数与列数:Array.GetUpperBound方法 vs Array.GetLength方法-LMLPHP

 C#获取多维数组的行数与列数:Array.GetUpperBound方法 vs Array.GetLength方法-LMLPHP

C#获取多维数组的行数与列数:Array.GetUpperBound方法 vs Array.GetLength方法-LMLPHP

02-09 15:37