zoukankan      html  css  js  c++  java
  • C# 數據集合---1.數組

    數組arry
    1.一維數組
    namespace arry
    {
        class Myarry
        {
            /// <summary>
            /// 數組Arry
            /// </summary>
            /// <param name="args"></param>
            static void Main(string[] args)
            {
                //聲明數組datatype[] arryName  datatype指定被存儲在數組中的元素的類型  []指定數組的維度,秩指定數組的大小 arryName指定數組名稱
                int[] n = new int[10];  //初始化數組
               //賦值 n[0] = 111;
               // 聲明數組同時賦值           double[] balance = { 1,2,3,4};
               //創建并初始化數組 int []marks = new int[5] {1,2,3,4,5}
                //賦值一個數組變量到另一個目標數組變量中int []marks =new int[]{1,2,3,4,5};  int[] score = marks;
               // c#中,當創建一個數組時,每個數組元素為一個默認值初始化為0(int) 0.0(float)  false(bool) null(引用)
    
                //    可以聲明一個數組變量但不將其初始化,但將數組分配給此變量時必須使用new 運算符
                int i, j;
                /* 初始化數組N的元素 */
                for (i = 0; i < 10; i++)
                {
                    n[i] = i + 100;
                }
                /* 輸出每個數組元素的值 */
                for (j = 0; j < 10; j++)
                {
                    Console.WriteLine("Element[{0}] = {1}",j,n[j]);
                }
                Console.ReadKey();
            }
        }
    }
    2.多維數組
      int[,] arry2 = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };//二維數組
                int[, ,] arry3 = new int[2,2,3] { { { 1, 2, 3 }, { 4, 5, 6 } }, { { 7, 8, 9 }, { 10, 11, 12 } } };//三維數組
                Console.WriteLine(arry2[0,0]);//輸出結果為1
                Console.WriteLine(arry3[1,0,1]);//輸出結果為8
    3.交錯數組
    交錯數組是元素為數組的數組,交錯數組元素的維度和大小可以不同
    int [] [] Arr = new int [3][];  //實例化交錯數組
                Arr[0] = new int[] { 1, 2, 3 };  //賦值
                Arr[1] = new int[] { 4, 5, 6, 7 };
                Arr[2] = new int[] { 8, 9, 10, 11, 12 };
                Console.WriteLine(Arr[0][1]);//輸出結果為2
                Console.WriteLine(Arr[1][1]);//輸出結果為5
    4.傳遞數組給函數
    class Myarry2
        {
          double getAverage(int[] arr,int size)
            {
                int i;
                double avg;
                int sum = 0;
                for(i=0;i<size;++i)
                {
                    sum += arr[i];
                }
                avg = (double)sum/size;
                return avg;
            }
            /// <summary>
            /// 傳遞數組給函數,計算平均值
            /// </summary>
            /// <param name="args"></param>
            static void Main2(string[] args)
            {
                Myarry2 app = new Myarry2(); 
                int[] balance = new int[]{1,2,3,4,5};//帶有5個元素的int數組
                double avg;
                /* 傳遞數組的指針作為參數 */
                avg = app.getAverage(balance,5);
                Console.WriteLine("平均值為:{0}",avg);
                Console.ReadKey();
            }
        }
    5.參數數組
    如果不確定要傳遞給函數作為參數的參數數目,可用參數數組
    使用params關鍵字 params使用格式:
    public 返回類型 方法名稱(params 類型名稱[] 數組名稱)
    class ParamArray
        {
            public int AddElements(params int[] arr)  //參數數組
            {
                int sum = 0;
                foreach (int i in arr)
                {
                    sum += i;
                }
                return sum;
            }
        }
    
                ParamArray app = new ParamArray();
                int sum = app.AddElements(1, 2, 3, 4);
                Console.WriteLine("總和是:{0}",sum);
    6.arry 類
    arry類提供各種用於數組的屬性和方法
    屬性:
    • IsFixedSize  獲取一個值,該值指示數組是否帶有固定大小
    • IsReadOnly 獲取一個值,該值指示數組是否只讀
    • Length 獲取一個32位整數,該值標書所有維度中數組中的元素總數(LongLength 獲取64位)
    • Rank  獲取數組的秩(維度)
    常用方法:
    • Clear 根據元素類型,設置數組中某個範圍的元素為0,false,null
    • Copy(Array,Array,Int32)  從數組的第一個元素開始複製某個範圍的元素到另一個數組的第一個元素位置,長度由一個32位整數指定
    • Copy to(array int32) 從當親一維數組複製所有元素到一個指定一維數組的指定索引位置
    • GetLength   /   GetLongLength   獲取一個32/64位整數,表示指定維度的數組中的元素總數
    • GetLowerBound /GetUpperBound     獲取數組中指定維度的下界/上界
    • IndexOf(Array,Object)  搜索指定對象返回整個一維數組第一次出現的索引
    • Reverse(Array) 逆轉整個一維數組中元素的順序
    • GetValue(int32)/SetValue (Object,Int32)獲取/設置一維數組指定位置的元素設置值
    • ToString 返回一個表示當前對象的字符串
     
  • 相关阅读:
    线段树 by yyb
    【SYZOJ279】滑稽♂树(树套树)
    【BZOJ2806】Cheat(后缀自动机,二分答案,动态规划,单调队列)
    【BZOJ2733】永无乡(线段树,并查集)
    【BZOJ4991】我也不知道题目名字是什么(线段树)
    【BZOJ4999】This Problem Is Too Simple!(线段树)
    【BZOJ1858】序列操作(线段树)
    【BZOJ1835】基站选址(线段树)
    【BZOJ2962】序列操作(线段树)
    【BZOJ1558】等差数列(线段树)
  • 原文地址:https://www.cnblogs.com/ygtup/p/9359026.html
Copyright © 2011-2022 走看看