Array
Array你可以理解为是所有数组的大哥
普通数组 : 特点是长度固定, 只能存储相同类型的数据
static void Main(string[] args)
{
//声明
int[] ints;
string[] strings;
People[] peoples;
//默认值
//int 类型是 0
//string 类型是 null
int[] ints1 = { 1, 2, 3 };
string[] strings1 = { "张三", "李四", "王五" };
//数组里面如果存 值类型 存储就是值本身
//数组里面如果存 引用类型 存储的就是内存地址
//数组遍历
for (int i = 0; i < ints1.Length; i++)
{
Console.WriteLine(ints1[i]);
}
foreach (var s in strings1)
{
Console.WriteLine(s);
}
int[] ages = { 18, 19, 2, 30, 60, 15, 14 };
//Array类上的方法
//1.Clear() 将数组恢复成默认值 参1:索引 参2:长度
Array.Clear(ints1,1,1);
//2.Copy() 复制
//Array.Copy(ints1, strings1, ints1.Length);
//3.Reverse() 反转
Array.Reverse(ints1);
//4.IndexOf() 从前往后查询参数2在参数1中首次出现的位置,有则返回索引 没有返回-1
//参1:数组 参2:该数在参1出现的位置 参3:指定开始查询位置 参4:查询的个数
Array.IndexOf(ages,30);
//5.LastIndexOF() 从后向前查找参2数据,出现在参1数组中,有则返回索引,没有返回-1
Array.LastIndexOf(ages,30);
//6.Find() 从前往后查询参数2在参数1中首次出现的位置 ,有则返回值 没有返回数据默认值
Array.Find(ages, x => x > 18);
//7.FindLast() 从后往前查询参数2条件的值 有则返回查到的值 没有返回数据类型默认值
Array.FindLast(ages, x => x <18);
//8.FindIndex() 从前往后查询参数2条件的值 有则返回查到的值的索引 没有返回-1
Array.FindIndex(ages, x => x ==18);
//9.FindLastIndex() 从后向前查询参数2条件的值 返回的是一个数组
Array.FindLastIndex(ages, x => x ==18);
//10.FindAll() 查找数组中所有符合参数2条件的值 返回的是一个数组
Array.FindAll(ages, x => x % 2 == 0);
//11.TrueForAll() 判断数组中的数据是否全部满足参数2,如果满足返回true 只要有一个不满足 则返回false
Array.TrueForAll(ages, x => x>0);
//12.Exists() 判断数组中是否有一项满足参数2的条件,只要有一项满足条件 则返回true 所有不满足则返回false
Array.Exists(ages,x=>x%2==0);
//实例上的方法:
//1.CopyTo()
//2.GetLength() 获取指定维度长度
//3.SetValue() 设置值
//4.GetValue() 获取值
}
}
class People
{
public string Name { get; set; }
}
List
//List: 集合 只能存储相同类型的数据,List的长度是不固定的
//格式: List<数据类型> 变量名 = new List<数据类型>();
List<string>list=new List<string>() { "1","2","3"};
List<int> list2=new List<int>(){1,2,3};
list[0] = "1111";
Console.WriteLine(list[0]);
Console.WriteLine(list.Count);
list.Sort();
list.Reverse();
list.Clear();
list.IndexOf("1");
list.Insert(0,"2");
ArrayList
#region ArrayList
//ArrayList 是一个动态数组 不固定长度和类型
ArrayList list1 = new ArrayList();
ArrayList array=new ArrayList() { "你好",1,2,true,new int[] {1,2,3} };
//获取动态数组的长度
Console.WriteLine(array.Count);
array[0] = 100;
Console.WriteLine(array[0]);
//1.Add 向ArrayList 的最后位置添加数据
list1.Add(100);
//2.AddRange()
int[] ints2 = {1,2,3,4,5,6};
list1.AddRange(ints2);
ArrayList array2 = new ArrayList() {"Hello Word!" };
list1.AddRange(array2);
//3.Insert() 在指定索引位置插入数组
list1.Insert(1,"小丑");
//4.InsertRange() 在指定的索引位置 插入集合的内容
list1.InsertRange(2,ints2);
//5.Clear()
list1.Clear();
//6.GetRange() 从集合中截取对应的数据 返回一个新的ArrayList
//参1:开始索引的位置
//参2:截取的个数
ArrayList arr = list1.GetRange(1, 3);
//7.Remove() 删除动态数组中指定的第一个值
array.Remove(true);
//8.RemoveAt() 删除数组中指定索引位置的数据
array.RemoveAt(0);
//9.RemoveRange() 删除指定范围数据 从索引1的位置开始删除 删除两个
array.RemoveRange(1, 2);
//10.SetRange() 将参数2集合中的数据 复制到当前动态数组中
//参数1:指定从动态数组中 第几个索引开始
array.SetRange(0, array2);
#endregion
Dictionary
#region Dictionary
//Dictionary(字典) 使用"键"来操作
//固定数据类型 长度不固定
//键: 标识 在一个字典中 键是唯一的 并且不能为null
//格式: Dictionary<键的数据类型,值的数据类型>变量名=new
Dictionary<string,int> dic = new Dictionary<string,int>()
{
{"1",666 },
{"2",222 },
{"3",444 },
{"4",555 }
};
//向字典中添加数据 参数1:键 参数2:值
dic.Add("你好", 666);
//取值
Console.WriteLine(dic["1"]);
//修改
dic["2"] = 333;
//键值对的个数
Console.WriteLine(dic.Count);
//判断字典中是否包含指定的key(键)和Value(值)
Console.WriteLine(dic.ContainsKey("4"));
Console.WriteLine(dic.ContainsValue(666));
#endregion
Hashtable
#region Hashtable
//Hashtable 哈希表 表示一系列由键和值组成的数据 使用键访问
Hashtable hashtable = new Hashtable()
{
{1,"1" },
{2,"2"},
{1,1 },
{"2",2 },
{true,false},
};
hashtable.Add("8", "6666");
Console.WriteLine(hashtable[1]);
hashtable["2"] = "你好";
//Keys 获取哈希表中所有的键
Console.WriteLine(hashtable.Keys);
//Values 获取哈希表中所有的值
Console.WriteLine(hashtable.Values);
//是否拥有固定大小
Console.WriteLine(hashtable.IsFixedSize);
//是否只读
Console.WriteLine(hashtable.IsReadOnly);
#endregion
SortList
#region SortList 排序列表
SortedList sortedList = new SortedList()
{
{10,"这是10" },
{1,"这是1"},
{ 2,"这是2"}
};
sortedList.Add(9, "这是9");
//GetByIndex() 通过索引进行访问 排序列表会自动根据键进行排序,索引为0的时候,获取的键值对是 键最小的那个键对值
Console.WriteLine(sortedList.GetByIndex(0));
sortedList[2] = "这个变20了";
Console.WriteLine(sortedList.GetByIndex(1));
//GetKey() 通过索引进行访问 获取键值对的 键
Console.WriteLine(sortedList.GetKey(2));
foreach (int key in sortedList.Keys)
{
Console.WriteLine(key+"\t");
}
foreach (string key in sortedList.Values)
{
Console.WriteLine(key+"\t");
}
Console.WriteLine(sortedList.Count);
#endregion
Stack
#region Stack 堆栈
Stack<string> stack = new Stack<string>();
//添加元素 推入元素
stack.Push("张三");
stack.Push("李四");
stack.Push("王五");
Console.WriteLine(stack.Count);
//移除并返回在堆栈顶部的对象
Console.WriteLine(stack.Pop());
//返回在堆栈顶部的对象,但不移除它
Console.WriteLine(stack.Peek());
Queue<string> queue = new Queue<string>();
queue.Enqueue("张三");
queue.Enqueue("李四");
queue.Enqueue("王五");
queue.Dequeue();
Console.WriteLine(queue.Peek());
#endregion