网上抄的:
在 .NET Framework 中,数组是作为 Array 类的实例实现的。此类提供了许多有用的方法,如 Sort 和 Reverse。
下面的示例演示了使用这些方法是多么的简单。首先,使用 Reverse 方法将数组元素反转,然后使用 Sort 方法对它们进行排序:
C#
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[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