集合ArrayList Hashtable,泛型集合


ArrayList

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace _01ArrayList演示
{
    class Program
    {
        static void Main(string[] args)
        {
            #region ArrayList的常用方法1


            //ArrayList arrayList = new ArrayList();

            //Console.WriteLine("集合中元素的个数为:{0}", arrayList.Count);
            //Console.WriteLine("集合现在的容量是:{0}", arrayList.Capacity);
            向集合中增加元素
            //arrayList.Add(10);

            //Console.WriteLine("集合中元素的个数为:{0}", arrayList.Count);
            //Console.WriteLine("集合现在的容量是:{0}", arrayList.Capacity);
            //arrayList.Add("10");

            //Console.WriteLine("集合中元素的个数为:{0}", arrayList.Count);
            //Console.WriteLine("集合现在的容量是:{0}", arrayList.Capacity);
            //arrayList.Add("黄林");
            //Console.WriteLine("集合中元素的个数为:{0}", arrayList.Count);
            //Console.WriteLine("集合现在的容量是:{0}", arrayList.Capacity);
            //Person xzl = new Person();
            //xzl.Name = "许正龙";
            //arrayList.Add(xzl);
            //Console.WriteLine("集合中元素的个数为:{0}", arrayList.Count);
            //Console.WriteLine("集合现在的容量是:{0}", arrayList.Capacity);
            //arrayList.Add(true);
            //Console.WriteLine("集合中元素的个数为:{0}", arrayList.Count);
            //Console.WriteLine("集合现在的容量是:{0}", arrayList.Capacity);
            //arrayList.AddRange(new int[] { 1, 3, 5, 7, 8, 10, 12 });
            //Console.WriteLine("集合中元素的个数为:{0}", arrayList.Count);
            //Console.WriteLine("集合现在的容量是:{0}", arrayList.Capacity);

            通过下标获取集合中的元素
            //Console.WriteLine(arrayList[0]);
            //Console.WriteLine(arrayList[5]);
            //for (int i = 0; i < arrayList.Count; i++)
            //{
            //    Console.WriteLine(arrayList[i]);
            //}

            向指定的位置插入一个元素
            //arrayList.Insert(0, "石国庆");
            //Console.WriteLine("=========================================");

            //arrayList.InsertRange(5, new string[] { "a", "b", "b" });
            //for (int i = 0; i < arrayList.Count; i++)
            //{
            //    Console.WriteLine(arrayList[i]);
            //}


            //ArrayList arrayList = new ArrayList();
            //arrayList.AddRange(new int[] { 1, 3, 4, 5, 6, 7, 8, 9 });

            //Console.WriteLine("============删除结果是:=============================");
            //删除元素,这样不能将ArrayList中的元素都删除掉。
            //arrayList.RemoveAt(
            for (int i = 0; i < arrayList.Count; i++)
            {
                arrayList.RemoveAt(i);
            }
            //arrayList.Clear();
            //Console.WriteLine(arrayList.Count);//???????

            //ArrayList arrayList = new ArrayList();
            //Person p1 = new Person();
            //p1.Name = "马毅";
            //p1.Age = 18;
            //p1.Email = "my@yahoo.com";

            //arrayList.Add(p1);
            //arrayList.Add(99);
            //arrayList.Add("黄林");

            //Person p2 = new Person();
            //p2.Name = "马毅";
            //p2.Age = 18;
            //p2.Email = "my@yahoo.com";

            //arrayList.Add(p2);

            //arrayList.Remove(99);

            //Person p3 = new Person();
            //p3.Name = "马毅";
            //p3.Age = 18;
            //p3.Email = "my@yahoo.com";

            //p3 = p1;

            //arrayList.Remove(p3);
            //string name = new string(new char[] { '黄', '林' });
            //arrayList.Remove(name);

            //Console.WriteLine(arrayList.Count);
            //Console.ReadKey();

            #endregion






            #region MyRegion
            //ArrayList arrayList = new ArrayList();

            //Person p1 = new Person();
            //p1.Name = "马毅";
            //p1.Age = 18;
            //p1.Email = "my@yahoo.com";

            //arrayList.Add(p1);
            //arrayList.Add(99);
            //arrayList.Add("黄林");

            //Person p2 = new Person();
            //p2.Name = "马毅";
            //p2.Age = 18;
            //p2.Email = "my@yahoo.com";

            //arrayList.Add(p2);
            //string s = new string(new char[] { '黄', '林' });
            //Console.WriteLine(arrayList.Contains(s));


            把集合转换为数组的一般方法:
            arrayList.ToArray(); 
            #endregion


            #region ArrayList的Sort方法
            //ArrayList arr = new ArrayList(new int[] { 1, 32, 87, 48, 2, 5 });

            默认sort()方法是升序排序。
            //arr.Sort();
            没有降序排序的方法,但是有一个反转的方法。
            //arr.Reverse();
            //for (int i = 0; i < arr.Count; i++)
            //{
            //    Console.WriteLine(arr[i]);
            //}
            //Console.ReadKey();

            =========================================================
            //ArrayList arrlist = new ArrayList(new string[] { "hl", "xzl", "yzk", "fxh", "sr", "yhb", "abm","xyz","xay" });
            //arrlist.Sort();
            //for (int i = 0; i < arrlist.Count; i++)
            //{
            //    Console.WriteLine(arrlist[i]);
            //}
            //Console.ReadKey();

            //===========================================================================
            ArrayList arr = new ArrayList();
            Person p1 = new Person();
            p1.Name = "chenjinlin";
            p1.Age = 19;
            p1.Email = "cjl@yahoo.com";

            Person p2 = new Person();
            p2.Name = "zhengdd";
            p2.Age = 18;
            p2.Email = "zdd@yahoo.com";

            Person p3 = new Person();
            p3.Name = "zhubq";
            p3.Age = 17;
            p3.Email = "zbq@yahoo.com";



            Person p4 = new Person();
            p4.Name = "pdh";
            p4.Age = 16;
            p4.Email = "pdh@yahoo.com";
            arr.Add(p1);
            arr.Add(p2);
            arr.Add(p3);
            arr.Add(p4);
            Console.WriteLine(arr.Count);
            arr.Sort();
            for (int i = 0; i < arr.Count; i++)
            {
                Console.WriteLine(((Person)arr[i]).Name);
            }
            Console.ReadKey();


            #endregion

        }
    }
    public class Person : IComparable
    {
        public string Name
        {
            get;
            set;
        }
        public int Age
        {
            get;
            set;
        }
        public string Email
        {
            get;
            set;
        }
        public int CompareTo(object obj)
        {
            Person p = obj as Person;
            if (p != null)
            {
                //return p.Age - this.Age;
                return p.Name.Length - this.Name.Length;//this.Name.Length - p.Name.Length;
            }
            return 0;
        }
    }
}

