• 泛型练习


    1. 实现装箱和拆箱操作  数值类型赋值给object、object赋值给数值

    2. ArrayList的使用

      (1)创建一个ArrayList对象

      (2)Add向ArrayList对象添加各种类型的元素

      (3)删除元素  Remove、RemoveAt

      (4)制定位置插入元素Insert

      (5)循环遍历,打印ArrayList对象中的元素,并且答应集合中元素的个数Count

    3. 创建强类型List<T>,string和int类型,重复上面的同样的操作。

    4. namespace ConsoleApp1
      
      {
      
          class Program
      
          {
      
      
              static void Main(string[] args)
      
              {
      
                  int i = 4;
      
                  object x = (object)i;
      
                  Console.WriteLine(x);
      
                  int y = (int)x;
      
      
      
                  ArrayList arr = new ArrayList();
      
                  Person p = new Person();
      
                  //arr.Add(p);
      
                  arr.Add(4);
      
                  arr.Add("hello");
      
                  arr.Add(5.6);
      
      
      
                  foreach(object item in arr)
      
                  {
      
                      Console.WriteLine(item);
      
                  }
      
      
      
                  List<int> intArr = new List<int>();
      
                  arr.Add(5);
      
                  Console.WriteLine(intArr.Count);
      
                  arr.Remove(5);
      
                  Console.WriteLine(intArr.Count);
      
      
                  List<string> stringArr = new List<string>();
      
                  stringArr.Add("hello");
      
                  stringArr.Add("world");        
      
      
                  List<Person> personArr = new List<Person>();
      
                  personArr.Add(p);
      
      
                  Console.ReadKey();
      
              }
      
          }
      
          public class Person
      
          {
      
          }
      
      }

      运行结果:

  • 相关阅读:
    LRU算法简介
    linux下安装nginx+php+mysql环境 详细教程
    CentOS 6.6编译安装Nginx1.6.2+MySQL5.6.21+PHP5.6.3
    unicode 格式 转汉字
    js 操作cookie
    哈希函数
    php商城秒杀活动
    php 栈、 出栈、入栈
    php单例模式
    封装PHP增删改查方法
  • 原文地址:https://www.cnblogs.com/programme-maker/p/12020959.html
Copyright © 2020-2023  润新知