一:獲取二維數組的行數與列數,截圖

二:代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ArrayRank
{
public partial class Frm_Main : Form
{
public Frm_Main()
{
InitializeComponent();
}
private string[,] G_str_array;//定義數組類型變量
private Random G_Random_Num = new Random();//生成隨機數對象
private void btn_GetArray_Click(object sender, EventArgs e)
{
/*參數
minValue
類型:System.Int32
返回的隨機數的下界(隨機數可取該下界值)。
maxValue
類型:System.Int32
返回的隨機數的上界(隨機數不能取該上界值)。 maxValue 必須大於或等於 minValue。
返回值
類型:System.Int32
一個大於等於 minValue 且小於 maxValue 的 32 位帶符號整數,即:返回的值范圍包括 minValue 但不包括 maxValue。
如果 minValue 等於 maxValue,則返回 minValue。
*/
txt_display.Clear();//清空控件中的字符串
G_str_array = new string[//隨機生成二維數組
G_Random_Num.Next(2, 10),
G_Random_Num.Next(2, 10)];
lab_Message.Text = string.Format(
"生成了 {0} 行 {1 }列 的數組",
/*GetUpperBound參數
dimension
類型:System.Int32
數組的從零開始的維度,其上限需要確定。
返回值
類型:System.Int32
數組中指定維度最後一個元素的索引,或 -1(如果指定維度為空)。
*/
G_str_array.GetUpperBound(0) + 1,//GetUpperBound(0) 返回 Array 的第一維的索引上限,GetUpperBound(Rank - 1) 返回 Array 的最後一維的上限。獲取數組的行數
G_str_array.GetUpperBound(1) + 1);//獲取數組的列數
DisplayArray();//調用顯示數組方法
}
private void DisplayArray()
{
//使用循環賦值
for (int i = 0; i < G_str_array.GetUpperBound(0) + 1; i++)
{
for (int j = 0; j < G_str_array.GetUpperBound(1) + 1; j++)
{
G_str_array[i, j] = i.ToString() + "," + j.ToString() + " ";
}
}
//使用循環輸出
for (int i = 0; i < G_str_array.GetUpperBound(0) + 1; i++)
{
for (int j = 0; j < G_str_array.GetUpperBound(1) + 1; j++)
{
txt_display.Text += G_str_array[i, j];
}
txt_display.Text += Environment.NewLine;
}
}
}
}
三:按指定條件在數組中檢索元素,截圖

四:代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace FindStr
{
public partial class Frm_Main : Form
{
public Frm_Main()
{
InitializeComponent();
}
private string[] G_str_array;//定義字符串數組字段
private void Frm_Main_Load(object sender, EventArgs e)
{
G_str_array = new string[] {//為字符串數組字段賦值
"明日科技","C#編程詞典","C#范例大全","C#范例寶典"};
for (int i = 0; i < G_str_array.Length; i++)//循環輸出字符串
{
lab_Message.Text += G_str_array[i] + "\n";
}
}
private void txt_find_TextChanged(object sender, EventArgs e)
{
if (txt_find.Text != string.Empty)//判斷查找字符串是否為空
{
/*Array.FindAll類型參數
T
數組元素的類型。
參數
array
類型:T[]
要搜索的從零開始的一維 Array。
match
類型:System.Predicate<T>
Predicate<T> ,定義要搜索的元素的條件。
返回值
類型:T[]
如果找到一個其中所有元素均與指定謂詞定義的條件匹配的 Array,則為該數組;否則為一個空 Array。
*/
string[] P_str_temp = Array.FindAll//使用FindAll方法查找相應字符串
(G_str_array, (s) => s.Contains(txt_find.Text));//表示一個方法。()中的是這個方法的參數,後面是方法的返回值。
if (P_str_temp.Length > 0)//判斷是否查找到相應字符串
{
txt_display.Clear();//清空控件中的字符串
foreach (string s in P_str_temp)//向控件中添加字符串
{
txt_display.Text += s + Environment.NewLine;
}
}
else
{
txt_display.Clear();//清空控件中的字符串
txt_display.Text = "沒有找到記錄";//提示沒有找到記錄
}
}
else
{
txt_display.Clear();//清空控件中的字符串
}
}
}
}