C#數(shù)組應(yīng)用分析第2/2頁(yè)
更新時(shí)間:2007年08月01日 19:49:37 作者:
在程序循環(huán)中初始化
可以使用此處所示的嵌套循環(huán)初始化數(shù)組中的所有元素:
int[,] arr7 = new int[5,4];
for(int i=0; i<5; i++)
{
for(int j=0; i<4; j++)
{
arr7[i,j] = 0; // initialize each element to zero
}
}
System.Array 類
在 .NET Framework 中,數(shù)組是作為 Array 類的實(shí)例實(shí)現(xiàn)的。此類提供了許多有用的方法,如 Sort 和 Reverse。
下面的示例演示了使用這些方法是多么的簡(jiǎn)單。首先,使用 Reverse 方法將數(shù)組元素反轉(zhuǎn),然后使用 Sort 方法對(duì)它們進(jìn)行排序:
class ArrayMethods
{
static void Main()
{
// Create a string array of size 5:
string[] employeeNames = new string[5];
// Read 5 employee names from user:
System.Console.WriteLine("Enter five employee names:");
for(int i=0; i<employeeNames.Length; i++)
{
employeeNames[i]= System.Console.ReadLine();
}
// Print the array in original order:
System.Console.WriteLine("\nArray in Original Order:");
foreach(string employeeName in employeeNames)
{
System.Console.Write("{0} ", employeeName);
}
// Reverse the array:
System.Array.Reverse(employeeNames);
// Print the array in reverse order:
System.Console.WriteLine("\n\nArray in Reverse Order:");
foreach(string employeeName in employeeNames)
{
System.Console.Write("{0} ", employeeName);
}
// Sort the array:
System.Array.Sort(employeeNames);
// Print the array in sorted order:
System.Console.WriteLine("\n\nArray in Sorted Order:");
foreach(string employeeName in employeeNames)
{
System.Console.Write("{0} ", employeeName);
}
}
}
輸出
Enter five employee names:
Luca
Angie
Brian
Kent
Beatriz
Array in Original Order:
Luca Angie Brian Kent Beatriz
Array in Reverse Order:
Beatriz Kent Brian Angie Luca
Array in Sorted Order:
Angie Beatriz Brian Kent Luca
相關(guān)文章
C#動(dòng)態(tài)生成PictureBox并指定圖片的方法
這篇文章主要介紹了C#動(dòng)態(tài)生成PictureBox并指定圖片的方法,實(shí)例分析了C#圖形控件的動(dòng)態(tài)生成及使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07C#實(shí)現(xiàn)讀取Word表格到DataSet
在應(yīng)用項(xiàng)目里,多數(shù)情況下我們會(huì)遇到導(dǎo)入 Excel 文件數(shù)據(jù)到數(shù)據(jù)庫(kù)的功能需求,但某些情況下,也存在使用 Word 進(jìn)行表格數(shù)據(jù)編輯的情況,這其中也包括導(dǎo)入Word內(nèi)容的功能,比如表格數(shù)據(jù)導(dǎo)出到DataSet數(shù)據(jù)集,本文將給大家介紹了C#讀取Word表格到DataSet2023-12-12使用C#連接并讀取MongoDB數(shù)據(jù)庫(kù)
這篇文章介紹了使用C#連接并讀取MongoDB數(shù)據(jù)庫(kù)的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03C#中派生類調(diào)用基類構(gòu)造函數(shù)用法分析
這篇文章主要介紹了C#中派生類調(diào)用基類構(gòu)造函數(shù)用法,實(shí)例分析了派生類調(diào)用基類構(gòu)造函數(shù)的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04