国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

黑馬程序員-集合和索引器

2019-11-17 01:29:21
字體:
來源:轉載
供稿:網友

黑馬程序員-集合和索引器

索引器

為了方便將類、結構或接口當做數組來使用。

索引器用于封裝內部集合或數組

定義語法

[訪問修飾符] 返回類型 this[索引類型 索引名]

{

  //get或set方法體

}

索引本質就是屬性

利用索引可以用key得到項,亦可用項得到序號

集合具有很強的通用性(方法名應該記住)

增加

Add、AddRange

移除

Remove、RemoveAt

清空

Clear

排序

Sort、Reverse

查找

IndexOf、LastIndexOf

判存

Contains

集合的總數

Count屬性

淺復制

Clone

索引

集合也可以通過“下標”來訪問,叫做索引

namespaceIndexer

{

classPRogram

{

staticvoidMain(string[]args)

{

MyCollectionmc=newMyCollection();

int[]nums={0,1,2,3,4,5,6,7,8,9};

//添加的功能

mc.Add(100);

mc.AddRange(nums);

//插入,在3位置上添加一個100

mc.Insert(3,100);

//移除

//mc.Remove(100);

mc.RemoveAt(2);

//清空

//mc.Clear();

//讀取位置為3的數據

Console.WriteLine(mc[2]);

Console.ReadKey();

}

}

//咱們手動寫一個集合

classMyCollection

{

//int[]nums;

ArrayListal;

//用來測試的方法

privatevoidTest()

{

this.Clear();

//this在類的內部,表示當前實例

//this[3];

}

//索引

//索引的本質是屬性;屬性的本質是方法。

publicobjectthis[intindex]

{

get{returnal[index];}

set{al[index]=value;}

}

publicMyCollection()

{

al=newArrayList();

}

//Add方法

//最不好的辦法

#region不好的方法

//publicintAdd(intnum)

//{

//if(nums==null)

//{

//nums=newint[]{num};

//}

//else

//{

//int[]temp=newint[nums.Length+1];

//for(inti=0;i<nums.Length;i++)

//{

//temp[i]=nums[i];

//}

//temp[temp.Length-1]=num;

//nums=temp;

//}

//returnnums.Length+1;

//}

#endregion

publicintAdd(objecto)

{

returnal.Add(0);

}

publicvoidAddRange(ICollectionic)

{

al.AddRange(ic);

}

publicvoidRemove(objecto)

{

al.Remove(o);

}

publicvoidRemoveAt(intindex)

{

al.RemoveAt(index);

}

publicvoidClear()

{

al.Clear();

}

publicvoidInsert(intindex,objecto)

{

al.Insert(index,o);

}

}

}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鄄城县| 烟台市| 务川| 广宁县| 厦门市| 黄陵县| 潢川县| 北流市| 萨迦县| 赤壁市| 西盟| 玉屏| 万全县| 股票| 济阳县| 贵定县| 民乐县| 哈密市| 建阳市| 隆化县| 湘潭市| 荔波县| 饶阳县| 武宁县| 六盘水市| 石林| 文安县| 新蔡县| 银川市| 岢岚县| 隆尧县| 玉环县| 宁波市| 兴业县| 大同市| 自治县| 沈阳市| 梧州市| 永靖县| 绍兴市| 常熟市|