zoukankan      html  css  js  c++  java
  • C#数组/集合/泛型集合

    数组: 必须规定类型,规定长度
    1.定义
    int[] i = new int[5];
    int[] j = new int[] { 1, 2, 3, 4, 5 };
    string[] k = new string[] { "a", "b", "c", "d", "e" };
    2.数组的遍历
    Console.WriteLine(j[0]);
    for (int x = 0; x < j.Length; x++)
    {
    Console.WriteLine(j[x]);
    }

    foreach (int temp in j)
    {
    Console.WriteLine(temp);
    }
    foreach (var temp in k)
    {
    Console.WriteLine(temp);
    }
    3.数组赋值
    j[0] = 4;

    int[] x = new int[5];
    var x = j.Clone();
    4.二维数组
    int[,] erw = new int[2, 2];
    int[,] erw2 = new int[,] { { 4, 5 }, { 7, 8 } };
    Console.WriteLine(erw2[1, 0]);//[1][0]

    集合:不需要规定类型 / 长度
    1.定义

    ArrayList arr = new ArrayList();
    DateTime dt = new DateTime(2017, 11, 3);
    arr.Add("abc");
    arr.Add(123);
    arr.Add(true);
    arr.Add(dt);
    2.集合遍历
    foreach (var x in arr)
    {
    Console.WriteLine(x);
    }
    arr.Remove(123);
    arr.RemoveAt(1);
    arr.Reverse();//顺序翻转
    arr.Insert(2, "啦啦啦");
    Console.WriteLine(arr.Contains(123));
    foreach (var x in arr)
    {
    Console.WriteLine(x);

    泛型集合:规定类型,不规定长度

    1.定义

    List<string> a = new List<string>();

    2.赋值

    a.add(“11111”);

    3.插入

    a.insert(0."2222");

  • 相关阅读:
    cocos2d-x Tests讲解 Particle System(粒子系统)
    c++ 知识点
    详解C/C++函数指针声明
    VS中的路径宏 vc++中OutDir、ProjectDir、SolutionDir各种路径
    cocos2d-x学习知识点记录
    Ogre实现简单地形
    Ogre内部渲染流程分析系列
    gcc/g++编译
    gcc和g++的区别
    Hack with rewrite
  • 原文地址:https://www.cnblogs.com/yangchuanqi/p/7792124.html
Copyright © 2011-2022 走看看