C#數(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 類的實例實現(xiàn)的。此類提供了許多有用的方法,如 Sort 和 Reverse。
下面的示例演示了使用這些方法是多么的簡單。首先,使用 Reverse 方法將數(shù)組元素反轉(zhuǎn),然后使用 Sort 方法對它們進行排序:
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
相關文章
C#連接SQL?Sever數(shù)據(jù)庫詳細圖文教程
C#是Microsoft公司為.NET Framework推出的重量級語言,和它搭配最完美的數(shù)據(jù)庫無疑就是Microsoft SQL Server了,下面這篇文章主要給大家介紹了關于C#連接SQL?Sever數(shù)據(jù)庫的詳細圖文教程,需要的朋友可以參考下2023-06-06
在C#中使用OpenCV(使用OpenCVSharp)的實現(xiàn)
這篇文章主要介紹了在C#中使用OpenCV(使用OpenCVSharp)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11
c#使用EPPlus將圖片流嵌入到Excel實現(xiàn)示例
這篇文章主要為大家介紹了c#使用EPPlus將圖片流嵌入到Excel實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
Unity3D實現(xiàn)物體旋轉(zhuǎn)縮放移動效果
這篇文章主要為大家詳細介紹了Unity3D實現(xiàn)物體旋轉(zhuǎn)縮放移動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-02-02

