C#數組--(一維數組,二維數組的聲明,使用及遍歷)

數組:是具備相同數據類型的一組數據的集合。數組的每個的變量稱爲數組的元素,數組可以容納元素的數稱爲數組的長度。

一維數組:以線性方式存儲固定數目的數組元素,它只須要1個索引值便可標識任意1個數組元素

 1.格式:數組類型 [ ] 數組名稱;

     [  ] 指定數組的秩(維度),秩指定數組的大小。數組

2.數據類型能夠爲任意數據類型

3.數據訪問以前必須初始化

4.初始化方式有兩種:

(1)new關鍵詞;(2)字面值逗號相隔指定。spa

For example

 

      //建立並初始化一維數組
      int[] day = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

 

      int[] score = new int[4] { 95, 88, 80, 96 };

遍歷元素:數組中遍歷元素,即對數組中全部元素都按次序訪問且僅一次

 

 

1.for循環,foreach循環

2.數組索引從0開始,因此訪問數組的最後一我的元素應該爲n-1

3.遍歷數組時避免越界

4.一維數組遍歷時應該儘可能使用foreach語句,由於foreach會自動檢查數組的索引,使其不會出現越界值。

For examplecode

  //建立並初始化一維數組
  int[] day = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  for (int i = 0; i < 12; i++)                                //利用循環將信息輸出
  {    
  Console.WriteLine((i + 1) + "月有" + day[i] + "");       //輸出的信息
  }
  Console.ReadLine();
            int[] score = new int[4] { 95, 88, 80, 96 };
            Console.WriteLine("小明的各科成績以下:");
            for (int i = 0; i < 4; i++)//利用循環將信息輸出
            {
                Console.Write("  " + score[i] + "  ");//輸出的信息
            }
            Console.ReadLine();
           Console.WriteLine("狼人殺遊戲主要身份:");//提示信息
            //定義數組,存儲狼人殺遊戲主要角色
            string[] roles = { "狼人", "預言家", "村民", "女巫", "丘比特", "獵人", "守衛" };
            foreach(string role in roles)//遍歷數組
            {
                Console.Write(role + "  ");//輸出遍歷到的元素
            }
            Console.ReadLine();

二維數組:多維數組最簡單的形式,一個二維數組能夠被看作是一個帶有x行和y行列的表格。

1.格式:數據類型[   ,   ] <數組名稱>

2.初始化一樣有兩種:

  (1)new關鍵字(2)字面值初始化 blog

For example

            char[][] arr = new char[4][];// 建立一個4行的二維數組
            arr[0] = new char[] { '', '', '', '', '' };// 爲每一行賦值
            arr[1] = new char[] { '', '', '', '', '' };
            arr[2] = new char[] { '', '', '', '', '' };
            arr[3] = new char[] { '', '', '', '', '' };
            /* 橫版輸出 */
            Console.WriteLine("-----橫版-----");
            for (int i = 0; i < 4; i++)
            {                           // 循環4行
                for (int j = 0; j < 5; j++)
                {                        // 循環5列
                    Console.Write(arr[i][j]);                   // 輸出數組中的元素
                }
                if (i % 2 == 0)
                {
                    Console.WriteLine(",");                     // 若是是1、三句,輸出逗號
                }
                else
                {
                    Console.WriteLine("");                     // 若是是2、四句,輸出句號
                }
            }
            /* 豎版輸出 */
            Console.WriteLine("\n-----豎版-----");
            for (int j = 0; j < 5; j++)
            {                           // 列變行
                for (int i = 3; i >= 0; i--)
                {                  // 行變列,反序輸出
                    Console.Write(arr[i][j]);                   // 輸出數組中的元素
                }
                Console.WriteLine();                                //換行
            }
            Console.WriteLine("。,。,");                        //輸出最後的標點
            Console.ReadLine();
           Console.Title = "簡單客車售票系統";                     //設置控制檯標題
            string[,] zuo = new string[9, 4];                           //定義二維數組
            for (int i = 0; i < 9; i++)                                 //for循環開始
            {
                for (int j = 0; j < 4; j++)                             //for循環開始
                {
                    zuo[i, j] = "【有票】";                         //初始化二維數組
                }
            }
            string s = string.Empty;                                    //定義字符串變量
            while (true)                                            //開始售票
            {
                Console.Clear();                                    //清空控制檯信息
                Console.WriteLine("\n        簡單客車售票系統" + "\n"); //輸出字符串
                for (int i = 0; i < 9; i++)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        System.Console.Write(zuo[i, j]);                //輸出售票信息
                    }
                    Console.WriteLine();                            //輸出換行符
                }
                Console.Write("請輸入坐位行號和列號(如:0,2)輸入q鍵退出:");
                s = Console.ReadLine();                         //售票信息輸入
                if (s == "q") break;                                    //輸入字符串"q"退出系統
                string[] ss = s.Split(',');                             //拆分字符串
                int one = int.Parse(ss[0]);                         //獲得坐位行數
                int two = int.Parse(ss[1]);                         //獲得坐位列數
                zuo[one, two] = "【已售】";                         //標記售出票狀態
            }
相關文章
相關標籤/搜索