1、集合:
(1)ArryList a=new ArryList();
追加时可用add,改变数值时可用集合名加下标。
Remove(1)只会移除一次。
RemoveAt(1)移除下标为1的。
.Count查询元素数。
.Capacity查询容量。
class Program
{
static void Main(string[] args)
{
ArrayList Arr = new ArrayList();
Arr.Add(1);
Arr.Add(2);
Arr.Add(3);
Arr.Remove(1);
foreach (object i in Arr)
{
Console.WriteLine(i);
}
Console.WriteLine(Arr.Count);
Console.WriteLine(Arr.Capacity);
}
}
(2)List(泛型)
List<string>list=new List<string>;
指定类型不用像ArryList一样转换类型。
建议用List不用ArryList。
(3)栈
Stack stack=new Stack();
添加参数:用Push
如:stack.Push(1);
移除:stack.Pop();取最后一个。
stack.Peek();看最后一个,不移除。
先进后出。
(4)队列
Queue queue=new Queue();
queue.Enqueue(1);添加
queue.Peek();只看不取。
queue.Dequeue();看了就拿。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsoleApplication1
{
class Goods
{
public int Number;
public string Name;
public double Price;
public string Guige;
public int GoodsType;
}
struct GoodsType
{
public int Number;
public string TypeName;
}
class Program
{
static void LoadType(List<GoodsType> types)
{
GoodsType gt1 = new GoodsType();
gt1.Number = 1;
gt1.TypeName = "家电";
types.Add(gt1);
GoodsType gt2 = new GoodsType();
gt2.Number = 2;
gt2.TypeName = "日用品";
types.Add(gt2);
GoodsType gt3 = new GoodsType();
gt3.Number = 3;
gt3.TypeName = "办公用品";
types.Add(gt3);
}
static void Main(string[] args)
{
List<GoodsType> types = new List<GoodsType>();
LoadType(types);
List<Goods> goodses = new List<Goods>();
while (true)
{
Console.Clear();
Console.WriteLine("1、添加商品 2、修改商品 3、删除商品 4、查询商品");
switch (Console.ReadLine())
{
case "1":
AddGoods(goodses, types);
break;
case "2":
ModifyGoods(goodses, types);
break;
case "3":
DeleteGoods(goodses, types);
break;
case "4":
QueryGoods(goodses, types);
break;
}
}
}
private static void DeleteGoods(List<Goods> goodses, List<GoodsType> goodstype)
{
Console.WriteLine("请输入修改商品的名称:");
string goodsname = Console.ReadLine();
Console.WriteLine("{0,-8}{1,-18}{2,-8}{3,-18}{4,-12}", "编号", "名称", "价格", "规格", "类型");
foreach (Goods goods in goodses)
{
if (goods.Name.Contains(goodsname))
{
string typename = "";
foreach (GoodsType gt in goodstype)
{
if (gt.Number == goods.GoodsType)
{
typename = gt.TypeName;
break;
}
}
Console.WriteLine("{0,-10}{1,-" + (20 - (Encoding.Default.GetByteCount(goods.Name) - goods.Name.Length)).ToString() + "}{2,-10}{3,-" + (20 - (Encoding.Default.GetByteCount(goods.Guige) - goods.Guige.Length)).ToString() + "}{4,-" + (14 - (Encoding.Default.GetByteCount(typename) - typename.Length)).ToString() + "}", goods.Number, goods.Name, goods.Price, goods.Guige, typename);
}
}
Console.WriteLine("请选择商品编号:");
int goodsnumber = int.Parse(Console.ReadLine());
foreach (Goods goods in goodses)
{
if (goods.Number == goodsnumber)
{
goodses.Remove(goods);
break;
}
}
}
private static void ModifyGoods(List<Goods> goodses, List<GoodsType> goodstype)
{
Console.WriteLine("请输入修改商品的名称:");
string goodsname = Console.ReadLine();
Console.WriteLine("{0,-8}{1,-18}{2,-8}{3,-18}{4,-12}", "编号", "名称", "价格", "规格", "类型");
foreach (Goods goods in goodses)
{
if (goods.Name.Contains(goodsname))
{
string typename = "";
foreach (GoodsType gt in goodstype)
{
if (gt.Number == goods.GoodsType)
{
typename = gt.TypeName;
break;
}
}
Console.WriteLine("{0,-10}{1,-" + (20 - (Encoding.Default.GetByteCount(goods.Name) - goods.Name.Length)).ToString() + "}{2,-10}{3,-" + (20 - (Encoding.Default.GetByteCount(goods.Guige) - goods.Guige.Length)).ToString() + "}{4,-" + (14 - (Encoding.Default.GetByteCount(typename) - typename.Length)).ToString() + "}", goods.Number, goods.Name, goods.Price, goods.Guige, typename);
}
}
Console.WriteLine("请选择商品编号:");
int goodsnumber = int.Parse(Console.ReadLine());
foreach (Goods goods in goodses)
{
if (goods.Number == goodsnumber)
{
string typename = "";
foreach (GoodsType gt in goodstype)
{
if (gt.Number == goods.GoodsType)
{
typename = gt.TypeName;
break;
}
}
Console.WriteLine("{0,-10}{1,-" + (20 - (Encoding.Default.GetByteCount(goods.Name) - goods.Name.Length)).ToString() + "}{2,-10}{3,-" + (20 - (Encoding.Default.GetByteCount(goods.Guige) - goods.Guige.Length)).ToString() + "}{4,-" + (14 - (Encoding.Default.GetByteCount(typename) - typename.Length)).ToString() + "}", goods.Number, goods.Name, goods.Price, goods.Guige, typename);
Console.WriteLine("请选修改项:");
bool mark = false;
while (true)
{
Console.WriteLine("1、{0} 2、{1} 3、{2} 4、{3} 5、{4}", "名称", "价格", "规格", "类型", "退出");
switch (Console.ReadLine())
{
case "1":
Console.WriteLine("请输入新名称:");
goods.Name = Console.ReadLine();
break;
case "2":
Console.WriteLine("请输入新价格:");
goods.Price = double.Parse(Console.ReadLine());
break;
case "3":
Console.WriteLine("请输入新规格:");
goods.Guige = Console.ReadLine();
break;
case "4":
Console.WriteLine("请输入选择新类型:");
foreach (GoodsType gt in goodstype)
{
Console.WriteLine("{0}、{1}", gt.Number, gt.TypeName);
}
goods.GoodsType = int.Parse(Console.ReadLine());
break;
case "5":
mark = true;
break;
}
if (mark)
{
break;
}
}
}
}
}
private static void QueryGoods(List<Goods> goodses, List<GoodsType> goodstype)
{
Console.WriteLine("{0,-8}{1,-18}{2,-8}{3,-18}{4,-12}", "编号", "名称", "价格", "规格", "类型");
foreach (Goods goods in goodses)
{
string typename = "";
foreach (GoodsType gt in goodstype)
{
if (gt.Number == goods.GoodsType)
{
typename = gt.TypeName;
break;
}
}
Console.WriteLine("{0,-10}{1,-" + (20 - (Encoding.Default.GetByteCount(goods.Name) - goods.Name.Length)).ToString() + "}{2,-10}{3,-" + (20 - (Encoding.Default.GetByteCount(goods.Guige) - goods.Guige.Length)).ToString() + "}{4,-" + (14 - (Encoding.Default.GetByteCount(typename) - typename.Length)).ToString() + "}", goods.Number, goods.Name, goods.Price, goods.Guige, typename);
}
Console.WriteLine("按任意退出!");
Console.ReadLine();
}
static void AddGoods(List<Goods> goodses, List<GoodsType> goodstype)
{
try
{
Goods goods = new Goods();
goods.Number = GetMaxNumber(goodses) + 1;
Console.WriteLine("请输入商品名称:");
goods.Name = Console.ReadLine();
Console.WriteLine("请输入商品价格:");
goods.Price = double.Parse(Console.ReadLine());
Console.WriteLine("请输入商品规格:");
goods.Guige = Console.ReadLine();
Console.WriteLine("请选择商品类型编号:");
foreach (GoodsType gt in goodstype)
{
Console.WriteLine("{0}、{1}", gt.Number, gt.TypeName);
}
goods.GoodsType = int.Parse(Console.ReadLine());
goodses.Add(goods);
Console.WriteLine("添加成功!");
Thread.Sleep(2000);
}
catch
{
Console.WriteLine("添加失败!");
Thread.Sleep(2000);
}
}
static int GetMaxNumber(List<Goods> goodses)
{
int maxnumber = 0;
foreach (Goods goods in goodses)
{
if (goods.Number > maxnumber)
{
maxnumber = goods.Number;
}
}
return maxnumber;
}
}
}