C#實(shí)現(xiàn)獲取多維數(shù)組的行數(shù)與列數(shù)
Array類是公共語(yǔ)言運(yùn)行庫(kù)中所有數(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ù)組的從零開(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類的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#中調(diào)用Python代碼的兩種實(shí)現(xiàn)方式
這篇文章主要介紹了在C#中調(diào)用Python代碼的兩種實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
C#中Dictionary泛型集合7種常見(jiàn)的用法
本文主要介紹了Dictionary集合的7種最基礎(chǔ)的用法,包括創(chuàng)建、添加、查找、遍歷、刪除等方法,程序都是由簡(jiǎn)入繁,希望能通過(guò)閱讀簡(jiǎn)單的示例,給大家一些啟發(fā)。2016-03-03
C#中DataSet,DataTable,DataView的區(qū)別與用法
這篇文章介紹了C#中DataSet,DataTable,DataView的區(qū)別與用法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
C#網(wǎng)絡(luò)請(qǐng)求與JSON解析的示例代碼
這篇文章主要介紹了C#網(wǎng)絡(luò)請(qǐng)求與JSON解析的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
C# Console利用mspaint打開(kāi)圖像并保存的方法
這篇文章主要介紹了C# Console利用mspaint打開(kāi)圖像并保存的方法,涉及C#調(diào)用畫(huà)圖板操作圖片的相關(guān)技巧,需要的朋友可以參考下2016-01-01
C#實(shí)現(xiàn)學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
winform使用委托和事件來(lái)完成兩個(gè)窗體之間通信的實(shí)例
這篇文章介紹了winform使用委托和事件來(lái)完成兩個(gè)窗體之間通信的實(shí)例,有需要的朋友可以參考一下2013-09-09

