我们曾经认识过阵列(array),
今天来介绍他的兄弟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生肖!
鼠
牛
虎
兔
龙
蛇
马
羊
猴
鸡
狗
猪
他的初始值给法跟阵列十分相似,但她不需要给初始长度
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生肖!
鼠
牛
虎
兔
龙
蛇
马
羊
猴
鸡
狗
猪
熊
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的总笔数
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生肖!
鼠
牛
虎
兔
龙
蛇
马
羊
猴
鸡
狗
猪
熊
象
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生肖!
鼠
牛
虎
兔
龙
蛇
马
羊
猴
鸡
狗
猪
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)
各位连假结束了,小夥伴们请振作起来,这礼拜咻一下就会飞过去了,快点去补补你们欠下的技术债。Tail...
昨天打完程序码後,出现下面的Bug: Assets/LineRendererSetting.cs(5...
在没有网路的年代,人们获取资讯的来源可能是报章杂志、广播电视等等,报章杂志的排版需要符合出版业的要求...
今晚你要选哪一道菜呢? 初识料理这件事 各位第一次下厨,是帮自己泡面、煎荷包蛋或炒饭呢? 关於料理的...
tags: OC 30 day NULL 可以作为指针变量的值。如果一个指针变量的值是NULL值,代...