1. 程式人生 > 實用技巧 >C# Array基類 Length LengthTo BinarySearch Sort 方法的學習

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 }