⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex-09-13

📁 Programming Csharp Source Code(代码) Programming Csharp Source Code
💻
字号:
// Example 09-13: Sorting an integer and an employee array

namespace Programming_CSharp
{
   using System;
   using System.Collections;
  
   // a simple class to store in the array
   public class Employee : IComparable
   {
      public Employee(int empID)
      {
         this.empID = empID;
      }

      public override  string ToString()
      {
         return empID.ToString();
      }

       // Comparer delegates back to Employee
      // Employee uses the integer's default
      // CompareTo method
      public int CompareTo(Object rhs)
      {
         Employee r = (Employee) rhs;
         return this.empID.CompareTo(r.empID);
      }
     
      private int empID;
   }
   public class Tester
   {
      static void Main()
      {
         ArrayList empArray = new ArrayList();
         ArrayList intArray = new ArrayList();

         // generate random numbers for 
         // both the integers and the
         // employee id's
         Random r = new Random();

         // populate the array
         for (int i = 0;i<5;i++)
         {
            // add a random employee id
            empArray.Add(new Employee(r.Next(10)+100));

            // add a random integer
            intArray.Add(r.Next(10));
         }
         
         // display all the contents of the int array
         for (int i = 0;i<intArray.Count;i++)
         {
            Console.Write("{0} ", intArray[i].ToString());
         }
         Console.WriteLine("\n");

         // display all the contents of the Employee array
         for (int i = 0;i<empArray.Count;i++)
         {
            Console.Write("{0} ", empArray[i].ToString());
         }
         Console.WriteLine("\n");

         // sort and display the int array
         intArray.Sort();
         for (int i = 0;i<intArray.Count;i++)
         {
            Console.Write("{0} ", intArray[i].ToString());
         }
         Console.WriteLine("\n");

         // sort and display the employee array
         //Employee.EmployeeComparer c = Employee.GetComparer();
         //empArray.Sort(c);
         empArray.Sort();

         // display all the contents of the Employee array
         for (int i = 0;i<empArray.Count;i++)
         {
            Console.Write("{0} ", empArray[i].ToString());
         }
         Console.WriteLine("\n");
         
      }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -