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 ...
随机推荐
- 性能测试 vs 负载测试 vs 压力测试
在做一些软件测试工作时,常常会被提及性能测试.负载测试.压力测试,这也是在软件测试方面最容易混淆的三个概念.之前和一个测试大牛聊天,他和我说常常面试一些测试人员会问一些这样的问题,大多人认为负载测试等 ...
- mysql工具——使用mysqlshow查看mysql对象信息,查看mysql表大小
关键词:查看表大小,mysqlshow mysqlshow --count -uroot -p test
- 配置 CentOS 7 安装位置
红框里是CentOS 7安装程序检测到的硬盘,选择你想把系统安装到哪个硬盘当中. 点击 Add disk 按钮可以添加指定的附加设备或者网络设备(通过ISCSI.FCoE等方式添加硬盘) 此处设置分区 ...
- (五)Audio子系统之AudioRecord.stop
在上一篇文章<(四)Audio子系统之AudioRecord.read>中已经介绍了AudioRecord如何获取音频数据,接下来,继续分析AudioRecord方法中的stop的实现 函 ...
- Python开发:Windows下Python+Eclipse+Pydev开发环境配置
一.配置前的准备: 1.安装jdk: 下载地址: https://www.oracle.com/technetwork/java/javase/downloads/index.html 2.安装Ecl ...
- Ribbon是什么?
学而时习之,不亦说乎! --<论语> Ribbon使用版本2.2.2 Ribbon是什么? 开始接触Ribbon的时候,网上以及很 ...
- Java 并发编程——Callable+Future+FutureTask
Java 并发编程系列文章 Java 并发基础——线程安全性 Java 并发编程——Callable+Future+FutureTask java 并发编程——Thread 源码重新学习 java并发 ...
- 修改zend studio字体大小
第一步:进入设置窗口 windows -> preferences第二步:进入修改字体的选项卡. General -> Appearance -> Colors and ...
- echo(),print(),print_r()之间的区别?
echo是PHP语句, print和print_r是函数,语句没有返回值,函数可以有返回值(即便没有用) print只能打印出简单类型变量的值(如int,string) print_r可以打印出复 ...
- Implicit super constructor xx() is undefined for default constructor. Must define an explicit constructor
错误:Implicit super constructor xx() is undefined for default constructor. Must define an explicit c ...