第二十一讲:简单数组

来源:互联网 发布:延边网络电视台 编辑:程序博客网 时间:2024/06/05 15:34
回顾
    1. 我们自己写的类就是自定义类,它属于引用类型,而结构是值类型
    2. 构造函数的主要作用是初始化对象中的数据成员
    3. 多态的两种表现形式是重载实现的编译时多态和重写实现的运行时多态
        重载的特点:方法名必须相同,返回类型可以不同,参数列表必须不同
        重写的特点:方法名,返回类型,参数列表全都必须相同
    4. 访问修饰符实现了封装。之所以要封装是因为在类中有一些成员没必要对外公开
    5. 接口规定了对象通信的契约,抽象类规定了程序内部实现类的公共行为的规范
    
主要内容
    1. 数组的概念以及为什么使用数组
        即是一组相同类型数据组合在一起,使用一个通用的名称,通过分配下标访问元素
        数组的下标从0开始
        
    2. 数组的申明和初始化
        申明时必须明确数组中存储的数据类型
        语法: 数据类型[] 数组变量名
        
        初始化必须说明数组的大小,也可以一次性完成填充数据的操作
        示例:
            int[] myInt;  //申明数组变量
            
            myInt=new int[7]; //初始化数组
            
            int[] myInt1 = new int[7]; //一次性申明
            
            int[] myInt2 = new int[7]{1,2,3,4,5,6,7}; //申明并填充
            
            int[] myInt3 = new int[]{1,2,3,4,5,6,7};
            
            int[] myInt4 = {1,2,3,4,5,6,7};
            
            
    3. 访问数组
        数组在声明和初始化后,就可以使用索引器访问其中的元素了
        数组只支持有整型参数的索引器
        索引从0开始
        索引错误将引发下标越界错误
        使用length属性可以获得数组长度
        
    4. Foreach迭代
        foreach可以隐藏集合类型的内部实现方法,从而更加有效地处理集合类型元素。
        
        通过foreach循环语句,可以循环列举某集合的元素,并对各元素执行一次相关的嵌入语句
        语法:
            foreach(类型 迭代变量 in 被迭代的表达式或集合变量)
            {
                处理语句
            }
        

    5. 向数组存放引用类型对象


看完笔记,写代码:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Array1{    public class Persons    {        string _name; //姓名        int _age;//年龄        /// <summary>        /// 姓名属性        /// </summary>        public string Name        {            get { return _name; }            set { _name = value; }        }                /// <summary>        /// 年龄属性        /// </summary>        public int Age        {            get { return _age; }            set { _age = value; }        }        public Persons() { }        /// <summary>        /// 构造函数        /// </summary>        /// <param name="str">姓名参数</param>        /// <param name="x">年龄参数</param>        public Persons(string str, int x) {            this._name = str;            this._age = x;        }    }}

上面的代码只是在做对象引用在数组的使用方式写的,下面还有

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Array1{    class Program    {        static void Main(string[] args)        {            //简单的建几个数组            //定义一个数组变量,明确数组要存储数据的数据类型            string str = "========最普通的创建数组的方式之一==============";            Console.WriteLine(str);            int[] Myarray;            Myarray = new int[5];            Myarray[0] = 12;            Myarray[1] = 23;            Myarray[2] = 56;            Myarray[3] = 34;            Myarray[4] = 45;            for (int i=0; i < Myarray.Length; i++)            {                Console.WriteLine(Myarray[i]);            }            str = "========最普通的创建数组的方式之二==============";            Console.WriteLine(str);            string[] Myarray2 = new string[] {"hello","everyone","哈罗!","大家好啊!"};            for (int i = 0; i < Myarray2.Length; i++)            {                Console.WriteLine(Myarray2[i]);            }            str = "========最普通的创建数组的方式之三==============";            Console.WriteLine(str);            double[] Myarray3 = { 12d, 23.45d, 456.789d, 789.099d };            for (int i = 0; i < Myarray3.Length; i++)            {                Console.WriteLine(Myarray3[i]);            }            str = "========最普通的创建数组的方式之四==============";            Console.WriteLine(str);            float[] Myarray4 = new float[3] { 1.2345f, 2.34f, 4.56f };            //这里使用数组的一个方法,可以获取数组指定维数中的项数,即长度            int x = Myarray4.GetLength(0);            for (int i = 0; i < x; i++) {                Console.WriteLine(Myarray4[i]);            }            str = "========最普通的创建数组的方式之五二维数组==============";            Console.WriteLine(str);            int[,] Myarray5 = new int[2, 3];            Myarray5[0, 0] = 12;            Myarray5[0, 1] = 34;            Myarray5[0, 2] = 45;            Myarray5[1, 0] = 234;            Myarray5[1, 1] = 345;            Myarray5[1, 2] = 456;            Console.WriteLine("*****循环输出方式一*****");            int y = Myarray5.GetLength(0);//二维数组中的第一维中的项数            int z = Myarray5.GetLength(1);//你懂的            for (int i = 0; i < y; i++) {                for (int o = 0; o < z; o++) {                    Console.Write(Myarray5[i,o]+"   ");                }                Console.WriteLine("\n=======隔离带=======");            }            Console.WriteLine("*****循环输出方式二*****");            int a = 0;            foreach (int i in Myarray5)            {                ++a;                Console.Write(i+"   ");                if (a % 3 == 0) { Console.WriteLine("\n=======隔离带======="); }            }            str = "\n\n========最普通的创建数组的方式之六对象引用==============";            Console.WriteLine(str);            Persons[] pe = new Persons[] {                 new Persons("张三",23),                new Persons("李四",21),                new Persons("王五",45),                new Persons("赵六",19)            };            Persons[] per ={                 new Persons("张三",23),                new Persons("李四",21),                new Persons("王五",45),                new Persons("赵六",19)            };            foreach (Persons p in pe) {                Console.WriteLine("我叫"+p.Name+",今年"+p.Age.ToString()+"岁了");            }            Console.WriteLine("\n======另一种写法也是可以的=========");            foreach (Persons p in per)            {                Console.WriteLine("我叫" + p.Name + ",今年" + p.Age.ToString() + "岁了");            }            Console.ReadKey();        }    }}

最后看执行结果:


0 0
原创粉丝点击