//泛型集合类小测试代码
private Dictionary<int,string> frontHashTable ;// = new Dictionary<int,string>() ;
private List<string> myList;
private Stack<string> myStack;
private Queue<string> myQueue;
private SortedList<int,string> mySortedList;
public Form1()
{
InitializeComponent();
frontHashTable = frontHashTable = new Dictionary<int, string>();
myList = new List<string>();
myStack = new Stack<string>();
myQueue = new Queue<string>();
mySortedList = new SortedList<int,string>();
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
frontHashTable.Clear();
frontHashTable.Add(1, "近光灯");
frontHashTable.Add(2, "远光灯");
frontHashTable.Add(3, "前雾灯");
frontHashTable.Add(4, "后雾灯");
foreach (KeyValuePair<int,string> kp in frontHashTable)
{
if (kp.Key % 7 != 0)
{
listBox1.Items.Add(kp.Value);
}
}
listBox1.Items.Add("********");
foreach (string s in frontHashTable.Values)
{
listBox1.Items.Add(s);
}
//以下用List泛型方法
listBox1.Items.Clear();
myList.Clear();
richTextBox1.Clear();
myList.Add("左转向灯");
myList.Add("右转向灯");
myList.Add("挂车左转向灯");
myList.Add("挂车右转向灯");
myList.Add("仪表小灯");
//listBox1.DataSource = myList;
foreach (string s in myList)
{
richTextBox1.Text += s + "/r/n";
listBox1.Items.Add(s);
}
//以下用Stack泛型方法
myStack.Clear();
listBox1.Items.Clear();
richTextBox1.Clear();
myStack.Push("近光灯");
myStack.Push("远光灯");
myStack.Push("前雾灯");
myStack.Push("后雾灯");
myStack.Push("仪表小灯");
foreach (string s in myStack)
{
listBox1.Items.Add(s);
}
listBox1.Items.Add("*******************");
myStack.Pop();
foreach (string s in myStack)
{
listBox1.Items.Add(s);
}
//以下用Queue泛型方法
myQueue.Clear();
//listBox1.Items.Clear();
richTextBox1.Clear();
myQueue.Enqueue("近光灯");
myQueue.Enqueue("远光灯");
myQueue.Enqueue("前雾灯");
myQueue.Enqueue("后雾灯");
myQueue.Enqueue("仪表小灯");
foreach (string s in myQueue)
{
richTextBox1.Text += s + "/r/n";
}
richTextBox1.Text += "****************" + "/r/n";
myQueue.Dequeue();
foreach (string s in myQueue)
{
richTextBox1.Text += s + "/r/n";
}
//以下应用SortedList泛型方法
mySortedList.Clear();
listBox1.Items.Clear();
richTextBox1.Clear();
mySortedList.Add(5,"前灯");
mySortedList.Add(2, "后灯");
mySortedList.Add(1, "前雾");
mySortedList.Add(4, "后雾");
mySortedList.Add(9, "仪表小灯");
foreach (KeyValuePair<int ,string> de in mySortedList)
{
if (de.Key % 10 != 0)
{
listBox1.Items.Add(de.Value);
}
}
}
}