现在的位置: 首页 > 综合 > 正文

C#中关于集合的一个小例子

2013年06月09日 ⁄ 综合 ⁄ 共 3658字 ⁄ 字号 评论关闭

  这是一个在c#中使用集合的一个小小的例子,里面涉及到了 Hashtable,ArrayList,集合的排序,枚举器,集合的遍历,成员的查找。

  例子如下:

Form1代码:

 

 public partial class Form1 : Form
    
{
        
public Form1()
        
{
            InitializeComponent();
        }

        
public string rInfo;
        
public int rInfo2;

        
private void button1_Click(object sender, EventArgs e)
        
{
            
            Hashtable hTable 
= new Hashtable();
            
int flag = 0;

            
try
            
{
                hTable.Add(
5new animal(5"red bull"));
                hTable.Add(
6new animal(6"blue lion"));
                hTable.Add(
4new animal(4"green fly"));
                hTable.Add(
2new animal(2"yellow dog"));
                hTable.Add(
3new animal(3"white horse"));
                hTable.Add(
1new animal(1"black pig"));
            }

            
catch (Exception Ex)
            
{
                MessageBox.Show (Ex.Message);
            }


            
//用ArrayList对键进行排序
            ArrayList aList = new ArrayList(hTable.Keys);
            aList.Sort();

            
//遍历器打印排序后的结果
            IDictionaryEnumerator iDicEnumerator = hTable.GetEnumerator();
            System.Text.StringBuilder sb
=new System.Text.StringBuilder();
            
while (iDicEnumerator.MoveNext())
            
{
                animal oAnimal 
= (animal)hTable[iDicEnumerator.Key];
                sb.AppendLine (oAnimal.code 
+ " " + oAnimal.name);
            }

            MessageBox.Show(sb.ToString());
            
//查找动物名字
            Form2 oF2 = new Form2(this"0");
            oF2.ShowDialog();
       
            
foreach (int sCode in aList)
            
{
                animal oAnimal 
= (animal)hTable[sCode];
                
if (oAnimal.name == rInfo)
                
{
                    MessageBox.Show(
"查找结果:代码为" + oAnimal.code );
                    flag 
= 1;
                    
break;
                }

            }

            
if (flag == 0)
            
{
                MessageBox.Show(
"没有这个动物!");
            }


            
//查找动物代码
            Form2 oF3= new Form2(this"1");
            oF3.ShowDialog();
            
if (hTable.ContainsKey(rInfo2))
            
{
                animal s 
= (animal)hTable[rInfo2];
                MessageBox.Show(
"查找结果:动物名字为" + s.name);
            }

            
else
            
{
                MessageBox.Show(
"没有找到这样的动物!");
            }


        }


      
    }


    
class animal
    
{
        
public int code;
        
public string name;

        
public animal(int Code, string Name)
        
{
            
this.code = Code;
            
this.name = Name;
        }

    }

 

form2的代码:

 

 public partial class Form2 : Form
    
{
        Form1 kk;
        
string rFlag="";
        
public Form2(Form1 k,string flag)
        
{
            InitializeComponent();
            kk 
= k;
            rFlag 
= flag;
            
if (rFlag == "1")
            
{
                textBox1.ReadOnly 
= true;
                textBox1.Text 
= "1";
            }

        }


        
private void button1_Click(object sender, EventArgs e)
        
{
            
if (this.textBox1.Text.Trim() != "")
            
{
                
if (rFlag == "0")
                
{
                    kk.rInfo 
= this.textBox1.Text;
                }

                
else
                
{
                    kk.rInfo2 
= Convert.ToInt32(this.textBox1.Text);
                }

                
                
this.Close();
            }

        }


        
private void textBox1_MouseDoubleClick(object sender, EventArgs e)
        
{
            
int liu = Convert.ToInt32(this.textBox1.Text) + 1;
            
if(liu>6
                 
this.textBox1.Text="1";
             
this.textBox1.Text = liu.ToString();
        }



       
    }

抱歉!评论已关闭.