zoukankan      html  css  js  c++  java
  • c#中list使用示例

        protected void Page_Load(object sender, EventArgs e)
        {
            List<string> studentNames = new List<string>();
            studentNames.Add("John");
            studentNames.Add("Mary");
            studentNames.Add("Rose");
    
            //显示各元素
            foreach (string item in studentNames)
            {
                Response.Write(item);
                Response.Write("<br/>");
            }
            Response.Write("<br/><br/>");
    
            //List转换成符号分隔字符串
            string studentAllName = string.Join(",", studentNames.ToArray());
            Response.Write(studentAllName);
            Response.Write("<br/><br/>");
    
            List<decimal> studentScore = new List<decimal>();
            studentScore.Add(100);
            studentScore.Add(98);
            studentScore.Add(59);
            //排序
            studentScore.Sort();
            //反转排序
            studentScore.Reverse();
            //显示各元素
            foreach (decimal score in studentScore)
            {
                Response.Write(score);
                Response.Write("<br/>");
            }
            //总计SUM
            Response.Write("总分" + studentScore.Sum());
            Response.Write("<br/>");
            //List中是否存在
            Response.Write(studentScore.Exists(MatchPRE));
            Response.Write("<br/><br/>");
    
            //List转换成JSon
            List<Student> list = new List<Student>();
            for (int i = 0; i < 5; i++)
            {
                Student a = new Student();
                a.Name = "张三" + i;
                a.Age = i;
                a.Sex = "男";
                list.Add(a);
            }
           string  json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(list);
           Response.Write(json);
           Response.Write("<br/><br/>");
    
        }
    
        private static bool MatchPRE(decimal p)//条件匹配函数,list1中每个元素都会传入P中                                                                           //匹配后函数返回
        {
            if (p == 100)//此句为匹配条件,如果匹配,返回,你可以随意更改成你想要的值
                return true;
            else
            {
                return false;
            }
        }
    
        public struct Student
        {
            public string Name;
            public int Age;
            public string Sex;
        }

  • 相关阅读:
    POJ 1860 Currency Exchange (Bellman ford)
    POJ 1502 MPI Maelstrom (最短路)
    2015 Multi-University Training Contest 2 1006(DFS)
    HDU 1495 非常可乐(枚举+DFS)
    HDU 5289 Assignment(单调队列)
    ACDream 1734 Can you make a water problem?(贪心)
    ACDream 1735 输油管道
    ACDream 1726 A Math game (折半查找)
    CSU 1602 Needle Throwing Game (投针问题)
    CSU 1604 SunnyPig (BFS)
  • 原文地址:https://www.cnblogs.com/smartsmile/p/6234227.html
Copyright © 2011-2022 走看看