顯示具有 集合 標籤的文章。 顯示所有文章
顯示具有 集合 標籤的文章。 顯示所有文章

2011年1月1日 星期六

複製Dictionary

修改第一個Dictionary(被複製的對象)不會影響到第二個Dictionary。
程式碼
using System;
using System.Collections.Generic;

class Program
{
 static void Main()
 {
  //
  // Create and initialize Dictionary.
  //
  Dictionary dictionary = new Dictionary();
  dictionary.Add("cat", 1);
  dictionary.Add("dog", 3);
  dictionary.Add("iguana", 5);

  //
  // 複製Dictionary 到第二個物件
  //
  Dictionary copy = new Dictionary(dictionary);

  //
  // Change the first Dictionary. It won't change the copy.
  //
  dictionary.Add("fish", 4);

  //
  // Display the first Dictionary.
  //
  Console.WriteLine("--- Dictionary 1 ---");
  foreach (var pair in dictionary)
  {
   Console.WriteLine(pair);
  }
  //
  // Display the second Dictionary.
  //
  Console.WriteLine("--- Dictionary 2 ---");
  foreach (var pair in copy)
  {
   Console.WriteLine(pair);
  }
 }
}
輸出
--- Dictionary 1 ---
[cat, 1]
[dog, 3]
[iguana, 5]
[fish, 4]
--- Dictionary 2 ---
[cat, 1]
[dog, 3]
[iguana, 5]
摘要
複製整個Dictionary到一個新的Dictionary。Dictionary的類別並沒有定義複製的方法,需使用複製建構式來進行複製。

2010年12月31日 星期五

產生隨機數列

程式碼
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication3
{
 class Program
 {
  static void Main(string[] args)
  {
   foreach (int tt in GenerateRandomList(1, 20))
   {
    Console.WriteLine(tt);
   }
   Console.ReadLine();
  }

  private static List GenerateRandomList(int min,int max)
  {
   Random rand = new Random();
   int index = 0;
   string str = string.Empty;
   List list = new List();
   List randList = new List();

   for (int j = min; j <= max; j++)
   {
    list.Add(j);
   }

   for (int i = 0; i < max; i++)
   {
    index = rand.Next(0, list.Count - 1);
    randList.Add (list[index]);
    list.RemoveAt(index);
   }
   return randList ;
  }
 }
}

Generic List的排序與搜尋

假設有一Person類別
Person類別
public class Person
{
public int age;
public string name;

public Person(int age, string name)
{
this.age = age;
this.name = name;
}
}
List
 people = new List
();

people.Add(new Person(50, "Fred"));
people.Add(new Person(30, "John"));
people.Add(new Person(26, "Andrew"));
people.Add(new Person(24, "Xavier"));
people.Add(new Person(5, "Mark"));
people.Add(new Person(6, "Cameron"));
程式碼
Console.WriteLine("Unsorted list");
people.ForEach(delegate(Person p) { Console.WriteLine(String.Format("{0} {1}", p.age, p.name)); });

//搜尋小於25歲的人
List
 young = people.FindAll(delegate(Person p) { return p.age < 25; });
Console.WriteLine("Age is less than 25");
young.ForEach(delegate(Person p) { Console.WriteLine(String.Format("{0} {1}", p.age, p.name)); });

//依姓名排序
Console.WriteLine("Sorted list, by name");
people.Sort(delegate(Person p1, Person p2) { return p1.name.CompareTo(p2.name); });
people.ForEach(delegate(Person p) { Console.WriteLine(String.Format("{0} {1}", p.age, p.name)); });

//依歲數排序
people.Sort(delegate(Person p1, Person p2) { return p1.age.CompareTo(p2.age); });
Console.WriteLine("Sorted list, by age");
people.ForEach(delegate(Person p) { Console.WriteLine(String.Format("{0} {1}", p.age, p.name)); });
輸出
Unsorted list
50 Fred
30 John
26 Andrew
24 Xavier
5 Mark
6 Cameron

Age is less than 25
24 Xavier
5 Mark
6 Cameron

Sorted list, by name
26 Andrew
6 Cameron
50 Fred
30 John
5 Mark
24 Xavier

Sorted list, by age
5 Mark
6 Cameron
24 Xavier
26 Andrew
30 John
50 Fred