C# 集合

ArrayList 数组集合
 
4AddAddRangeRemoveRemoveAtRemoveRangeInsertInsertRange
    
这几个方法比较类似
Add
方法用于添加一个元素到当前列表的末尾
AddRange
方法用于添加一批元素到当前列表的末尾
Remove
方法用于删除一个元素,通过元素本身的引用来删除
RemoveAt
方法用于删除一个元素,通过索引值来删除
RemoveRange
用于删除一批元素,通过指定开始的索引和删除的数量来删除
Insert
用于添加一个元素到指定位置,列表后面的元素依次往后移动
InsertRange
用于从指定位置开始添加一批元素,列表后面的元素依次往后移动
using System;
using System.Collections;
namespace App{
    class MyClass{
        public static void Main(string[] args){
            ArrayList arrayList = new ArrayList();
            arrayList.Add(1);//添加一个元素到ArrayList
            Console.WriteLine(arrayList[0]);
        }
    }
}
 
 
 
12
12
 
 
 
 
 
1
using System;
2
using System.Collections;
3
namespace App{
4
    class MyClass{
5
        public static void Main(string[] args){
6
            ArrayList arrayList = new ArrayList();
7
            arrayList.Add(1);//添加一个元素到ArrayList
8
            Console.WriteLine(arrayList[0]);
9
        }
10
    }
11
}
12

 
 
///定义一个一维数组
            int[] intArray = { 1, 2, 3 };
            ///直接将一维数组转换为ArrayList;
            ArrayList array = new ArrayList(intArray);
            ///Add方法
            array.Add(5);
            ///Insert方法两个参数,索引位置和值
            array.Insert(3, 7);
            ////删除元素
            ///直接删除Array中的指定元素
            array.Remove(3);  
            ////删除索引位置为3的元素
            array.RemoveAt(3);
            ////删除一个范围,从Array中索引为1的元素开始删除三个元素
            array.RemoveRange(1, 3);
            ///Array的遍历
            foreach (int i in intArray)
            {
                Console.WriteLine(i);
            }
            ///查找元素
            ///查找元素为5,返回值为索引
            array.IndexOf(5);
            ////查找元素为5的,返回值为true/false
            array.Contains(5);

            Console.ReadLine();
 
 
 
x
27
 
 
 
 
 
1
///定义一个一维数组
2
            int[] intArray = { 1, 2, 3 };
3
            ///直接将一维数组转换为ArrayList;
4
            ArrayList array = new ArrayList(intArray);
5
            ///Add方法
6
            array.Add(5);
7
            ///Insert方法两个参数,索引位置和值
8
            array.Insert(3, 7);
9
            ////删除元素
10
            ///直接删除Array中的指定元素
11
            array.Remove(3);  
12
            ////删除索引位置为3的元素
13
            array.RemoveAt(3);
14
            ////删除一个范围,从Array中索引为1的元素开始删除三个元素
15
            array.RemoveRange(1, 3);
16
            ///Array的遍历
17
            foreach (int i in intArray)
18
            {
19
                Console.WriteLine(i);
20
            }
21
            ///查找元素
22
            ///查找元素为5,返回值为索引
23
            array.IndexOf(5);
24
            ////查找元素为5的,返回值为true/false
25
            array.Contains(5);
26

27
            Console.ReadLine();
 
 
using System;
using System.Collections;
namespace App{
    class MyClass{
        public static void Main(string[] args){
            ArrayList arrayList = new ArrayList();
            arrayList.Add(1);//添加一个元素到ArrayList
            arrayList.Add(1);//添加一个元素到ArrayList
            arrayList.Add(1);//添加一个元素到ArrayList
            arrayList.Insert(1,4);
            arrayList.Remove(1);//直接删除第一个值为1的元素
            arrayList.RemoveAt(1);//直接删除第一个值为1的元素
            foreach(int i in arrayList){
                Console.WriteLine(i);
            }
        }
    }
}
 
 
 
18
18
 
 
 
 
 
1
using System;
2
using System.Collections;
3
namespace App{
4
    class MyClass{
5
        public static void Main(string[] args){
6
            ArrayList arrayList = new ArrayList();
7
            arrayList.Add(1);//添加一个元素到ArrayList
8
            arrayList.Add(1);//添加一个元素到ArrayList
9
            arrayList.Add(1);//添加一个元素到ArrayList
10
            arrayList.Insert(1,4);
11
            arrayList.Remove(1);//直接删除第一个值为1的元素
12
            arrayList.RemoveAt(1);//直接删除第一个值为1的元素
13
            foreach(int i in arrayList){
14
                Console.WriteLine(i);
15
            }
16
        }
17
    }
18
}
 
 
数组转化为ArrayList集合
            string[] strArr = { "abc", "123", "asdf" };
            ArrayList array = new ArrayList(strArr);
            foreach (string item in array)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
 
 
 
7
7
 
 
 
 
 
1
            string[] strArr = { "abc", "123", "asdf" };
2
            ArrayList array = new ArrayList(strArr);
3
            foreach (string item in array)
4
            {
5
                Console.WriteLine(item);
6
            }
7
            Console.ReadKey();
 
 
