sing System;
using System.Collections.Generic;
namespace Program
{
class Program
{
static void Main(string[] args)
{
List<Class1> a = new List<Class1>();
//Class1相当于你自己写的代码
var b = new List<int>();
var c = new List<int>(10) { 1, 3, 4 };//10代表列表的容量
c.Add(13);//添加一个数字到列表的最后一位,大括号里是初始值
Console.WriteLine(c[0]);//获取空位置会报错
int d = c.Capacity;//获取当前列表最大容量
int e = c.Count;//获取当前列表已用容量
Console.WriteLine(d+" "+e);
c.Insert(2, 13);//在2号位插入13,2号位以后的元素整体后移1位
c.Remove(13);//从第一个元素开始从前向后遍历,删除第一个遇到的元素,剩余数据向前移动
c.RemoveAt(0);//删除指定位置的元素
c.RemoveRange(0, 3);//从0号位开始,删除包括该位置在内的3个元素
int f = c.IndexOf(76);//从前向后遍历
int g = c.LastIndexOf(76); //从后向前遍历
//输出第一个遇到的与括号内元素相同的元素在列表中的位置
Console.WriteLine(f+" "+g);
c.Sort();//从小到大排序
bool h = c.Contains(3);//确定3是否存在于列表
Console.WriteLine(h);
foreach (var item in c)
{
Console.Write(item + " ");
}
Console.WriteLine();
for (int i = 0;i < c.Count;i++)
{
Console.Write(c[i]+" ");
}
Console.ReadKey();
Console.WriteLine("Hello World!");
}
}
}
2021-03-17
最新推荐文章于 2024-08-14 12:02:23 发布