在没有今天的研究之前,我一直以为COLLECTION类里面只有arraylist和Hashtable是有用的。今天早上大起看了书以后,对Collection类有了更深的了解。其中以下的代码将是VB和C#穿插着讲。因为本人C#和VB都会,由于有些函数C#功能不是很好,所以使用了VB。 1:Collection的当家花旦当然是数组咯。。数组的定义方法为: int[] int_array=new int[10] int[] myIntArray = new int[5] { 1, 2, 3, 4, 5 }; 上面两句话,我就不多做解释了。 2:结构体在数组中的使用,代码如下: 创建一个类: class test { public string str_name; public string str_phone; } 对该类的引用和使用: test[] mytest=new test[3]; for(int i=0;i<mytest.Length;i++) { mytest[i]=new test(); } mytest[0].str_name="hello"; mytest[1].str_name=" world!"; mytest[0].str_phone="hahah"; 3:ArrayList ArrayList我就不多说了,反正他最大的特点就是排序。 4:Hashtable Hashtable的缺点就是不支持排序。很遗憾,另外在C#里根据KEY取VALUE很麻烦。 5:SortedList SortedList的使用方法和ArrayList的使用方法差不多,只是SortedList自动排序。 6:Stack Dim st As New Stack st.Push("aa") st.Push("bb") Stack是对仗,按照是先进后出的原则 7:Queue Dim myque As New System.Collections.Queue myque.Enqueue("aa") Queue于Stack刚刚相反,Queue是先进先出的原则来的。 8:Specialized Specialized下面有好多实力,自己去用一下就OK了。 9:枚举VB和C#示例: VB: Dim ie As System.Collections.IEnumerator = al.Keys.GetEnumerator Dim str As String = "" While (ie.MoveNext) str += ie.Current End While C#: System.Collections.IEnumerator ie=sl.Keys.GetEnumerator(); string str=""; while(ie.MoveNext()) { str+=ie.Current.ToString(); } 
|