NameSpace System.Collections

ArrayList可以存放多樣的型態,陣列內元素可以是任何Object可接受的資料型別

且永遠都是"一維陣列",常用在存放混合各種資料型別以及存放的個數不確定之情形

(ps.陣列須明確知道存放資料個數,以及資料型別都要相同)


宣告:

System.Collections.ArrayList Msg = new System.Collections.ArrayList();


使用:

Msg.Insert(0,"M0"); //新增

Msg.Insert(1,"M1");//新增

Msg.Insert(2,"M2");//新增

int count = Msg.Count;  //取得ArrayList個數

var item = Msg[1]; //存取指定index

Msg.Remove("M0");//移除符合元素


//移除特定索引.移除後該索引後的索引,向前遞補

//index: 0 , 1 , 2 , 3 , 4

Msg.RemoveAt(1);

//index: 0 , 1 , 2, 3  => 1 , 2, 3 是未移除前的2,3,4


另一種存放範例

    先宣告依資料結構

        private struct eMsg

        {

            public string ID;

            public string Title;

            public string URL;

        }

  //宣告Arraylist

  System.Collections.ArrayList Msg = new System.Collections.ArrayList();

            eMsg _msg;

           //設定資料結構內參數

            _msg.ID = "33187620";

            _msg.Title = "[高雄縣]辦理「豬事順利,稅稅平安」";

            _msg.URL = "http://www.ntas.gov.tw/county/";

            Msg .Add(_msg); //將eMsg資料結構加入ArrayList中


可以使用foreach 來抓取內容來操作 例如

             foreach (eMsg tmpmsg in Msg )

                {

                   if (tmpmsg .Title == "[高雄縣]辦理「豬事順利,稅稅平安」") //找到相符合元件

                    {

                        webBrowser1.Navigate(tmpmsg .URL); 曲的該元件URL參數並處理

                        return;

                    }

                }

arrow
arrow
    全站熱搜

    baechang 發表在 痞客邦 留言(0) 人氣()