zoukankan      html  css  js  c++  java
  • 泛型集合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

    运行结果:

  • 相关阅读:
    加载与隐藏显示
    Task 自我总结认知
    修复SDF数据库引擎C#代码
    Windows防火墙开启后 ping不通了 的解决方案
    C# 串口
    WPF DataGrid中单元格运用Combobox的示例
    组合模式
    适配器模式
    【转载】.net 动态代理
    python数组操作
  • 原文地址:https://www.cnblogs.com/chenyanlong/p/6505001.html
Copyright © 2011-2022 走看看