字典集合HashTable与泛型集合Dictionary
特点:保证了数据的安全性与完整性
字典集合HashTable
namespace _94字典集合HashTable与泛型集合Dictionary
{
/// <summary>
/// 客户类
/// </summary>
public class Customer
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
private string address;
public string Address
{
get { return address; }
set { address = value; }
}
public Customer(string name,int age,string address)
{
this.name = name;
this.age = age;
this.address = address;
}
}
}
using System;
using System.Collections;
namespace _94字典集合HashTable与泛型集合Dictionary
{
internal class Program
{
static void Main(string[] args)
{
#region 字典集合HashTable
Console.WriteLine("**********************字典集合HashTable**********************");
Customer lei = new Customer("雷军", 22, "广州");
Customer ma = new Customer("马化腾", 35, "北京");
Customer qiao = new Customer("乔布斯", 48, "美国");
//键值对的方式保存数据
Hashtable list = new Hashtable();
//1.访问集合元素
list.Add(lei.Name, lei);
list.Add(ma.Name, ma);
list.Add(qiao.Name, qiao);
Customer cust = list["马化腾"] as Customer;
Console.WriteLine("{0} 客户在排除,其地址是:{1}", cust.Name, cust.Address);
Console.WriteLine("================2.常用属性(获取键)================");
foreach (object item in list.Keys)
{
Console.WriteLine("排除的客户:{0}", item);
}
Console.WriteLine("================2.常用属性(获取值)================");
foreach (var item in list.Values)
{
Customer customer = item as Customer;
Console.WriteLine("{0}客户在排队,年龄:{1},住址:{2}", customer.Name, customer.Age, customer.Address);
}
Console.WriteLine("集合中有{0}个客户", list.Count);
Console.WriteLine("================3.常用方法--添加元素================");
Customer yiming = new Customer("曾一鸣", 38, "牡丹园");
list.Add(yiming.Name, yiming);
Console.WriteLine("================3.常用方法--删除元素================");
list.Remove(lei.Name);
foreach (object item in list.Values)
{
Customer customer = item as Customer;
Console.WriteLine("{0}客户在排队", customer.Name);
}
Console.WriteLine("================3.常用方法--清除元素================");
list.Clear();
Console.WriteLine("{0},个客户在排队", list.Count);
Console.WriteLine("================3.判断几个是否包含指定key================");
if (list.ContainsKey("曾一鸣"))
{
Console.WriteLine("曾一鸣在排队");
}
else
{
Console.WriteLine("曾一鸣客户没有在排队");
}
#endregion
}
}
}
泛型集合Dictionary
namespace _94字典集合HashTable与泛型集合Dictionary
{
public class Goods
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private double price;
public double Price
{
get { return price; }
set { price = value; }
}
private string desc;
public string Desc
{
get { return desc; }
set { desc = value; }
}
public Goods(string name, double price, string desc)
{
this.name = name;
this.price = price;
this.desc = desc;
}
}
}
using System;
using System.Collections.Generic;
namespace _94字典集合HashTable与泛型集合Dictionary
{
public class ShoppingCart
{
public ShoppingCart()
{
this.ShoppingList = new Dictionary<Goods, int>();//创建集合对象
}
private Dictionary<Goods, int> ShoppingList;//商品集合
//查看商品
public void Show()
{
Console.WriteLine("已放入购物车商品如下:");
Console.WriteLine("商品名称\t\t价格\t\t数量");
Console.WriteLine("==========================================================");
foreach (Goods goods in ShoppingList.Keys)
{
Console.WriteLine("{0}\t\t{1}\t\t{2}", goods.Name, goods.Price,this.ShoppingList[goods]);
}
}
//添加商品
public void Add(Goods goods, int number)
{
this.ShoppingList.Add(goods, number);
Console.WriteLine("把{0}件{1}放入了购物车", number, goods.Name);
}
public void Remove(Goods goods, int number)
{
if (this.ShoppingList[goods] > number)
{
this.ShoppingList[goods] -= number;
Console.WriteLine("从购物车中移除了{0}件{1}", number, goods.Name);
}
else
{
this.ShoppingList.Remove(goods);
Console.WriteLine("从购物车中移除了{0}", goods.Name);
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _94字典集合HashTable与泛型集合Dictionary
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("********************************************泛型集合Dictionary********************************************");
Goods bag = new Goods("鳄鱼钱包", 1200, "全球限量发售500个");
Goods boots = new Goods("雪地靴", 1200, "就算是在雪地里也有走在火炭上的感觉");
Goods phone = new Goods("王牌手机", 1200, "在火星也能打电话回家");
ShoppingCart cart = new ShoppingCart();
cart.Add(bag, 2);
cart.Add(phone, 3);
cart.Show();
//移除一个钱包
cart.Remove(bag, 1);
cart.Show();
//再移除一个钱包
cart.Remove(bag, 1);
cart.Show();
Console.Read();
}
}
}