C#list 中的一些方法

List<string> list1 = new List<string>();
        List<string> list2 = new List<string>();
        list1.Add("1");
        list1.Add("2");
        list1.Add("3");
        list1.Add("5");
        list2.Add("2");
        list2.Add("4");
  

        List<string> listjiaoji = list1.Intersect(list2).ToList<string>();//求交集,list1和list2的交集


        List<string> list1cha = list1.Except(list2).ToList<string>();//差集,list1有,list2没有


        List<string> list2cha = list2.Except(list1).ToList<string>();//差集,list2有,list1没有


        List<string> listtccf = list2.Union(list1).ToList<string>();//并集,剔除重复项


        List<string> listblcf = list2.Concat(list1).ToList<string>();//并集,包含重复项


        list2.Insert(0, "7"); //(int index,string item) 将元素插入到指定索引处


        list2.Remove("2");//(string item)移除某个元素


        list2.RemoveAt(0);//(int index)移除索引处的元素


        list1.RemoveRange(1, 1);//(int index,int count)移除一定范围的元素,从索引处开始移除数量的元素


        list1.Sort();//排序正序(字母或者数字)


        list1.Reverse();//排序反序(字母或者数字)


        //list1.Clear();//清空所有元素


       bool f=list1.Exists((string s) => s == "1");//判断集合中是否有该元素


       string l=list1.Find((string s) => s == "1");


       int sd= list1.FindIndex((string s) => s == "1");//第一个匹配的元素从零开始的索引


       int sd1 = list1.FindLastIndex((string s) => s == "1");//最后一个匹配的元素从零开始的索引


        int flag = listblcf.BinarySearch("2");//返回元素的索引


         // int index=  list1.IndexOf((string s) => s == "1");


        is5.Value = listjiaoji.Count.ToString();//返回元素数目
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值