C#代码精粹
ArreyList
- 请简述ArreyList和数组的区别
ArreyList本质上是一个object数组的封装
区别:
(1)而数组在使用时候需要定长,ArreyList可以一开始就不用定长。
(2)数组可以指定存储类型,ArreyList默认为object类型。
(3)数组的增删需要我们自己去实现,Arraylist帮我们封装好了方便使用的API。
(4)数组只要不是object就不存在装箱拆箱,而ArrayList存在。
(5)数组长度为Length,ArrayList长度为Count。 - 创建一个背包类,使用ArrayList存储物品,实现购买物品,卖出物品,显示物品功能,买入或卖出物品会导致金钱变化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129using System.Collections;
using System.Reflection.Metadata.Ecma335;
namespace C__01
{
public class Item
{
public int id;
public string name;
public int price;
public int number;
public Item(int id, string name, int price, int number)
{
this.id = id;
this.name = name;
this.price = price;
this.number = number;
}
}
public class backPack
{
public ArrayList items;
private int money;
public backPack(int money)
{
this.items = new ArrayList();
this.money = money;
}
public void buyItems(Item item)
{
if (item.id <= 0 || item.price <= 0)
{
Console.WriteLine("Item Doesn't Exist!");
return;
}
//如果钱不够直接跳出
if (this.money < item.price * item.number)
{
Console.WriteLine("Need More Cash!");
return;
}
//花钱并打印信息
money = money - (item.price * item.number);
Console.WriteLine("购买{0}{1}个花费{2}钱", item.name, item.number, item.price * item.number);
if (items.Count == 0)
{
items.Add(item);
}
else
{
//遍历添加物品
for (int i = 0; i <= items.Count; i++)
{
//如果已经存在,叠加物品
if ((items[i] as Item).id == item.id)
{
(items[i] as Item).number += item.number;
return;
}
//否则添加新物品
if ((items[i] as Item).id != item.id)
{
items.Add(item);
return;
}
}
}
}
public void sellItems(Item item)
{
int num = 0;
for (int j = 0; j < items.Count; j++)
{
if ((items[j] as Item).id == item.id)
{
if ((items[j] as Item).number > item.number)
{
num = item.number;
}
else
{
num = (items[j] as Item).number;
items.RemoveAt(j);
}
int sellMoney = (int)(item.price * item.number * 0.8f);
money = money + sellMoney;
Console.WriteLine("卖了{0}{1}个,赚了{2}元", item.name, item.number, sellMoney);
Console.WriteLine("还剩{0}元", this.money);
}
}
}
public void showItems()
{
Item item;
for (int index = 0; index < items.Count; index++)
{
item = items[index] as Item;
Console.WriteLine("有{0}{1}个", item.name, item.number);
}
Console.WriteLine("有{0}钱", this.money);
}
}
internal class Program
{
static void Main(string[] args)
{
backPack bag = new backPack(10000);
Item Throwingknife = new Item(1, "Throwingknife", 50, 5);
Item bullets = new Item(2, "Bullets", 100, 3);
Item Armor = new Item(3, "Armor", 1000, 1);
Item TTI2011 = new Item(4, "Pistol", 7000, 1);
bag.buyItems(Armor);
bag.buyItems(Throwingknife);
bag.buyItems(TTI2011);
bag.buyItems(bullets);
Console.WriteLine(bag.items.Count);
bag.sellItems(Throwingknife);
bag.showItems();
}
}
}
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 ❄️Echo Blog!
评论

4970776.webp)

.webp)

.webp)
