添加Lesson7
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
namespace Lesson7_List
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
#region 知识点一 List的本质
|
||||
//List是C#为我们封装好的一个类
|
||||
//它的本质是一个可变类型的泛型数组
|
||||
//List类帮助我们实现了很多方法
|
||||
//比如泛型数组的增删查改
|
||||
#endregion
|
||||
|
||||
#region 知识点二 声明
|
||||
//需要引用命名空间System.Collections.Generic
|
||||
List<int> list = new List<int>();
|
||||
List<string> list2 = new List<string>();
|
||||
#endregion
|
||||
|
||||
#region 知识点三 增删查改
|
||||
//增
|
||||
list.Add(1);
|
||||
list.Add(2);
|
||||
list.Add(3);
|
||||
list2.Add("123");
|
||||
//范围增
|
||||
List<string> list3 = new List<string>();
|
||||
list3.Add("456");
|
||||
list2.AddRange(list3);
|
||||
//插入
|
||||
list.Insert(0,9999);
|
||||
|
||||
|
||||
//删
|
||||
//1.移除指定元素
|
||||
list.Remove(1);
|
||||
//2.移除指定位置的元素
|
||||
list.RemoveAt(0);
|
||||
//3.清空
|
||||
list.Clear();
|
||||
|
||||
//查
|
||||
//1.得到指定位置的元素
|
||||
Console.WriteLine(list[0]);
|
||||
//2.查看元素是否存在
|
||||
if (list.Contains(1))
|
||||
{
|
||||
Console.WriteLine("存在为1的元素");
|
||||
}
|
||||
//3.正向查找元素位置
|
||||
//找到返回位置,找不到返回-1
|
||||
int index = list.IndexOf(1);
|
||||
Console.WriteLine(index);
|
||||
//4.反向查找元素位置
|
||||
//找到返回位置,找不到返回-1
|
||||
index = list.LastIndexOf(1);
|
||||
Console.WriteLine(index);
|
||||
|
||||
//改
|
||||
Console.WriteLine(list[0]);
|
||||
list[0] = 99;
|
||||
Console.WriteLine(list[0]);
|
||||
#endregion
|
||||
|
||||
#region 知识点四 遍历
|
||||
//长度
|
||||
Console.WriteLine(list.Count);
|
||||
//容量
|
||||
//避免产生垃圾
|
||||
Console.WriteLine(list.Capacity);
|
||||
for (int i = 0; i < list.Count; i++)
|
||||
{
|
||||
Console.Write(list[i]);
|
||||
}
|
||||
foreach (Object item in list)
|
||||
{
|
||||
Console.WriteLine(item);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user