004days
- Statck__Queue(栈和队列)
- ArrayList
- Dictionary
- List
- Hashtable
Statck__Queue(栈和队列)
using System;
using System.Collections;
namespace Statck__Queue
{//Statck Queue
//栈, 队列
//区别
//栈:先进先出
//队列:先进后出
// using System.Collections
class Program
{
static void Main(string[] args)
{
STATCK();
QUEUE();
}
public static void STATCK()
{
//实例化一个栈
Stack s = new Stack();
//将一个元素压栈
s.Push("hello");
s.Push("h");
s.Push("e");
s.Push("l");
s.Push("o");
//获取栈顶的元素
object ele = s.Peek();
Console.WriteLine(ele);
//将栈顶元素出栈,返回刚刚出栈的元素
object e = s.Pop();
//遍历集合
foreach (object obj in s)
{
Console.WriteLine(obj);
}
}
public static void QUEUE()
{//实例化一个Queue
Queue q = new Queue();
//将一个元素放到队列中
q.Enqueue("hello");
q.Enqueue("h");
q.Enqueue("e");
q.Enqueue("l");
q.Enqueue("o");
//获取排在队列首位的元素
object ele = q.Peek();
Console.WriteLine(ele);
//将队首元素移出队列,放回值为刚刚移出的元素
object e = q.Dequeue();
}
}
}
ArrayList
using System;
using System.Collections;
namespace housebook5
{
//集合:是一个容器,是用来存储兼容的数据类型的容器
#region
/*
//数组和集合的不同点
//1.数组是定长的容器,而集合是变长的容器
//2.集合提供了很多的快捷的方法来操作元素
//1.ArrayList:一个类似于数组的容器
//using System.Collectioms
{
class Program
{
static void Main(string[] args)
{//实例化一个ArrayList对象
//增、删、改、查
//ArrayList中可以添加的元素类型为object
ArrayList list = new ArrayList();
//增
AddOperation(list);
//删
RemoveOperation(list);
//改
UpdateOperation(list);
//查
SearchOperation(list);
//其他方法
}
public static void AddOperation(ArrayList list)
{//增,可以添加任意类型的元素
list.Add(1);
list.Add("hello world");
list.Add(3.14);
list.Add(true);
//批量添加:将一个集合中的元素添加到另外一个集合中
list.AddRange(new int[] { 1, 2, 3 });
list.Add(new int[] { 1, 2, 3 });
Console.WriteLine(list.Count);
//插入
list.Insert(0, "hello world");
//批量插入元素
list.InsertRange(0,new bool[] {true,false,true })
}
public static void RemoveOperation(ArrayList list)
{//移除指定的第一个元素
list.Remove(1);
//移除指定下标的元素
//注意不要越界
list.RemoveAt(4);
//给定一个下标和长度,确定一个范围,移除这个范围内的元素
//注意:不要越界,长度不要超出
list.RemoveRange(3, 2);
}
public static void UpdateOperation(ArrayList list)
{//1.通过下标来修改一个元素
list[1] = 1;
//2.批量修改元素
list.SetRange(0, new string[] { "a", "b", "c"});
}
public static void SearchOperation(ArrayList list)
{//通过便利下标
// for(int i = 0; i < list.Count; i++)
// {
// Console.WriteLine(list[i]);
//}
//2.通过foreach遍历
//注意:在foreach中,遍历的元素类型一定要和集合中存储的元素类型保持一致
//如果遍历的类型和集合中类型中不一样,会将集合中的的元素进行强制类型转换
// foreach(object obj in list)
// {//在foreach中不可以修改集合
// Console.WriteLine(obj)}
//3.通过枚举器/迭代器来遍历,也不能修改集合
IEnumerator em = list.GetEnumerator();
while (em.MoveNext())
{
object obj = em.Current;
Console.WriteLine(obj);
}
//查找某个元素对应的下标
int index = list.IndexOf(3); //查询某个元素第一次出现的下标
int index2 = list.LastIndexOf(3); //查询某个元素最后一次出现的下标
int index3 = list.BinarySearch(3); //使用二分查找查询某个元素
}
public static void OtherOperation(ArrayList list)
{//1.集合排序,默认是降序
list.Sort();
//集合倒序
list.Reverse();
//3.判断集合中是否包含某个元素
bool contains = list.Contains(1);
//4、将一个集合中的元素拷贝到另外一个集合中
object[] newList = new object[10];
list.CopyTo(newList);
}
}
*/
#endregion
//设计一个person类
//字段:int age;
//设计一个Arraylist的集合,在这个集合中添加10个person对象
//将这个集合中的元素按照年龄进行升序排序
//试一下:sort()方法能不能实现?
#region
/*
//Q:集合中可以存储任何类型的数据,如果集合中存储的是自定义的类型,如何使用sort
//A:
//1.sort():会将集合中的元素按照一定的规则进行排序
//1.通过Icomparable接口实现
//让集合中需要排序的元素对应的类去实现这个接口
class Peolpe:IComparable, IComparable<Peolpe>
{
public int age;
public Peolpe(int age)
{
this.age = age;
}
public override string ToString()
{
return age+"";
}
//1.这个方法就是Icomparable接口中自定义比较规则的方法
//前提:要比较的两个对象是谁?this和obj
//==0:this==obj
//>0 this>obj
//<0 this<obj
public int CompareTo(object obj)
{
//先判断obj是不是person类型的
if (obj is Peolpe)
{
Peolpe anotherpeople = obj as Peolpe;
//将比较结果返回
return age - anotherpeople.age;
}
else
return 0;
}
//2.使用 IComparable<Peolpe>来定义规则
public int CompareTo(Peolpe obj)
{
return age - obj.age;
}
}
class Program
{
public static void Main(string[] args)
{
ArrayList list = new ArrayList();
ADD(list);
list.Sort();
foreach (object obj in list)
{
Console.WriteLine(obj);
}
}
public static void ADD(ArrayList list)
{
list.Add(new Peolpe(3));
list.Add(new Peolpe(4));
list.Add(new Peolpe(5));
list.Add(new Peolpe(6));
list.Add(new Peolpe(1));
list.Add(new Peolpe(2));
list.Add(new Peolpe(7));
list.Add(new Peolpe(10));
list.Add(new Peolpe(9));
list.Add(new Peolpe(8));
}
}
*/
#endregion
}```
## Dictionary
```csharp
using System;
using System.Collections.Generic;
//Dictionary<>:类似于Hashtable,存储的元素也是键值对
//using System.Collections.Generic;
namespace Dictionary
{
class Program
{
static void Main(string[] args)
{//实例化一个Dictionary对象
Dictionary<string, string> dic = new Dictionary<string, string>();
//增
AddOperation(dic);
//删
RemoveOperation(dic);
// 改
UpdateOperation(dic);
//查
SearchOperation(dic);
}
//增
static void AddOperation(Dictionary<string, string> dic)
{//添加
dic.Add("name", "xiaohong");
dic.Add("age", "10");
dic.Add("gender", "male");
dic.Add("score", "59.9");
}
//删
static void RemoveOperation(Dictionary<string, string> dic)
{
dic.Remove("age");
}
//改
static void UpdateOperation(Dictionary<string, string> dic)
{
dic["name"] = "laowang";
}
//查
static void SearchOperation(Dictionary<string, string> dic)
{//直接遍历Dictionary
//Dictionary<>中的元素类型是keyvaluepair<>
foreach(KeyValuePair<string,string> obj in dic)
{
Console.WriteLine(obj);
}
foreach (KeyValuePair<string, string> obj in dic)
{
string key = obj.Key;
string value = obj.Value;
Console.WriteLine($"{key}={value}");
}
//获取dictionary中所有的key
ICollection<string> keys = dic.Keys;
foreach(string key in keys)
{
string value = dic[key];
Console.WriteLine($"{key}={value}");
}
}
}
}
List
using System;
using System.Collections.Generic;
/*
namespace List
{//list<>
//也是一个集合,和ArrayList的区别"
//list<>集合是只能存储指定类型的集合
// using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{//实例化一个list对象
List<string> list = new List<string>();
//实例化一个ArrayList对象
//增、删、改、查
//增
AddOperation(list);
//删
RemoveOperation(list);
//改
UpdateOperation(list);
//查
SearchOperation(list);
//其他方法
}
public static void AddOperation(List<string> list)
{//增,可以添加任意类型的元素
list.Add("阿童木");
list.Add("hello world");
list.Add("一休");
list.Add("大力水手");
list.Add("大力水手");
list.Add("大力水手");
list.Add("大力水手");
list.Add("大力水手");
list.Add("大力水手");
//批量添加:将一个集合中的元素添加到另外一个集合中
list.AddRange(new string[] { "1", "2", "3" });
Console.WriteLine(list.Count);
//插入
list.Insert(0, "hello world");
//批量插入元素
list.InsertRange(0, new string[] { "true", " false, true" });
}
public static void RemoveOperation(List<string> list)
{//移除指定的第一个元素
list.Remove("一休");
//移除指定下标的元素
//注意不要越界
list.RemoveAt(4);
//给定一个下标和长度,确定一个范围,移除这个范围内的元素
//注意:不要越界,长度不要超出
list.RemoveRange(3, 2);
//4\删除所有满足条件的匹配项
// 1. list.RemoveAll(Method);
//2.
list.RemoveAll(name => name == "大力水手");
}
public static bool Method(string obj)
{
return obj == "大力水手";
}
public static void UpdateOperation(List<string> list)
{//1.通过下标来修改一个元素
list[0] = "机械暴龙兽";
}
public static void SearchOperation(List<string> list)
{//通过便利下标
// for(int i = 0; i < list.Count; i++)
// {
// Console.WriteLine(list[i]);
//}
//2.通过foreach遍历
//注意:在foreach中,遍历的元素类型一定要和集合中存储的元素类型保持一致
//如果遍历的类型和集合中类型中不一样,会将集合中的的元素进行强制类型转换
// foreach(object obj in list)
// {//在foreach中不可以修改集合
// Console.WriteLine(obj)}
//3.通过枚举器/迭代器来遍历,也不能修改集合
IEnumerator<string> em = list.GetEnumerator();
while (em.MoveNext())
{
object obj = em.Current;
Console.WriteLine(obj);
}
//查找某个元素对应的下标
int index = list.IndexOf("机械暴龙兽"); //查询某个元素第一次出现的下标
int index2 = list.LastIndexOf("机械暴龙兽"); //查询某个元素最后一次出现的下标
int index3 = list.BinarySearch("机械暴龙兽"); //使用二分查找查询某个元素
}
public static void OtherOperation(List<string> list)
{//获取一个集合中的子集
List<string> sub = list.GetRange(1, 2);
//Exists:判断集合中受否存在指定条件的元素
bool result = list.Exists(name=>name=="阿童木");
//Contains:判断集合中是否包含指定的元素
}
}
}
*/
namespace Other
{
class Program
{public static void Main(string[] args)
{
//实例化一个集合
List<int> list = new List<int>();
//添加元素
list.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
//判断集合中是否包含指定条件的元素
bool result = list.Exists(number => number % 2 != 0);
Console.WriteLine(result);
//查找集合中的第一个奇数
int n = list.Find(number => number % 2 != 0);
Console.WriteLine(n);
//查找集合中所有的奇数
List<int> results= list.FindAll(number => number % 2 != 0);
//第一个奇数的下标
list.FindIndex(number => number % 2 != 0);
//查找集合中最后一个奇数
list.FindLast(number => number % 2 != 0);
//移除所有的奇数
list.RemoveAll(number => number % 2 != 0);
}
}
}
Hashtable
using System;
using System.Collections;
//Hashtable:是一个集合,存储的元素是一个 键值对(key-value-pair)
//键值对:是有两个值来组成,一个是键(key),一个是值(value)
//using System.Collections;
namespace xiaoming
{
class Program
{
static void Main(string[] args)
{//实例化一个Hashtable
Hashtable table = new Hashtable();
//增
AddaOperation(table);
//删除
RemoveOperation(table);
//改
UpdateOperation(table);
//查询操作
searchOperation(table);
//其他操作
OtherOperation(table);
}
//增
public static void AddaOperation(Hashtable table)
{//hashtable中键值对的特点
//1.键和值需要同时出现
//2.键和值一一对应,一个键对应一个值
//3.不允许出现相同的键(key);,但可以出现相同的值(value)
//4.Hashtable中的元素是按照key的hash编码来进行排序的,所以,hashtable中元素的顺序可能和添加的顺序是不一样的
//添加的是键值对
table.Add("name", "xiaoming");
table.Add(1, "xiaoming");
table.Add("xiaomingd", 10);
table.Add(false, true);
// table.Add(false, 13);会出现异常
}
//删
public static void RemoveOperation(Hashtable table)
{//通过key来删除一个键值对
table.Remove("name");
}
//改
public static void UpdateOperation(Hashtable table)
{//通过键来修改值,
table["name"] = "laowang";
//还可以通过这种方式添加
table["height"] = 173;
}
//查询操作
public static void searchOperation(Hashtable table)
{//1.
/*
//Hashtable中存储的元素类型是DictionaryEntry
foreach(DictionaryEntry entry in table)
{//获取一个键值对中的键
object key = entry.Key;
//获取一个键值对中的值
object value = entry.Value;
Console.WriteLine($"{key}={value}");*/
//2
//获取Hashtable中所有的Key
ICollection keys = table.Keys;
//遍历刚刚获取到的所有的key
foreach(object key in keys)
{//通过key来获取value
object value = table[key];
Console.WriteLine($"{ key}={value}");
}
}
//其他操作
public static void OtherOperation(Hashtable table) {
//判断Hashtable中是否包含特定的键
bool result = table.Contains("age");
}
}
}