Dictionary 字典<键对值,不允许有重复的值>
using System;
using System.Collections.Generic;
namespace App{
    class MyClass{
        public static void Main(string[] args){
            Dictionary<string,string> myDict = new Dictionary<string,string>();
            myDict.Add("add","abc");//添加键对值
            myDict.Add("1add","abc");
            myDict.Add("2add","abc");
            myDict.Add("3add","abc");
            myDict.Add("4add","abc");
            Console.WriteLine(myDict["add"]);
            foreach(string item in myDict.Keys){    //遍历
                Console.WriteLine(myDict[item]);//取值
            }
        }
    }
}
 
 
 
18
18
 
 
 
 
 
1
using System;
2
using System.Collections.Generic;
3
namespace App{
4
    class MyClass{
5
        public static void Main(string[] args){
6
            Dictionary<string,string> myDict = new Dictionary<string,string>();
7
            myDict.Add("add","abc");//添加键对值
8
            myDict.Add("1add","abc");
9
            myDict.Add("2add","abc");
10
            myDict.Add("3add","abc");
11
            myDict.Add("4add","abc");
12
            Console.WriteLine(myDict["add"]);
13
            foreach(string item in myDict.Keys){    //遍历
14
                Console.WriteLine(myDict[item]);//取值
15
            }
16
        }
17
    }
18
}
 
 
Hashtable 
添加元素
            Hashtable hs = new Hashtable();
            hs.Add("a","B");//天际
            Console.WriteLine(hs["a"]); //读取元素
 
 
 
3
3
 
 
 
 
 
1
            Hashtable hs = new Hashtable();
2
            hs.Add("a","B");//天际
3
            Console.WriteLine(hs["a"]); //读取元素
 
 
删除元素
            hs.Remove("b");//删除元素
 
 
 
1
1
 
 
 
 
 
1
            hs.Remove("b");//删除元素
 
 
遍历操作
            Hashtable hs = new Hashtable();
            hs.Add("a","B");
            hs.Add("b","B");
            foreach( string item in hs.Keys){//遍历键
                Console.WriteLine(item);
            }
 
 
 
6
6
 
 
 
 
 
1
            Hashtable hs = new Hashtable();
2
            hs.Add("a","B");
3
            hs.Add("b","B");
4
            foreach( string item in hs.Keys){//遍历键
5
                Console.WriteLine(item);
6
            }
 
 
            foreach(DictionaryEntry de in hs) //ht为一个Hashtable实例
            {
                Console.WriteLine(de.Key);  //de.Key对应于keyvalue键值对key
                Console.WriteLine(de.Value);  //de.Key对应于keyvalue键值对value
            }
 
 
 
5
5
 
 
 
 
 
1
            foreach(DictionaryEntry de in hs) //ht为一个Hashtable实例
2
            {
3
                Console.WriteLine(de.Key);  //de.Key对应于keyvalue键值对key
4
                Console.WriteLine(de.Value);  //de.Key对应于keyvalue键值对value
5
            }
 
 
查找操作
   Hashtable hs = new Hashtable();
            hs.Add("a","b");
            hs.Add("b","b");
            hs.Remove("b");
            if(hs.ContainsKey("a")){
                Console.WriteLine("存在!");//查找键
            }
 
 
 
7
7
 
 
 
 
 
1
   Hashtable hs = new Hashtable();
2
            hs.Add("a","b");
3
            hs.Add("b","b");
4
            hs.Remove("b");
5
            if(hs.ContainsKey("a")){
6
                Console.WriteLine("存在!");//查找键
7
            }
 
 
查找键
            Hashtable hs = new Hashtable();
            hs.Add("a","b");
            hs.Add("b","b");
            hs.Remove("b");
            if(hs.ContainsValue("b")){
                Console.WriteLine("存在!");//查找值
            }
 
 
 
7
 
 
 
 
 
1
            Hashtable hs = new Hashtable();
2
            hs.Add("a","b");
3
            hs.Add("b","b");
4
            hs.Remove("b");
5
            if(hs.ContainsValue("b")){
6
                Console.WriteLine("存在!");//查找值
7
            }
 
 
泛型LIst
            List<int> list = new List<int>();
            list.Add(10);
            Console.WriteLine(list);
            foreach(int item in list){
                Console.WriteLine(item);
            }
 
 
 
 
 
 
1
            List<int> list = new List<int>();
2
            list.Add(10);
3
            Console.WriteLine(list);
4
            foreach(int item in list){
5
                Console.WriteLine(item);
 
 
6
            }
 
 

<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">


类型安全问题

            var arrList=new ArrayList();
            arrList.Add("asdas");
            arrList.Add(1);
            foreach(var item in arrList){
                Console.WriteLine(item); //asdas 1
            }

 Stack 栈 

方法:Push, Pop,Contains;

属性: Count;

            Stack<int> s = new Stack<int>();
            s.Push(1);
            s.Push(2);
            Console.WriteLine(s);
            foreach(int item in s)
            {
                Console.WriteLine(item);
            }

 



posted @ 2019-04-13 12:12  liliyou  阅读(103)  评论(0编辑  收藏  举报