[Day15]C# 鸡础观念- 多笔资料的好朋友~List

我们曾经认识过阵列(array),
今天来介绍他的兄弟List吧

List< T >/images/emoticon/emoticon07.gif

  • T的部份我们可以用其他型别来替换他
  • 常见的方式是将string或int,甚至是将class做成物件模型来使用他
  • 非常适合用於foreach回圈

范例:我们将十二生肖填入List中

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //宣告一个List
            List<string> myList = new List<string>() { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };

            Console.WriteLine("出来吧12生肖!");
            //我们使用foreach回圈来列出List的值
            foreach(string item in myList)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
    }
 }

结果:

出来吧12生肖!











他的初始值给法跟阵列十分相似,但她不需要给初始长度

新增资料/images/emoticon/emoticon12.gif

  • 如果我们需要新增资料进到List呢,可以使用.Add()来新增单笔资料
  • 如果需要新增多笔资料就须使用AddRange()

范例:有一天,科学家发现,原来一年有13个月,因此必须帮第十三年补上熊的生肖

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //宣告一个List
            List<string> myList = new List<string>() { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };

            //使用Add()新增熊
            myList.Add("熊");

            Console.WriteLine("出来吧13生肖!");
            //我们使用foreach回圈来列出List的值
            foreach(string item in myList)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
    }
 }

结果:

出来吧13生肖!












范例:後来科学家发现,一年不只13个月,实际上有15个月,所以还要补上象、鱼这两种动物

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //宣告一个List
            List<string> myList = new List<string>() { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };
            //宣告一个要新增的List
            List<string> newList = new List<string>() { "熊", "象", "鱼" };

            //AddRange()将newList新增到myList中
            myList.AddRange(newList);

            //使用Count()来表示List笔数
            Console.WriteLine("出来吧" + myList.Count() + "生肖!");
            //我们使用foreach回圈来列出List的值
            foreach (string item in myList)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
    }
  }

结果:

出来吧15生肖!














我们这边多使用了myList.Count()来列出目前List的总笔数

移除资料/images/emoticon/emoticon16.gif

  • 单笔移除的方法我们使用Remove()
  • 多笔移除则是使用RemoveRange(第几笔,共删几笔)

范例:科学家发现原来是仪器故障才会算成15年,实际上只有14年,所以要把鱼拿掉

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //宣告一个List
            List<string> myList = new List<string>() { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪", "熊", "象", "鱼" };

            //使用Remove()移除鱼
            myList.Remove("鱼");

            //使用Count()来表示List笔数
            Console.WriteLine("出来吧" + myList.Count() + "生肖!");
            //我们使用foreach回圈来列出List的值
            foreach (string item in myList)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
    }
 }

结果:

出来吧14生肖!













范例:後来科学家又发现,原来研究仪器是玩具店买的,根本没有那麽多个月,实际上只有12个月,所以要把动物复原

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CsharpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //宣告一个List
            List<string> myList = new List<string>() { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪", "熊", "象", "鱼" };

            //由於第一笔是从0算起,所以要从12开始删3笔
            myList.RemoveRange(12,3);

            //使用Count()来表示List笔数
            Console.WriteLine("出来吧" + myList.Count() + "生肖!");
            //我们使用foreach回圈来列出List的值
            foreach (string item in myList)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
    }
 }

结果:

出来吧12生肖!











进阶用法:结合class/images/emoticon/emoticon27.gif

  • 我们可以利用class的属性来建立资料模型
  • 将资料模型做成List

范例:我们将12个月与生肖结合吧

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CsharpDemo
{
    //建立一个class
    class DataModel
    {
        //月份
        public int month;
        //动物
        public string animal;

    }
    class Program
    {
        static void Main(string[] args)
        {
            //宣告一个List
            List<DataModel> dataList = new List<DataModel>();
            //宣告一个List
            List<string> myList = new List<string>() { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };

            //宣告整数来算月份
            int Count = 1;
            //我们使用foreach回圈来填写dataList的值
            foreach (string item in myList)
            {
                //新增一笔新的资料所以我们要使用new
                dataList.Add(new DataModel
                {
                    month = Count,
                    animal = item
                });

                //进入下个月
                Count++;
            }

            //使用Count()来表示List笔数
            Console.WriteLine("出来吧" + dataList.Count() + "生肖!");
            //我们使用foreach回圈来列出List的值
            foreach (DataModel item in dataList)
            {
                //这边需要用item.物件名称,才有办法取到值
                Console.WriteLine(item.month + "月 , 属 :" + item.animal);
            }
            Console.ReadKey();
        }
    }
  }

结果:

出来吧12生肖!
1月 , 属 :鼠
2月 , 属 :牛
3月 , 属 :虎
4月 , 属 :兔
5月 , 属 :龙
6月 , 属 :蛇
7月 , 属 :马
8月 , 属 :羊
9月 , 属 :猴
10月 , 属 :鸡
11月 , 属 :狗
12月 , 属 :猪


<<:  电子书阅读器上的浏览器 [Day16] 网页汇出成 epub 档案 (II)

>>:  虹语岚访仲夏夜-2(专业的小四篇)

Day 22 - Tailwind Plugin 使用 (一) => Aspect Ratio、Line Clamp

各位连假结束了,小夥伴们请振作起来,这礼拜咻一下就会飞过去了,快点去补补你们欠下的技术债。Tail...

Day 24. VR菜单2

昨天打完程序码後,出现下面的Bug: Assets/LineRendererSetting.cs(5...

资料传输格式 JSON

在没有网路的年代,人们获取资讯的来源可能是报章杂志、广播电视等等,报章杂志的排版需要符合出版业的要求...

大共享时代系列_004_共享料理资讯

今晚你要选哪一道菜呢? 初识料理这件事 各位第一次下厨,是帮自己泡面、煎荷包蛋或炒饭呢? 关於料理的...

iOS APP 开发 OC 第七天, nil 跟 NULL 一样吗?

tags: OC 30 day NULL 可以作为指针变量的值。如果一个指针变量的值是NULL值,代...