亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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類是公共語言運(yùn)行庫中所有數(shù)組的基類,提供了創(chuàng)建、操作、搜索和排序數(shù)組的方法 

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

一、使用的方法

1.Array.GetUpperBound(Int32) 方法

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

(1)定義

public int GetUpperBound(int dimension)

參數(shù)

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

返回

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

例如

IndexOutOfRangeException
dimension 小于零。
或 -
dimension 等于或大于 Rank。
說明:在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 的從零開始的維度,其長度需要確定。

返回

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類的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ù)組類型變量
        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;  //每行一回車
            }
        }
    }
}

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)文章

  • C# 服務(wù)器發(fā)送郵件失敗實(shí)例分析

    C# 服務(wù)器發(fā)送郵件失敗實(shí)例分析

    在本篇文章里小編給大家?guī)硪黄P(guān)于C# 服務(wù)器發(fā)送郵件失敗實(shí)例內(nèi)容,需要的朋友們可以學(xué)習(xí)下。
    2020-03-03
  • C#識(shí)別出圖片里的數(shù)字和字母

    C#識(shí)別出圖片里的數(shù)字和字母

    本文給大家分享的是C#識(shí)別出圖片里的數(shù)字和字母的代碼,主要是識(shí)別以前公司的軟件注冊(cè)碼截圖里的數(shù)字和字母,功能很簡單,也存在很大的局限性,這里僅僅是分享,小伙伴們參考下。
    2015-03-03
  • Winform使用FTP實(shí)現(xiàn)自動(dòng)更新

    Winform使用FTP實(shí)現(xiàn)自動(dòng)更新

    這篇文章主要為大家詳細(xì)介紹了Winform使用FTP實(shí)現(xiàn)自動(dòng)更新,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • C#使用LINQ查詢操作符實(shí)例代碼(二)

    C#使用LINQ查詢操作符實(shí)例代碼(二)

    這篇文章介紹了C#使用LINQ查詢操作符的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • C#實(shí)現(xiàn)虛擬鍵盤的實(shí)例詳解

    C#實(shí)現(xiàn)虛擬鍵盤的實(shí)例詳解

    這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)虛擬鍵盤,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
    2022-12-12
  • c# 斷點(diǎn)續(xù)傳的實(shí)現(xiàn)

    c# 斷點(diǎn)續(xù)傳的實(shí)現(xiàn)

    這篇文章主要介紹了c# 斷點(diǎn)續(xù)傳的實(shí)現(xiàn),幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2020-12-12
  • 基于WinForm+Halcon實(shí)現(xiàn)圖像縮放與交互功能

    基于WinForm+Halcon實(shí)現(xiàn)圖像縮放與交互功能

    本文主要講述在 WinForm 中結(jié)合 Halcon 實(shí)現(xiàn)圖像縮放、平移及實(shí)時(shí)顯示灰度值等交互功能,包括初始化窗口的不同方式,以及通過特定事件添加相應(yīng)功能的詳細(xì)步驟和代碼示例,能提升圖像處理應(yīng)用的開發(fā)效率和用戶體驗(yàn),需要的朋友可以參考下
    2025-01-01
  • DevExpress GridView自動(dòng)滾動(dòng)效果

    DevExpress GridView自動(dòng)滾動(dòng)效果

    這篇文章主要為大家詳細(xì)介紹了DevExpress GridView自動(dòng)滾動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • 如何使用VS中的快捷鍵快速格式化代碼使好看,整齊

    如何使用VS中的快捷鍵快速格式化代碼使好看,整齊

    這篇文章主要介紹了如何使用VS中的快捷鍵快速格式化代碼使好看,整齊,非常不錯(cuò)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-04-04
  • 淺談C#跨線程調(diào)用窗體控件(比如TextBox)引發(fā)的線程安全問題

    淺談C#跨線程調(diào)用窗體控件(比如TextBox)引發(fā)的線程安全問題

    下面小編就為大家分享一篇淺談C#跨線程調(diào)用窗體控件(比如TextBox)引發(fā)的線程安全問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助
    2017-11-11

最新評(píng)論