數組可以是一維、多維或交錯數組
數值數組元素的預設設定為0,而引用元素的預設設定為null。【需要牢記,很多if語句需要用到】
交錯數組是數組的數組,因此起元素是參考型別並初始化為null。
數組的索引從零開始:具有n個元素的數組的索引是從0到n-1。【這個新手經常犯錯誤】
class testarraysclass
{
static void main()
{
//declare a single-dimensional aray
int[] array1 = new int[5];
//declare and set array element values
int[] array2 = new int[]{1,2,3,4,5};
//alternative syntax
int[] array3 ={1,2,3,4,5};
//declare a two dimensional array
int[,] multidimensionalarray1 = new int[2,3];
//declare and set array element values
int[,] multidimensionalarray2 = {{1,2,3},{1,2,3}};
//declare a jagged array
int[][] jaggedarray = new int[6][];
//set the values of the first array in the jagged array structure
jaggedarray[0] = new int[4] {1,2,3,4};
}
}
看一些.net 一些常用的數組操作
response.write(array.indexof(abc,"3",1));//在abc數組中尋找"3",從abc[1]開始找
response.write(array.lastindexof(abc,"3"));//在abc數組中尋找"3",從最後開始找
-------------------------------------------------------------
string[] arrstr=new string[8]{"1","4","3","2","16","14","12","14"};//arrstr[0]="1"...arrstr[7]="14"
array.reverse(arrstr); //顛倒arrstr數組,此時arrstr[0]="14"...arrstr[7]="1"
array.sort(arrstr); //給數組排序,此時順序為1,12,14,14,16,2,3,4(因為是按字串排序)
-------------------------------------------------------------
array型數組要重定義大小,必須用redim(vb),對於大數組會特別慢;且無法在中間插入元素;不能清除它們(只能設定為空白或0)
arraylist在使用上比array慢,但是不用重定義大小,使用myarrlist.add("dog")s可以方便的添加資料
arraylist myarrlist = new arraylist();//不用指出數組的大小,而且每個元素可以是任意資料類型;
myarrlist.insert(1,"abc"); //插入元素到數組[1]前
myarrlist.removeat(1); //刪除數組元素[1]
myarrlist.remove("abc"); //刪除內容為"abc"的數組元素,只刪除一次,如果要全刪,需要做迴圈
-------------------------------------------------------------
listitem newitem=new listitem();newitem.text="a";newitem.value="b";
mydropdown.items.add(newitem);//使用listitem為list框添加項
-------------------------------------------------------------
hashtable ht =new hashtable();ht["1"]="a";ht.add("2","a");//hashtable用法
sortedlist sl=new sortedlist();sl["1"]="a";sl.add("2","a");//sortedlist用法,會自動根據key進行排序
foreach(dictionaryentry abc in sl) //遍曆sortedlist的辦法