Hashtable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace _03Hashtable集合演示
{
    class Program
    {
        static void Main(string[] args)
        {

            #region MyRegion


            //Hashtable hash = new Hashtable();

            //hash.Add("hl", "黄林");

            //hash.Add("xzl", new Person() { Name = "许正龙" });

            键值对集合的“键”一定不能重复。(唯一)
            //hash.Add("huanglei", "huanglei");

            判断一个集合中是否存在某个键
            //if (!hash.ContainsKey("hl"))
            //{

            //}
            hash.ContainsValue();//判断键值对中是否存在某个值。
            hash.Contains(

            通过键获取值
            //Console.WriteLine(hash["hl"].ToString());

            //Person pp = hash["xzl"] as Person;
            //Console.WriteLine(pp.Name);

            遍历Hashtable
            1.遍历键
            //foreach (object item in hash.Keys)
            //{
            //    Console.WriteLine("键:{0}  →   值:{1}", item, hash[item]);
            //}
            //Console.WriteLine("=========直接遍历值===========");
            //foreach (object item in hash.Values)
            //{
            //    Console.WriteLine("值:{0}", item);
            //}
            //Console.WriteLine("======直接遍历“键值对”===========");
            //foreach (DictionaryEntry kv in hash)
            //{
            //    Console.WriteLine("键:{0}    值:{1}", kv.Key, kv.Value);
            //}


            //Console.ReadKey();

            #endregion

            #region MyRegion

            int[] arr = { 15, 175, 264, 359, 401, 598 };

            int number = 359;
            int index = number / 100;
            if (index >= 0 && index < arr.Length)
            {
                Console.WriteLine("存在这个值,索引位置是:{0},对应的值是:{1}", index, arr[index]);
            }
            Console.ReadKey();



            #endregion





        }
    }
    public class Person
    {
        public string Name
        {
            get;
            set;
        }
        public int Age
        {
            get;
            set;
        }
        public string Email
        {
            get;
            set;
        }
    }


}

List集合与Dictionary集合

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _05List集合与Dictionary集合
{
    class Program
    {
        static void Main(string[] args)
        {
            //List<string> list = new List<string>();
            //List<int> list1 = new List<int>();
            //Console.WriteLine(list.ToString());

            //List<int> list = new List<int>();
            //list.Add(100);
            //list.Add(999);
            //list.Add(889);

            list.ToArray();

            //Hashtable的泛型版本
            //Generic

            Dictionary<string, int> dict = new Dictionary<string, int>();

            dict.Add("huanglin", 100);
            dict.Add("xuzhenglong", 98);

            //Console.WriteLine(dict["huanglin"]);

            //遍历“键”
            foreach (string item in dict.Keys)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("=====================================");
            //遍历“值”
            foreach (int item in dict.Values)
            {
                Console.WriteLine(item);
            }
            //直接遍历“键值对”
            foreach (KeyValuePair<string, int> item in dict)
            {
                Console.WriteLine("键:{0}  →   值:{1}", item.Key, item.Value);
            }



            Console.ReadKey();







            Console.ReadKey();
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值