C#實(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)文章
Winform使用FTP實(shí)現(xiàn)自動(dòng)更新
這篇文章主要為大家詳細(xì)介紹了Winform使用FTP實(shí)現(xiàn)自動(dòng)更新,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07c# 斷點(diǎn)續(xù)傳的實(shí)現(xiàn)
這篇文章主要介紹了c# 斷點(diǎn)續(xù)傳的實(shí)現(xiàn),幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2020-12-12基于WinForm+Halcon實(shí)現(xiàn)圖像縮放與交互功能
本文主要講述在 WinForm 中結(jié)合 Halcon 實(shí)現(xiàn)圖像縮放、平移及實(shí)時(shí)顯示灰度值等交互功能,包括初始化窗口的不同方式,以及通過特定事件添加相應(yīng)功能的詳細(xì)步驟和代碼示例,能提升圖像處理應(yīng)用的開發(fā)效率和用戶體驗(yàn),需要的朋友可以參考下2025-01-01DevExpress GridView自動(dòng)滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了DevExpress GridView自動(dòng)滾動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06淺談C#跨線程調(diào)用窗體控件(比如TextBox)引發(fā)的線程安全問題
下面小編就為大家分享一篇淺談C#跨線程調(diào)用窗體控件(比如TextBox)引發(fā)的線程安全問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助2017-11-11