泛型集合List(C#)

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ListDemo
 7 {
 8 class Program
 9 {
10 static void Main(string[] args)
11 {
12 //泛型集合的基本使用
13 List<int> scoreList = new List<int>();
14 scoreList.Add(98);
15 scoreList.Add(28);
16 scoreList.Add(38);
17 scoreList.Add(78);
18 scoreList.Add(88);
19 //List<string> nameList = new List<string>();
20 scoreList.Insert(2, 70);
21 //获取元素总数
22 Console.WriteLine("获取元素总数:"+scoreList.Count );
23 Console.WriteLine("_________________");
24 //Console.ReadLine();
25 //遍历集合
26 Console.WriteLine("遍历输出在第一种方法:");
27 foreach (int score in scoreList)
28 {
29 Console.WriteLine(score);
30 }
31 Console.WriteLine("_________________");
32 Console.WriteLine("遍历输出在第二种方法:");
33 for (int i = 0; i < scoreList.Count; i++)
34 {
35 Console.WriteLine(scoreList[i]);
36 }
37 //删除一个元素
38 Console.WriteLine("_________________");
39 Console.WriteLine(" 删除一个元素");
40 scoreList.Remove(70);
41 scoreList.Remove(2);
42 for (int i = 0; i < scoreList.Count; i++)
43 {
44 Console.WriteLine(scoreList[i]);
45 }
46 Console.WriteLine("_________________");
47 Console.WriteLine("清除所有元素");
48 scoreList.Clear();
49 Console.WriteLine("获取元素总数:" + scoreList.Count);
50 Console.ReadLine();
51 }
52 }
53 }
View Code

 

运行结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值