2012年9月6日 星期四

[C#] 統計arraylist內各個項目出現次數


//註: arDif是存放資料的ArrayList, 我們要統計裡面存放的資料, 項目以及其出現次數

               Dictionary<double, int> result = new Dictionary<double, int>();
                foreach (int d in arDif)
                {
                    if (result.ContainsKey(d))
                    {
                        result[d] = result[d] + 1;
                    }
                    else
                    {
                        result.Add(d, 1);
                    }
                }  


//show資料

            foreach (KeyValuePair<double, int> item in result)
            {
                textBox9.Text += string.Format("{0} : {1}"+Environment.NewLine, item.Key, item.Value);
            }

[C#] ArrayList轉Array


static void Main(string[] args)
{
    {
        ArrayList Lists = new ArrayList();
        Lists.Add("a");
        Lists.Add("b");
        Lists.Add("c");
        string[] s = (string[])Lists.ToArray(typeof(string));
        foreach (string item in s) {
                    Console.WriteLine(item);
                }
    }
    {
        ArrayList Lists = new ArrayList();
        Lists.Add(1);
        Lists.Add(2);
        Lists.Add(3);
        int[] i = (int[])Lists.ToArray(Type.GetType("System.Int32"));
        foreach (int item in i) {
                    Console.WriteLine(item.ToString());
                }
    }
}