分享

V3.0维数组的声明

 时间剧毒 2019-04-29

using System;

using System.Collections.Generic;

using System.Text;

namespace V3._维数组的声明

{

    class Program

    {

        static void Main(string[] args)

        {

            /*--------------------------------------         申明             --------------------------------------*/

            int[] myintArray ={ 0,1,2,3,4,5,6,7,8,9};//初始化  用于已经变量内容声明

            //用于未知数组各元素值的一个声明,指定长度

            string[] mystringArray;

            mystringArray = new string[3] ;

            //元素

            const int a = 3;    //申明常量值

            string[] NameArray = new string[a] { "张三", "李四", "王五" };

            /*--------------------------------------           使用             --------------------------------------*/

            //使用数组中的元素是通过索引值来实现的

            Console.WriteLine("1.使用数组中的元素是通过索引值来实现的");

            Console.WriteLine(NameArray[0]);

            //数组元素重新赋值

            Console.WriteLine("2.数组元素重新赋值");

            NameArray[0] = "李六";

            //遍历元素

            Console.WriteLine("3.数组元素遍历元素 for ");

            for (int i = 0; i < NameArray.Length; i++) {

                Console.WriteLine(NameArray[i]);

            }

            Console.WriteLine("4.数组元素遍历元素 foreach");

                foreach (string name in NameArray)

                {

                    Console.WriteLine(name);

                };

             /*--------------------------------------            二维数组             --------------------------------------*/

                //二维数组声明

                string[,] Friend;

               //初始化第一种方法: 指定字面值,适用于已知元素内容

                string[,] Friend2 ={ { "张三", "男" }, { "李四", "女" }, { "王五", "男" }, { "赵六", "未知" }, { "李七", "女" } };

            //初始化第二种方法:  new关键字,适用于元素内容未知

                string[,] Friend3 = new string[5, 2];   //5行2列   

                //0,0   0,1

                //1,0   1,1

                //2,0   2,1

                //3,0   3,1

            //二维数组输出

                Console.WriteLine("--------------------------------------------------------------------------------------------");

             Console.WriteLine("5.二维数组输出");

             string[,] Friend5= new string[5, 2] { { "张三", "男" }, { "李四", "女" }, { "王五", "男" }, { "赵六", "未知" }, { "李七", "女" } };

             for (int i = 0; i < 5; i++) {

                 for (int j = 0; j < 2; j++)

                 {

                     Console.Write(Friend5[i, j] + "\t");

                 }

                 Console.WriteLine("");

             }

                 Console.WriteLine("");

                 Console.WriteLine(Friend5.GetUpperBound(0)); //该数组维度的 上线  

                 Console.WriteLine(Friend5.GetLowerBound(0)); //该数组维度的 下线  

                 //三维数组

                 //可以将 [3,2,2] 理解为3个二维数组,这个二维数组由2行2列构成

                 //有几层{}嵌套,那就是一个几维数组   

                 string[, ,] Friend4 = new string[3, 2, 2] { { { "0,0,0", "0,0,1" }, { "0,1,0", "0,1,1" } }, { { "1,0,0", "1,0,1" }, { "1,1,0", "1,1,1" } }, { { "2,0,0", "2,0,1" }, { "2,1,0", "2,1,1" } } };

                 Console.WriteLine("--------------------------------------------------------------------------------------------");

                 Console.WriteLine("6.三维数组输出");

                 for (int q = 0; q < 3; q++)

                 {

                     for (int i = 0; i < 2; i++)

                     {

                         for (int j = 0; j < 2; j++)

                         {

                             Console.Write(Friend4[q, i, j] + "\t");

                         }

                     }

                     Console.WriteLine("*******组*******");

                 }

                 Console.WriteLine(Friend4.Length); //数组包含元素的个数

                 Console.WriteLine(Friend4.GetUpperBound(0)); //该数组维度的 上线  

                 Console.WriteLine(Friend4.GetLowerBound(0)); //该数组维度的 下线  

                 Console.WriteLine(Friend4.GetUpperBound(1)); //该数组维度的 上线  

                 Console.WriteLine(Friend4.GetLowerBound(1)); //该数组维度的 下线  

                 Console.WriteLine(Friend4.GetUpperBound(2)); //该数组维度的 上线  

                 Console.WriteLine(Friend4.GetLowerBound(2)); //该数组维度的 下线  

                //查看 3维数组 第二组 1行1列

                 Console.WriteLine("查看 3维数组 第二组 1行1列");

                 Console.WriteLine(Friend4[2,1,1] );

                 Console.WriteLine(Friend4.GetValue (2,1,1));

            //查找数组元素

                 Console.WriteLine("--------------------------------------------------------------------------------------------");

                Console.WriteLine("7.查找数组元素");

                int[] myintArray1 ={ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12,13,1,2,3,4,5,6 };

                //想要查找是否存在某个元素

                //IndexOf 可以查找元素首次出现的位置

                //LastIndexOf   可以查找元素最后一次出现的位置,位置都是以从0开始索引的值

                //IndexOf   LastIndexOf 都返回一个索引值,为整型 int

                //IndexOf(参数1,参数2);参数1是我们要查找的数据 参数2是查找的元素

                //用来判断某个数组是否存在某个元素,存在返回>=0索引值,不存在返回-1

                int intresult;

                intresult = Array.IndexOf(myintArray1, 8);

                Console.WriteLine("第一次出现的索引值是{0},最后一次出现的索引值是{1}", Array.IndexOf(myintArray1,6), Array.LastIndexOf(myintArray1, 6));

                //BinarySearch(参数1,参数2);参数1是我们要查找的数据 参数2是查找的元素 不存在返回负值,速度比IndexOf块

                int intresult2 = Array.BinarySearch(myintArray1,20);

                Console.WriteLine("用BinarySearch,查找的索引值:" + intresult2);

                //Array 的 Contains 的方法 实际是对IList接口方法的实现,因此使用之前需要将数组转换为该对象 

                // 转换格式:(System.Collections.Ilist)myintArray.Contains(8);    //该数组是否包含 8

                //返回一个布尔值

                bool mybool;

                mybool = ((System.Collections.IList)myintArray1).Contains(22);

                if (mybool)

                {

                    Console.WriteLine("存在该元素");

                }

                else {

                    Console.WriteLine("不存在该元素");

                }

            //数组排序

                Console.WriteLine("--------------------------------------------------------------------------------------------");

                Console.WriteLine("8.数组排序");

             //Array中提供了 Sort  Reverse 进行操作

             //Array.Sort(数组);    

                //Array.Reverse(数组);

                int[] myintArray2 ={ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6 };

                Console.WriteLine("数组原数组");

                foreach (int i in myintArray2)

                {

                    Console.Write(i + "\t");

                }

                //利用Sort实现升序排序

                Array.Sort(myintArray2);

                Console.WriteLine("\n数组排序实现升序后是");

                foreach (int i in myintArray2)

                {

                    Console.Write(i+"\t");

                }

                //利用Reverse实现降序排序

                Array.Reverse(myintArray2);

                Console.WriteLine("\n数组排序实现升序后是");

                foreach (int i in myintArray2)

                {

                    Console.Write(i + "\t");

                }

                //数组的合并拆分

                Console.WriteLine("--------------------------------------------------------------------------------------------");

                Console.WriteLine("10.数组的合并拆分");

            //Array提供了copy方法,通过该方法可实现数组的合并和拆分,

            //使用格式: Copy方法有四个重载

            //1.Array.Copy(数组1,数组2,长度);    int 

            //2.Array.Copy(数组1,数组2,Int64);    long

                //3.Array.Copy(数组1,指定索引,数组2,指定索引,长度);  int

                //4.Array.Copy(数组1,指定索引,数组2,指定索引,长度);     long

                int[] num1 ={0,1,2,3,4,5,6,7,8 };

                int[] num2 ={ 10,11,12,13,14,15,16,17,18,19};

                Console.WriteLine("使用第一种重载");    

                int[] resultnum3 = new int[20];

                Array.Copy(num1, resultnum3, 7);  //表示将 num1中的数组元素从索引值0开始 取7个长度放入到 resultnum3中 ,resultnum3 中从索引值0开始

                foreach (int i in resultnum3)

                {

                    Console.Write(i + "\t");

                }

                Console.WriteLine("使用第二种重载");    

              int[] resultnum4= new int[20];

              Array.Copy(num2, 0, resultnum4, 5, 10);

              //从 num2索引值从0开始取10个长度,放入到resultNum4 中并从索引5开始存放

              foreach (int i in resultnum4)

              {

                  Console.Write(i + "\t");

              }

            Console.ReadKey();

        }

    }

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多