zoukankan      html  css  js  c++  java
  • 重温数据结构与算法(2) 编程中最常用,最通用的数据结构数组和ArrayList

      1,什么是数组?
      答:数组是一组同数据类型且可索引的数据的集合

      2,数组Array和ArrayList有何区别?
      答:数组是固定大小的,而ArrayList是可变大小的。

    一,数组的使用(适用于固定大小)

    namespace ArrayDemo1
    {
        class Program
        {
            static void Main(string[] args)
            {
                //1, 数组的声明和初始化,有两种方法
                //方法一:常规方法
                //string[] names=new string[5];//需要指定数组元素个数
                //names[0] = "a";
                //names[1] = "b";
                //names[2] = "c";
                //names[3] = "d";
                //names[4] = "e";
    
                //方法二:对象初始化列表
                string[] names = new string[] { "a","b","c","d","e"};//与方法一等价
    
                //2, 数组元素的设置和访问,有两种方法
                //方法一:直接存取法
                names[2] = "cc";//设置
                string myName=names[2];//存取
                
    
                //调用Array类的SetValue()和GetValue()方法
                //names.SetValue("cc",2);//设置
                //string myName1 = (string)names.GetValue(2);//存取,注意GetValue返回的类型是Object
    
                //3, 数组的遍历
                foreach (string name in names)//也可以使用for循环遍历
                {
                    Console.Write(name+" ");
                }
                Console.ReadKey();
    
                //程序输出结果为:
                /*
                 a b cc d e
                 */
            }
        }
    }

     二,ArrayList的使用(适用于动态增长的数组)

    namespace ArrayListDemo1
    {
        class Program
        {
            static void Main(string[] args)
            {
                //1, 创建一个ArrayList实例(对象)
                ArrayList grades = new ArrayList();//不需要指定数据项大小
    
                //2, 添加项,有两种添加方法
                //方法一:Add()方法
                grades.Add(100);//每次只能添加一个项,且添加在末尾
                grades.Insert(1,95);//添加在指定位置
    
                //方法二:AddRange()方法
                grades.AddRange(new int[]{90,80,70});//注意该方法参数为ICollection对象,即实现了接口ICollection
    
                //3, 移除项,有两种方法
                //方法一:Remove()方法
                if (grades.Contains(90))//移除前最好检查对象是否存在
                {
                    grades.Remove(90);
                    Console.WriteLine("90 be removed!");
                }
                else
                {
                    Console.WriteLine("Object not in ArrayList!");
                }
    
                //方法二:RemoveAt()方法,适用于知道元素的索引的情况下
                int pos = 0;
                pos = grades.IndexOf(80);//通过IndexOf()方法确定要移除元素的位置(索引)
                grades.RemoveAt(pos);
                Console.WriteLine("80 be removed!");
    
                //4, 遍历
                Console.WriteLine("The list of grades: ");
                foreach (Object grade in grades)
                {
                    Console.Write(grade+" ");
                }
                Console.ReadKey();
    
                //程序输出结果为:
                /*
                 100 95 70
                 */
            }
        }
    }
  • 相关阅读:
    mac os programming
    Rejecting Good Engineers?
    Do Undergrads in MIT Struggle to Obtain Good Grades?
    Go to industry?
    LaTex Tricks
    Convert jupyter notebooks to python files
    How to get gradients with respect to the inputs in pytorch
    Uninstall cuda 9.1 and install cuda 8.0
    How to edit codes on the server which runs jupyter notebook using your pc's bwroser
    Leetcode No.94 Binary Tree Inorder Traversal二叉树中序遍历(c++实现)
  • 原文地址:https://www.cnblogs.com/mcgrady/p/2595414.html
Copyright © 2011-2022 走看看