Array、ArrayList、List、IEnumerable、for、foreach應用

一、Array 類 (System)
聲明數組(本身也是一種變量,要先聲明再使用)
1.聲明數組的語法,數組大小由長度絕定;
數據類型 [] 數組名;
如:
string[] student;   //字符串型數組
int[] month;        //整型數組
 
2.指定數組大小
string[] student;
student = new string[3];            //先聲明數組,再指定大小3個字符.
或者
string [] student = new string [3]; //聲明同時指定其大小爲3個字符。
 
3.初始化數組
string[] student = new string[3] { "學生","士兵","教師"};
//聲明一個包含三個元素的字符串型數組,併爲組中的元素賦初始值.
 
string[] student = new string[] { "學生", "士兵", "教師" };
string[] student = { "學生", "士兵", "教師" };
//不指定數組的長度(大小),數組長度由打括號中的元素決定;
 
string[] student = new string [4];
student[0] = "羅帥";
student[2] = "楠楠";
//給指定的數組下標賦值
 
4.訪問數組
//數組元素的編號稱爲下標,下標從零開始.
 
二、Array應用
string[] ar = { "a", "c", "d", "e", "f" };
int i = Array.IndexOf(ar, "d", 1);      //在abc數組中查找"d"所有的位置,從abc[1]開始找,結果:2
int l = Array.LastIndexOf(ar, "a", 0);  //在abc數組中查找"c"所有的位置,從abc[0]開始找,結果:0
Array.Reverse(ar);                      //顛倒ar數組,此時的ar[0]等於"f"
Array.Sort(ar);                         //Sort與Reverse相反
object[] oar ={1,false,1.5,"string"};   //定義一個可以接受任何類型的數組;
 
//Array型數組要重定義大小,對於大數組會特別慢;且無法在中間插入元素;不能清除它們(只能設置爲空或0) ;
不能像javascript數組那樣用push添加;
 
三、ArrayList 類 (System.Collections)
ArrayList需要引用:using System.Collections;
 
ArrayList就是動態數組,是Array的複雜版本,它提供瞭如下一些好處:
1.動態的增加和減少元素
2.實現了ICollection和IList接口
3.靈活的設置數組的大小
 
ArrayList alist = new ArrayList();
alist.Add(1);
alist.Add(2);
 
ArrayList 類,常用屬性
Count屬性:獲取 ArrayList 中實際包含的元素的個數。
如:
int c = alist.Count;    //c等於2
 
ArrayList 類,常用方法
Contains方法:確定某元素是否在 ArrayList 中
如:
bool bl = alist.Contains("a");  bl等於True   
 
Add方法:將對象添加到 ArrayList 的結尾處。
如:
alist.Add(3);
 
ToArray方法:將ArrayList 的元素複製到指定元素類型的新數組中。
如:
Int32[] iar = (Int32[])alist.ToArray(typeof(Int32));
object[] oar = alist.ToArray(typeof(object));
 
遍歷 ArrayList
第一種遍歷
foreach(object o in al)
{
    //o
}
第二種遍歷
for(int i=0;i<alist.Count;i++)
{
    //alist[i]
}
第三種遍歷
IEnumerator ie = alist.GetEnumerator();
while (ie.MoveNext())
{
    //ie.Current.ToString()
}
 
五、List 泛型類 (System.Collections.Generic)
表示可通過索引訪問的對象的強類型列表
需要引用using System.Collections.Generic;
 
List<obj> list = new List<obj>();
list.Add(new obj(1 2, 1));
list.Add(new obj(2, 4, 3));
 
Count屬性:獲取 List<T> 中實際包含的元素的個數。
如:
int c = list.Count;    //c等於2
 
Add方法:將對象添加到 List<T> 的結尾處。
如:
list.Add(new obj(3, 4, 5));
 
Contains方法:確定某元素是否在 List<T> 中。
如:
bool bl = list.Contains(new obj(2, 4, 3));  bl等於True
 
六、List類應用
通過for添加隱式類型
如:
List<object> list = new List<object>();
for (int i = 0; i < 5; i++)
{
    list.Add(new { name = "sntetwt", age = 100 });
}
 
dropdownlist控件綁定泛型list<T>
如:
DropDownList drp = new DropDownList();
drp.DataSource = list;
drp.DataTextField = "name";
drp.DataValueField = "age";
drp.DataBind();<br><br>

//把數字字符串反序列化爲ListList<int>集合;
//以下述方法只對[1,2,5,3]這樣起作用,並且不能是"1,2,5,3"這種格式;
List<int> listid = new JavaScriptSerializer().Deserialize<List<int>>("[1,2,5,3]");
//格式:List<int> listid = new List<int> {1,2,5,3};
foreach (int id in listid) 
Response.Write(id); //結果:1253

<br>七、IEnumerable(遍歷) 接口 (System.Collections)
<em id="__mceDel">公開枚舉數,該枚舉數支持在非泛型集合上進行簡單迭代。
對集合使用foreach語句:
foreach(int i in col){...}
 
相當於:
IEnumerator etor = ((IEnumerable)col).GetEnumerator();
try
{
  while(etor.MoveNext())
  {
   ElementType clem (ElementType)etor.Current;
   ...;
  }
}
finally{(IDisposable)enumerator).Dispose();}
 
實例應用:通過Linq查找再遍歷,然後以JSON的格式輸出到客戶端;
using System.Linq;
using System.Web.Script.Serialization;
using System.Collections;
 
int[] items = new int[] { 1, 2, 3, 4, 5 };
IEnumerable<int> ints = from item in items
                        where item > 2.5
                        select item;
Response.Write(new JavaScriptSerializer().Serialize(ints)); 結果:[3,4,5]
</em>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章