自定义集合
来源:互联网 发布:淘宝盗图技巧不被发现 编辑:程序博客网 时间:2024/06/06 16:48
1.如何让普通的类具备foreach功能
2.本类是缓存类,提供添加,删除,清除,移除等方法来管理对象(你想存的东西)
缓存:
1.用户的一些设置
为什么要自定义集合?
using System;using System.Collections;namespace 补充自定义集合{class CacheManager:IEnumerable{private ArrayList _AL = new ArrayList();//索引器public object this [int index] {get{ if (index > _AL.Count - 1) {throw new Exception ("越界!");} else { return _AL [index];}}}//add方法public void Add(object obj){_AL.Add (obj);}//清除方法public void Clean(){_AL.Clear ();}//在指定下标位置插入元素public void Insert(object obj,int index){_AL.Insert (index, obj);}//查找public int Find(object obj){return _AL.IndexOf (obj);}//按下标移除public void RemoveAt(int index){_AL.RemoveAt (index);}//直接移除public void Remove(object obj){if (_AL.Contains (obj)) {int index = _AL.IndexOf (obj);RemoveAt (index);} else {Console.WriteLine ("异常!数组内不存在该元素:{0}", obj);}}//继承IEnumerable需要实现的方法public IEnumerator GetEnumerator (){return new MyEnumrator(_AL);}}class MyEnumrator:IEnumerator{private ArrayList _al;private int index;//构造方法public MyEnumrator(ArrayList al){_al = al;}//返回当前对象public object Current {get{ return _al [index++];}}//如果枚举遍历器成功推进到下一个元素,则返回turepublic bool MoveNext (){if (index > _al.Count - 1) {return false;} else {return true;}}//重置public void Reset (){index = 0;}}class MainClass{public static void Main (string[] args){User u1 = new User ("马志龙", "3687");User u2 = new User ("黄河", "4gsb");CacheManager cm = new CacheManager ();cm.Add (u1);cm.Add (u2);foreach (User user in cm) {Console.WriteLine ("用户编号:{0},用户姓名:{1}",user.Id,user.Name);}}}}
0 0
- 自定义集合
- 自定义集合
- HashSet存储自定义集合
- 集合类自定义排序
- .NET 自定义配置集合
- c# 自定义集合类
- JavaScript自定义方法集合
- 自定义集合MyList
- Java 自定义Sorted集合
- android 自定义View集合
- java集合 自定义排序
- 自定义LinkedList集合
- 为什么自定义集合类
- Android自定义控件集合
- 自定义集合正则表达式
- c#自定义集合类
- 集合Bootstrap自定义confirm
- C#自定义集合
- Codeforces Round #387 (Div. 2) E. Comments
- Codeforces 747B Mammoth's Genome Decoding(碱基对)
- Windows或Linux系统中备份和恢复MongoDB数据的教程
- 对输入的字符串中C关键词的查找统计。
- 数据结构 邻接矩阵的BFS DFS
- 自定义集合
- 2016.12.12 锋利的jQueryCH11--性能优化和技巧
- ajax异步刷新邂逅了你—innerHTML
- HDU 2539 点球大战
- 翻译《有关编程、重构及其他的终极问题?》——11.不要试图把尽量多的操作符放到一行代码里
- 第十七周oj项目B: 结构体
- MyBatis学习(二):解析MyBatis配置文件的写法和使用原理
- c++函数部分
- (课堂讨论)关于SQS