zoukankan      html  css  js  c++  java
  • C# Array基类 Length LengthTo BinarySearch Sort 方法的学习

    有点多,试了很久才试出来的。

            static void Main(string[] args)
            {
                //Array抽象基类或者叫抽象父类的学习
                //Array 的类因为是抽象的,所以他是不能创建对象
                //即Array a = new Array();这样写是错误的。
                //但是他的派生类是可以创建对象的(虽然还没学,。。)
    
                //Length
                //表示用32位的整数表示Array的所有维数中元素的总和
                //说白了就是算长度
                int[] a = new int[] { 1, 2, 3, 4, 5, 6 };
                Console.WriteLine("Length={0}",a.Length );
                //这里要注意,Length可以直接在a后面写,是因为Array是所有数组的基类,
                // a作为数组是Array的子类,继承了父类的方法。
    
                //LongLength
                //获得的64位从整数,就是可以表示更大的空间,和Length使用方法一致。
                Console.WriteLine("LongLength={0}", a.LongLength);
    
    
                //.Rank获取Array 的维数
                int[] b = new int[] { 3 };
                int[,] bb = new int[,] { { 1 }, { 2 } };
                int[][] bbb = new int[3][];
                int[,,] bbbb = new int[1,2,3] ;
    
                Console.WriteLine("数组维数={0}", b.Rank );
                Console.WriteLine("数组维数={0}", bb.Rank );
                Console.WriteLine("数组维数={0}", bbb.Rank);
                //交错数组通过.Rank的方法得出的结果是1而不是2,是因为我们没有填满它吗?我们在下面继续测试一下。
                Console.WriteLine("数组维数={0}", bbbb.Rank);
    
                bbb[0]=new int[3]{ 1,2,3};
                bbb[1] = new int[4] { 1, 2, 3 ,4};
                bbb[2] = new int[2] { 1, 2};
                Console.WriteLine("数组维数={0}", bbb.Rank);
                //可以看到,就算我们都写满了,交错数组用.Rank的方法还是1,看来以后交错数组不能用这个测维数了
    
                //Array.Copy
                //复制粘贴方法
                int[] c = new int[] { 5,4,1,8,8};
                int[] cc = new int[] { 5, 4,2,5,0 };
                int[] ccc = new int[] { 5, 4 };
                int[] cccc = new int[] {6,3,6,6,3,4,2 };
                
                //基本格式
                //Array.Copy(Array1,Array2.n);
                //从Array1的第一个数据开始复制并粘贴到Array2上面去,这种操作依次进行n个
                Array.Copy(c,cc,3);
                foreach (int i in cc)//输出cc检测是否成功copy
                {
                    Console.WriteLine("{0}", i);
                }
    
                //如果Copy的数超过了其上限会怎么样
                //重新还原数组
                /*
                    cc = new int[] { 5, 4, 2, 5, 0 };
                    Array.Copy(cc, ccc, 5);
                    foreach (int i in cc)//输出cc检测是否成功copy
                    {
                        Console.WriteLine("{0}", i);
                    }
                 */
                //这里我们尝试了一下,结果报错了,这说明Copy方法使用的时候虽然语法上没有报错,但是如果超过了还是会越界的
    
                //重新还原数组
                cc = new int[] { 5, 4, 2, 5, 0 };
    
                //Array.BinarySearch方法,在指定的Array里面搜索指定的item项
                //Array.BinarySearch(Array1.item);
                Array.BinarySearch(c,'5');
                Console.WriteLine("{0}", Array.BinarySearch(c, '5'));
                //这里让BinarySearch在c数组中间找字符5,自然是没有的,所以返回值是-6
    
                Array.BinarySearch(c, 5);
                Console.WriteLine("{0}", Array.BinarySearch(c, 5));
                //而这里返回值是-4,如果从高位向低位数的话就是-4
                //为了验证BinarySearch是从哪里开始数的,我们再是一次
    
                Array.BinarySearch(cc, 5);
                Console.WriteLine("{0}", Array.BinarySearch(cc, 5));
                //输出结果为3
    
                /*
                 NET.Framework4.8
                 返回值:
                 Int32
                如果找到 value,则为指定 array 中的指定 value 的索引;否则为负数。
                如果找不到 value 且 value 小于 array 中的一个或多个元素,则返回的负数是大于 value 的第一个元素的索引的按位求补。
                如果找不到 value 且 value 大于 array 中的所有元素,则返回的负数是(最后一个元素的索引加 1)的按位求补。
                如果使用非排序的 array 调用此方法,返回值则可能不正确并且可能会返回负数,即使 value 存在于 array 中也是如此。
                 */
                //根据4.8给出的解释,如果没有排序,返回值可能不正确
                //所以我们现在先了解一下排序方法
    
                //Array.Sort自动排序方法
    
                /*  
                 *  引用数据
                int[] c = new int[] { 5,4,1,8,8};
                int[] cc = new int[] { 5, 4,2,5,0 };
                int[] ccc = new int[] { 5, 4 };
                int[] cccc = new int[] {6,3,6,6,3,4,2 };
                */
    
                ccc = new int[] {5,4 };
                cc = new int[] { 5,2,0,1,4};
                Array.Sort(c);
                //第一种方法,直接对()内的数组排序
                Array.Sort(cccc,1,4);
                //第二种方法,对指定的函数的指定部分,即从0到6开始排序。
                Array.Sort(ccc ,cc);
                //第三种方法,
                //其中要注意的是
                /*
                 * Array.Sort(Key,items);
                 Key
                 一维 Array,其中包含要排序的关键字。
                items
                Array
                一维 Array,其中包含与 keysArray 中每个关键字对应的项。
                -或- 如果为 null,则只对 keysArray 进行排序。
                 */
                //则说明需要保证
    
                foreach (int i in c)
                {
                    Console.WriteLine("{0}",i);//证明其已经按照从小到大的顺序排列了。
                }
    
                Console.WriteLine("/////////////////////////////////");//分割线。
                foreach (int i in cccc)
                {
                    Console.WriteLine("{0}", i);//证明其已经按照从小到大的把第一位到第四位顺序排列了。
                }
    
                Console.WriteLine("/////////////////////////////////");//分割线。
                foreach (int i in ccc)
                {
                    Console.WriteLine("{0}", i);//这里由于Key Array没有包括 items Array的全部项,所以只对Key Array排序了
                }
    
                ccc = new int[] { 1,5,0,2,4 };
                cc = new int[] { 5, 2, 0, 1, 4 };
                //当Key Array  完全包括了 items Array时,不!!会!!对两者进行排序
                Array.Sort(ccc, cc);
                Console.WriteLine("/////////////////////////////////");//分割线。
                foreach (int i in ccc)
                {
                    Console.WriteLine("{0}", i);//Key Array 排序了
                }
                foreach (int i in cc)
                {
                    Console.WriteLine("{0}", i);//items Array 没有排序
                }
    
                //但是如果Key Array的项完全包括了 items Array的项并且还有items 没有的项的时候,就会报错
    
                //例如
                /*
                 ccc = new int[] { 1, 5, 0, 2, 4 ,9};
                cc = new int[] { 5, 2, 0, 1, 4 };
                Array.Sort(ccc, cc);
                Console.WriteLine("/////////////////////////////////");//分割线。
                foreach (int i in ccc)
                {
                    Console.WriteLine("{0}", i);//这里由于Key Array没有包括 items Array的全部项,所以只对Key Array排序了
                }
                
                 */
    
                Console.WriteLine("////////////////////////////////////////");
                Array.Sort(cccc);//对cccc整体进行排序
                Array.Sort(cc);//对cc整体进行排序
                //现在我们已经对c cc ccc cccc从大到小排列了
                //已经差不多可以使用Array.BinarySearch了
    
                /*
                int[] c = new int[] { 1,4,5,8,8};
                ccc = new int[] { 0,1,2,4,5 };和cc一致
    
                int[] cccc = new int[] {2,3,3,4,6,6,6 };
    
                 */
    
                Console.WriteLine("{0}", Array.BinarySearch(c, 5));//输出结果正确,为2
                Console.WriteLine("{0}", Array.BinarySearch(cc, 5));//输出结果正确,为4
                Console.WriteLine("{0}", Array.BinarySearch(cccc, 4));//输出结果正确,为3
    
    
    
    
    
    
            }
    悟已往之不谏,知来者之可追
  • 相关阅读:
    面向对象进阶
    20191011作业
    2019.10.10作业
    类的继承
    面向对象
    2019.10.09作业
    pandas模块
    [BZOJ 2190][SDOI2008]仪仗队(欧拉函数)
    [BZOJ 2729][HNOI2012]排队(组合数学+高精)
    [BZOJ 1491][NOI2007]社交网络(Floyd)
  • 原文地址:https://www.cnblogs.com/ljh-study/p/13636208.html
Copyright © 2011-2022 走看看