List<T>集合的常见用法(控制台)

static void Main(string[] args)
        {
            List<string> list = new List<string>();
            //向集合中添加一个值
            list.Add("abc");
            list.Add("dcf");
            list.Add("aaa");
            list.Add("bbb");
            list.Add("ccc");

            //List集合中值长度为多少
            int i = list.Count;
            Console.WriteLine(i);

            //移除的是集合中第一个找到的匹配项
            if (list.Remove("abc"))
            {
                Console.WriteLine("成功");
            }
            else
            {
                Console.WriteLine("失败");
            }

            //遍历集合中的每一个元素
            foreach (string item in list)
            {
                Console.WriteLine(item);
            }

            //Contains集合中包含不包含一个值
            if (list.Contains("abc"))
            {
                Console.WriteLine("有");
            }
            else
            {
                Console.WriteLine("没有");
            }

            //集合中的元素翻转
            list.Reverse();
            foreach (string item in list)
            {
                Console.WriteLine(item);
            }

            //将集合中的元素复制到新的数组中
            string[] str = list.ToArray();
            //增加元素的方法
            list.Insert(0, "0");
            foreach (string item in list)
            {
                Console.WriteLine(item);
            }

            List<int> list1 = new List<int>() { 1,2,3,4,5,6,7 };
            List<string> list2 = new List<string>();

            //得到一个名字的数组
            string[] names = "a,b,c,d,e,f,g,h,i,j,k,l,m,n".Split(',');
            //集合的遍历
            List<string> nems = new List<string>();
            for (int j = 0; j < names.Length; j++)
            {
                nems.Add(names[j]);
            }
            nems.RemoveAll(e => e == "l");
            foreach (string item in nems)
            {
                Console.WriteLine(item);
            }
            //改元素的值
            nems[3] = "BBBB";
            //清空所有的元素
            nems.Clear();

            string kkk = "传说在传智播客.net部门有个讲师叫赵晓虎,赵晓虎很帅,很纯洁,赵晓虎是智慧和勇敢的化身.第一次听人这么说我不相信,于是我怀着试试看的心情来到了传智播客,见到了传说中的赵晓虎。哇塞,赵晓虎岂止是帅,简直就是帅的无法用言语来形容";
            int index = -1;
            do
            {
                index = kkk.IndexOf("赵晓虎", index + 1);
                if (index != -1)
                {
                    Console.WriteLine(index);
                }
            }
            while (index != -1);
            //第二种方法
            List<int> num = new List<int>();
            do
            {
                index = kkk.IndexOf("赵晓虎", index + 1);
                if (index != -1)
                {
                    num.Add(index);
                }
            } while (index != -1);
            Console.WriteLine("=====================");
            foreach (int item in num)
            {
                Console.WriteLine(item);
            }
            //第三种方法
            Console.WriteLine("======================");
            int[] ints = Search(kkk, "赵晓虎");
            for (int s = 0; s < ints.Length; s++)
            {
                Console.WriteLine(ints[s]);  
            }
            Console.ReadKey();
        }
        public static int[] Search(string s, string s1)
        {
            List<int> nums = new List<int>();
            int index = -1;
            do
            {
                index = s.IndexOf(s1, index + 1);
                if (index != -1)
                {
                    nums.Add(index);
                }
            } while (index!=-1);
            return nums.ToArray();
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值