[技術類 - C# 學習誌 - C#面試考題 ] 陣列類別常用成員、字串類別常用成員

行動版 for , 瀏覽人次: 1512  , SSL Connection SSL
  • • 常用屬性
    – Rank–陣列的維度大小
    – Length–陣列元素個數
    • 常用方法
    – GetLength–回傳某一維度的長度
    – Clone–複製陣列內容至一新陣列實體  (不會複製參考所參考的物件)
    • 靜態方法
    – Sort–排序陣列元素
    – IndexOf–回傳第一個符合參數值的索引位置
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[,] x = new int[2, 3];
                Console.WriteLine(“Length︰" + x.Length);
                Console.WriteLine(“Rank︰" + x.Rank);
                Console.WriteLine(“GetLength(0)︰" +
                x.GetLength(0));
                Console.WriteLine(“GetLength(1)︰" +
                x.GetLength(1));
                car[] myCar = new car[5];
                for (int i = 0; i < myCar.Length; i++)
                {
                    myCar[i] = new car();
                    myCar[i].id = i;
                }
                Console.WriteLine();
                car[] b = (car[])myCar.Clone();  //Clone過來是object,必須做型別轉換!
                //– 建立一個型別為int 的一維陣列,內含10 個元素,元素值利用亂數來決(1~10)
                //– 請使用者輸入一個數字(1~10)
                //– 列印出陣列當中是否包含該數字,若有則列印該數字在陣列當中的位置
                //– 最後將陣列中所有元素列印出來
                int[] my_input = new int[10];
                Random rnd = new Random();
                for (int i = 0; i < my_input.Length; i++)
                {
                    int a = rnd.Next(1, 11);
                    my_input[i] = a;
                }
                Console.WriteLine(“請輸入一個數字(1~10)");
                int user_input = int.Parse(Console.ReadLine());
                if (Array.IndexOf(my_input, user_input) < 0)  //IndexOf:沒有則回傳-1
                {
                    Console.WriteLine(“陣列中沒有您剛剛輸入的數字!");
                }
                else  //IndexOf:有則回傳位置
                {
                    Console.WriteLine(“該數字在陣列當中的位置:" + Array.IndexOf(my_input, user_input));
                }
                for (int i = 0; i < my_input.Length; i++)
                {
                    Console.WriteLine(my_input[i]);
                }
                Console.ReadLine();
            }
        }
        class car
        {
            public int id;
        }
    }
    ——————————————————————————————————————–
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                string str = “Alice is a good student!!";
                Console.WriteLine(str);
                Console.WriteLine(“ToUpper︰" + str.ToUpper());
                Console.WriteLine(“ToLower︰" + str.ToLower());
                Console.WriteLine(“Insert︰" + str.Insert(6, “Wang “));
                string[] strArray = str.Split(‘ ‘);
                for(int i=0; i<strArray.Length; i++)
                {
                    Console.WriteLine(strArray[i]);
                }
                //– 請使用者輸入多個整數值(以逗號區隔)
                //– 列印輸入的整數值總和
                Console.WriteLine(“請輸入多個整數值(以逗號區隔)");
                string[] user_Array = Console.ReadLine().Split(‘,’);
                int[] all_num = new int[user_Array.Length];
                for(int i=0; i<user_Array.Length; i++)
                {
                    all_num[i]= int.Parse(user_Array[i]);
                }
                int all_sum = all_num.Sum();
                Console.WriteLine(“輸入的整數值總和:" + all_sum);
                Console.ReadLine();
            }
        }
    }
    ——————————————————————————————————————–
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                //– 請使用者輸入一年份
                //– 列印該年是否為閏年
                Console.WriteLine(“請輸入一個年份");
                int user_input = int.Parse(Console.ReadLine());
                if (DateTime.IsLeapYear(user_input)==true)
                {
                    Console.WriteLine(“是閏年");
                }else{
                    Console.WriteLine(“不是閏年");
                }
                Console.ReadLine();
            }
        }
    }
回 文章列表頁