加载中...

列表的增删改查遍历

  1. 两种遍历方法:for和foreach遍历
点击查看代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;

namespace test_12_列表
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<int> lst = new List<int>() { 666, 45,88,45,887,55 };
            lst.Add(1);
            for (int i = 0; i < lst.Count; i++)
            {
                Console.WriteLine(lst[i]);
            }
            //foreach (int temp in lst)
            //{
            //    Console.WriteLine(temp);
            //}

        }
    }
}

1. 一系列增删改查顺序
点击查看代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;

namespace test_12_列表
{
    internal class Program
    {
        static void ShouList(List<int> list) 
        {
            foreach (int temp in list)
            {
                Console.WriteLine(temp);
            }
        }
        static void Main(string[] args)
        {
            List<int> lst = new List<int>() { 666, 45,88,45,887,55};
            //lst.Remove(45);//移除匹配到的第一个数据
            //for (int i = 0; i < lst.Count; i++)
            //{
            //    Console.Write(lst[i]+" ");
            //}
            /*lst.RemoveAt(45);*///移除索引的数据
            //ShouList(lst);//
            //Console.WriteLine(lst.IndexOf(45));.//从前往后查找
            //Console.WriteLine(lst.LastIndexOf(45));//查找45这个数据最后出现的索引
            //
            lst.Sort();//排序
            ShouList(lst);

        }

        private static void ShouList()
        {
            throw new NotImplementedException();
        }
    }
}

posted @ 2024-08-03 14:50  神乐羊  阅读(24)  评论(0)    收藏  举报