C#可遍历的集合
public class Product
{
/// <summary>
/// 自增ID
/// </summary>
public int ID { get; set; }
/// <summary>
/// 主键
/// </summary>
public string Code { get; set; }
/// <summary>
/// 名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 类型
/// </summary>
public string Category { get; set; }
/// <summary>
/// 价格
/// </summary>
public decimal Price { get; set; }
/// <summary>
/// 生产日期
/// </summary>
public DateTime ProduceDate { get; set; }
public override string ToString()
{
return string.Format("{0}{1}{2}{3}{4}{5}", ID.ToString().PadLeft(), Category.PadLeft(), Code.PadLeft(), Name.PadLeft(), Price.ToString().PadLeft(), ProduceDate.ToString("yyyy-M-d").PadLeft());
}
public static ProductCollection GetSampleCollection()
{
ProductCollection collection = new ProductCollection(
new Product() { ID = , Code = "", Category = "Red Wine", Name = "Product1" }
, new Product() { ID = , Code = "", Category = "Red Wine", Name = "Product2" }
, new Product() { ID = , Code = "", Category = "Red Wine", Name = "Product3" }
, new Product() { ID = , Code = "", Category = "Red Wine", Name = "Product4" }
, new Product() { ID = , Code = "", Category = "Red Wine", Name = "Product5" }
, new Product() { ID = , Code = "", Category = "Red Wine", Name = "Product6" }
, new Product() { ID = , Code = "", Category = "Red Wine", Name = "Product7" }
);
return collection;
}
}
产品类
public class ProductCollection : IEnumerable<Product>
{
private List<Product> list = new List<Product>();
private Hashtable table;
public ProductCollection()
{
table = new Hashtable();
}
public ProductCollection(params Product[] array)
{
table = new Hashtable();
foreach (Product item in array)
{
this.Add(item);
}
}
public ICollection Keys
{
get { return table.Keys; }
}
private string getKey(int index)
{
if (index < || index > table.Keys.Count) throw new Exception("索引超出了范围");
string selected = "";
int i = ;
foreach (string key in table.Keys)
{
if (i == index)
{
selected = key; break;
}
i++;
}
return selected;
}
/// <summary>
/// 索引器 支持类似于collection[index]这样的访问
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public Product this[int index]
{
get { string key = getKey(index); return table[key] as Product; }
set { string key = getKey(index); table[key] = value; }
}
private string getKey(string key)
{
foreach (string k in table.Keys)
{
if (key == k)
{
return key;
}
}
throw new Exception("不存在此键值");
}
/// <summary>
/// 索引器 类似于collection[key]这样的访问
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public Product this[string key]
{
get { string selected = getKey(key); return table[selected] as Product; }
set { string selected = getKey(key); table.Remove(table[selected]); this.Add(value); }
}
/// <summary>
/// 在末尾添加成员
/// </summary>
/// <param name="item"></param>
public void Add(Product item)
{
//确保key不重复
foreach (string key in table.Keys)
{
if (key == item.Code) throw new Exception("产品代码不能重复");
}
table.Add(item.Code, item);
}
/// <summary>
/// 在任意位置添加成员
/// </summary>
/// <param name="index"></param>
/// <param name="item"></param>
public void Insert(int index, Product item) { }
/// <summary>
/// 移除某一成员
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public bool Remove(Product item)
{
return true;
}
/// <summary>
/// 移除某一位置的成员
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public bool RemoveAt(int index) { return true; }
/// <summary>
/// 清除所有成员
/// </summary>
public void Clear() { table.Clear(); }
public IEnumerator<Product> GetEnumerator()
{
return new ProductEnumerator(this);
}
IEnumerator IEnumerable.GetEnumerator()
{
return new ProductEnumerator(this);
}
/// <summary>
/// 获得集合的成员数量
/// </summary>
public int Count { get { return table.Keys.Count; } }
public class ProductEnumerator : IEnumerator<Product>
{
private ProductCollection collection;
private int index;
public ProductEnumerator(ProductCollection productCollection)
{
this.collection = productCollection;
index = -;
}
public Product Current { get { return collection[index]; } }
object IEnumerator.Current { get { return collection[index]; } }
public void Dispose() { }
public bool MoveNext()
{
index++;
if (index >= collection.Count) return false;
else return true;
}
public void Reset() { index = -; }
}
}
for和foreach都可进行的迭代
ProductCollection pc = Product.GetSampleCollection();
//for (int i = 0; i < pc.Count-1; i++)
//{
// string line = pc[i].ToString();
// Console.WriteLine(line);
//}
foreach (string item in pc.Keys)
{
Console.WriteLine(pc[item]);
}
Console.ReadKey();
测试代码
C#可遍历的集合的更多相关文章
- 遍历List集合,删除符合条件的元素
List集合的遍历有三种方式:增强for循环,普通for循环,Iterator迭代器遍历 如果只是对集合进行遍历,以上三种循环都可正常遍历: (1)增强For循环遍历List集合 List<St ...
- 用<forEach>遍历list集合时,提示我找不到对象的属性
<c:forEach items="${list}" var="item"> <tr> <td>${item.UserId} ...
- freemarker 直接使用List来遍历set集合,可能会报错
转摘:http://www.javaweb1024.com/java/JavaWebzhongji/2015/04/08/528.html freemarker 直接使用List来遍历set集合,可 ...
- DOM操作中,遍历动态集合的注意事项。ex: elem.children
elem.childNodes和elem.children返回的都是动态集合. 动态集合(live collection): 不实际存储元素和属性值 每次访问集合都重新查找DOM树 遍历动态集合: ...
- 遍历Map集合:java.util.Map.Entry、KeySet两种方式
遍历Map集合的两种方式: 1.用KeySet Map.keySet(),返回一个存放所有key的set集合,通过遍历集合,根据key值取出所有的value值. Map<String,Strin ...
- 键盘录入一个文件夹路径,统计该文件夹(包含子文件夹)中每种类型的文件及个数,注意:用文件类型(后缀名,不包含.(点),如:"java","txt")作为key, 用个数作为value,放入到map集合中,遍历map集合
package cn.it.zuoye5; import java.io.File;import java.util.HashMap;import java.util.Iterator;import ...
- Jsp使用遍历List集合
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- (2)集合 遍历set集合
set集合的一些方法 Set<String> set1=new HashSet<String>(); set1.add("a"); set1.add(&qu ...
- (1)集合 ---遍历map集合
Map接口 实现Map接口的类用来存储键(key)-值(value) 对.Map 接口的实现类有HashMap和TreeMap等.Map类中存储的键-值对通过键来标识,所以键值不能重复. Ha ...
- 遍历Map集合的几种方式
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entr ...
随机推荐
- python中TAB补全
tab补全的代码文件tab.py #!/usr/bin/env python # python startup file import sys import readline import rlcom ...
- C# 并发队列ConcurrentQueue
测试函数 static async Task RunProgram() { var taskQueue = new ConcurrentQueue<CustomTask>(); var c ...
- 【性能压测】:MQ队列异步处理机制导致的系统无法接受请求的问题
一,最近压测系统交易峰值时,因该支交易采用MQ异步队列处理机制:该增加积分的交易,前段服务器优先返回给客户增加积分成功的结果,后端的MQ队列服务器再慢慢处理该请求: 二,压测过程中出现的问题现象:前几 ...
- 浏览器页面的显隐对js的setInterval()执行所产生的bug
前段时间,所写的一个”js无间隙滚动效果“,当页面离开后,重新返回时,会出现动画的错乱.我以为是因为我代码逻辑的原因导致的,但是,当在火狐浏览器上进行浏览时却没有动画错乱的问题. 于是乎,在网上查找是 ...
- 新创建的数据库,执行db2look时,遇到package db2lkfun.bnd bind failed
在新创建的数据库中,执行db2look的时候,存在这样的问题 db2v97i1@oc0644314035 ~]$ db2look -d sample -e -l -o db2look.ddl -- N ...
- ubuntu apache2 .htaccess 下配置 反向代理
安装完apache2后, a2enmod rewrite //启用.htaccess规则 a2enmod proxy a2enmod proxy_http //启用反向代理支持 [P] 配置OK,就可 ...
- 将静态网页部署到git上访问
1.将已有的项目放在github上 http://www.cnblogs.com/zqunor/p/6583182.html 2.出现错误解决方案 提交错误 http://blog.csdn.net/ ...
- vim操作命令备忘
vim操作命令备忘 查找/替换 :%s/keyword//gn //搜索匹配的关键词数量 :%s/keywords/target //替换关键词 待续……
- Bitcoin交易及验证
目录 UTXO 理解 交易的结构 交易的确认 交易验证 逆波兰表示法 使用逆波兰表示法验证交易 UTXO 理解 未花费交易输出: Unspent Transxtion output UTXO---用比 ...
- switch case :在JDK 7中,又加入了对String类型的支持,从此不用再写If-Else来判断字符串了
switch的case语句可以处理int,short,byte,char类型的值, 因为short,byte,char都会转换成int进行处理,这一点也可以从生成的字节码看出. char a = 'e ...