c# array_arraylist_continue_break_foreach

wisdomone1發表於2012-03-17

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;//arraylist派生於array類

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
             //學習arraylist
       
        
             string[] name1 = new string[] { "a", "b", "c" };
            //array類位於System名稱空間中
            Array.Sort(name1);
             //輸出陣列的元素
             foreach (string subname in name1)
             {
                 Console.WriteLine(subname);
             }
           
            //在foreach中使用break,直接退出foreach語句 這樣最終只顯示a
             foreach (string subname in name1)
             {
                 if (subname=="b")
                 {
                     break;
                 }
                 Console.WriteLine(subname);
             }

            //在foreach中使用continue,直接進行下一次迴圈
            foreach (string subname in name1)
            {
                if (subname == "a")
                {
                    continue;
                }
                Console.WriteLine(subname);
            }

             string inputstr=Console.ReadLine();
             if (inputstr == "abc")
             {
                 Console.WriteLine("輸入字元是abc");

             }
             Console.WriteLine(inputstr);
             Console.ReadKey();

 

            //學習arraylist類
             ArrayList al1 = new ArrayList(4);
             al1.Add("arraylist的元素1");
             al1.Add(1);
             al1.Add(true);
             Console.WriteLine(al1.Count);
             Console.WriteLine(al1.Capacity);
             al1.Insert(3, "翟勳楊");
             al1.Remove(true);
            
             al1.Clear();
             Console.WriteLine("執行clear後"+al1.Count);
             Console.ReadKey();
         }
     
    }
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9240380/viewspace-718831/,如需轉載,請註明出處,否則將追究法律責任。