【C#】基础——数组(Array)

话不多说……直接看代码,代码里面有注释~~~

using System;

namespace Arrayy
{

    class ParamArray
    {
        public int AddElements(params int[] arr)
        {
            int sum = 0;
            foreach(int i in arr)
            {
                sum += i;
            }
            return sum;
        }
    }

    class Program
    {
        public void Hang()
         {
                Console.WriteLine("-------------------------------------------------");
         }

        //一维数组
        public void Array_one()
        {
             
            int[] a = new int[10];
            for (int i = 0; i < 10; i++)
            {
                a[i] = i + 100;
            }
            foreach (int j in a)
            {
                int i = j - 100;
                Console.WriteLine("a[{0}] : {1}", i, j);
            }
            for (int i = 0; i < 10; i++)
                Console.WriteLine("a[{0}] : {1}", i, a[i]);                
        }

        //二维数组
        public void Array_two()
        {
            string[,] s = new string[3, 2]
            {
                {"wzq","01" },
                { "xrb","02"},
                { "mdh","03"}
            };
            for(int i = 0; i < 3; i++)
            {
                for(int j=0;j<2;j++)
                {
                    Console.Write("{0}\t", s[i, j]);
                }
                Console.WriteLine();
            }
        }

        //交错数组
        public void Array_jiaocuo()
        {
            int[][] arr = new int[3][];
            arr[0] = new int[] { 1, 2, 3, 4, 5 };
            arr[1] = new int[] { 1, 2, 3, 4, 5 };
            arr[2] = new int[] { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 };
            for(int i = 0; i < 3; i++)
            {
                for(int j = 0; j < arr[i].Length; j++)
                {
                    Console.WriteLine("arr[{0}][{1}] : {2}", i,j, arr[i][j]);
                }
            }
        }
        double getAverage(int[] arr,int size)
        {
            int sum = 0;
            for (int i = 0; i < size; i++)
                sum += arr[i];
            return (double)sum / size;
        }
        //传递数组给函数
        public void SendArray()
        {
            Program n = new Program();
            int[] balance = new int[] { 1000, 2, 3, 17, 50 };
            double avg;
            avg = n.getAverage(balance, balance.Length);
            Console.WriteLine("average : {0}", avg);
        }

        //参数数组
        public void Param()
        {
            ParamArray P = new ParamArray();
            int sum = P.AddElements(512, 720, 250, 567, 889);
            Console.WriteLine("sum = {0}", sum);
        }

        public void ArrayClass()
        {
            Program P = new Program();
            int[] a = new int[5] { 1, 2, 3, 4, 5 };
            Console.WriteLine("--------------属性--------------");
            Console.WriteLine("获取一个值,该值指示数组是否带有固定的大小\tIsFixedSize : {0}", a.IsFixedSize);
            Console.WriteLine("获取一个值,指示数组是否只读\tIsReadOnly : {0}", a.IsReadOnly);
            Console.WriteLine("获取一个32位整数,该值表示所有维度数组元素总数\tLength : {0}", a.Length);
            Console.WriteLine("获取一个64位整数,该值表示…………同上\tLongLength : {0}", a.LongLength);
            Console.WriteLine("获取数组的秩(维度)\tRank : {0}",a.Rank);
            P.Hang();
            Console.WriteLine("--------------方法--------------");
            Console.WriteLine("参考:微软C#文档");            
            int[] list = { 34, 72, 13, 44, 25, 30, 10 };
            Console.Write("原始数组 : ");
            foreach(int i in list)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();

            //逆转数组
            Array.Reverse(list);
            Console.Write("逆转数组 : ");
            foreach(int i in list)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();

            //排序数组
            Array.Sort(list);
            Console.Write("排序数组 : ");
            foreach(int i in list)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();

            //清空数组
            Array.Clear(list, 0, 5);
        }

        static void Main(string[] args)
        {
            
            Program word = new Program();
            Program num = new Program();

            num.Array_one();
            word.Hang();
            //---------------------------------------------------------------
            //二维数组
            num.Array_two();
            word.Hang();

            //---------------------------------------------------------------
            //交错数组
            num.Array_jiaocuo();
            word.Hang();

            //---------------------------------------------------------------
            //传递数组给函数
            num.SendArray();
            word.Hang();

            //---------------------------------------------------------------
            //参数数组
            num.Param();
            word.Hang();

            //---------------------------------------------------------------
            //Array类
            num.ArrayClass();
            word.Hang();

            Console.ReadKey();
            //Console.WriteLine("Hello World!");
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值