欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)獲取多維數(shù)組的行數(shù)與列數(shù)

 更新時(shí)間:2024年02月05日 10:35:57   作者:wenchm  
這篇文章主要為大家詳細(xì)介紹了C#如何分別使用Array.GetUpperBound方法和Array.GetLength方法實(shí)現(xiàn)獲取多維數(shù)組的行數(shù)與列數(shù),需要的可以參考下

Array類(lèi)是公共語(yǔ)言運(yùn)行庫(kù)中所有數(shù)組的基類(lèi),提供了創(chuàng)建、操作、搜索和排序數(shù)組的方法 

可以用Array類(lèi)的GetUpperBound方法,獲取數(shù)組的行數(shù)與列數(shù)。同樣地,也可以用Array類(lèi)的GetLength方法,獲取數(shù)組的行數(shù)與列數(shù)。

一、使用的方法

1.Array.GetUpperBound(Int32) 方法

獲取數(shù)組中指定維度最后一個(gè)元素的索引。

(1)定義

public int GetUpperBound(int dimension)

參數(shù)

dimension    Int32 數(shù)組的從零開(kāi)始的維度,其上限需要確定。

返回

Int32 數(shù)組中指定維度最后一個(gè)元素的索引,或 -1(如果指定維度為空)。

例如

IndexOutOfRangeException
dimension 小于零。
或 -
dimension 等于或大于 Rank。
說(shuō)明:在C#中,使用GetUpperBound(0)+1獲取數(shù)組的行數(shù),使用GetUpperBound(1)+1獲取數(shù)組的列數(shù)。

(2)示例

//用Array.GetUpperBound方法獲取數(shù)組的行數(shù)與列數(shù)
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);
        }
    }
}
//運(yùn)行結(jié)果:
/*
1 2 3
4 5 6
7 8 9
Total elements: 9
 */

2.Array.GetLength(Int32) 方法

獲取一個(gè) 32 位整數(shù),該整數(shù)表示 Array 的指定維中的元素?cái)?shù)。

(1)定義

public int GetLength (int dimension);

參數(shù)

dimension    Int32 Array 的從零開(kāi)始的維度,其長(zhǎng)度需要確定。

返回

Int32 一個(gè) 32 位整數(shù),它表示指定維中的元素?cái)?shù)。

例如

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

(2)示例

// 用GetLength方法獲取數(shù)組的行數(shù)和列數(shù)
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();
            }
        }
    }
}
//運(yùn)行結(jié)果:
/*
Total elements: 9
Rows: 3
Columns: 3
1 2 3
4 5 6
7 8 9
 */

二、實(shí)例

本文使用了兩種方法完成了設(shè)計(jì)目的:

//方法1:使用GetUpperBound獲取行列數(shù)
int row = str_array.GetUpperBound(0) + 1;
int column = str_array.GetUpperBound(1) + 1;
 
//方法2:使用GetLength獲取行列數(shù)
int row = str_array.GetLength(0);
int column = str_array.GetLength(1);

1.源碼

//用Array類(lèi)的GetUpperBound方法獲取數(shù)組的行數(shù)與列數(shù)
namespace _093
{
    public partial class Form1 : Form
    {
        private Button? button1;
        private TextBox? textBox1;
        private Label? label1;
        private string[,]? str_array;//定義數(shù)組類(lèi)型變量
        private readonly Random? Random_Num = new();//生成隨機(jī)數(shù)對(duì)象
 
        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 = "獲取數(shù)組",
                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 = "獲取二維數(shù)組的行數(shù)與列數(shù)";
        }
 
        /// <summary>
        /// 獲取數(shù)組的行數(shù),獲取數(shù)組的列數(shù)
        /// </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獲取行列數(shù)
            //int row = str_array.GetUpperBound(0) + 1;
            //int column = str_array.GetUpperBound(1) + 1;
 
            //方法2:使用GetLength獲取行列數(shù)
            int row = str_array.GetLength(0);
            int column = str_array.GetLength(1);
            label1!.Text = string.Format("生成了 {0} 行 {1 }列 的數(shù)組",row,column);
 
            DisplayArray(row,column);
        }
        /// <summary>
        /// 使用循環(huán)賦值,使用循環(huán)輸出
        /// </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;  //每行一回車(chē)
            }
        }
    }
}

2.生成效果

到此這篇關(guān)于C#實(shí)現(xiàn)獲取多維數(shù)組的行數(shù)與列數(shù)的文章就介紹到這了,更多相關(guān)C#獲取多維數(shù)組行數(shù)與列數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論