C# 数组及集合

一 数组

声明:
int [] a;
int [,]b;
分配空间:
a=new int[5];
b=new int[4,5];

二 集合类

System.Collections命名空间;
数组列表ArrayList
相当于动态数组,实现IList;
哈希表 Hashtable
相当于键/值的集合,实现IDictionary;
用[] 进行访问,表示获取,增加、删除、修改;
提示:
用于查询时,比线性搜索的效率更高,可用于程序的优化;
栈和队列 Stack Queue;

三 使用foreach 访问数组及集合

foreach(类型 变量 in xxxx)
其中xxxx必须是实现了实现IEnumerable接口或含有GetEnumerator方法的类型;
这个方法的原型是IEnumerator GetEnumerator();
返回的是一个接口IEnumerator
Current属性
MoveNext及Reset方法;

四 泛型集合

泛型具有更好的类型检查及性能
System.Collections.Generic名称空间
列表 List/LinkedList SortedList
字典 Dictionary SortedDictionary;
集 HashSet SortedSet
栈,队列 Stack,Queue;

示例

1 ListTest

List 的Add方法,Count属性,[i]索引器;
foreach的遍历;

2 HashtableTest

HashTable的[] 索引,可以表示获取/加入/修改/删除(置为null)
Foreach遍历;

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

namespace 使用List
{
    public class ListTest
    {
        public static void Main()
        {
            List<string> fruits = new List<string>();
            fruits.Add("Apple");
            fruits.Add("Banana");
            fruits.Add("Carrot");

            Console.WriteLine("Count:{0}", fruits.Count);
            PrintValues1(fruits);
            PrintValues2(fruits);
            PrintValues3(fruits);

            Console.ReadKey();
        }

        static void PrintValues1(IList<string>myList)
        {
            for (int i = 0; i < myList.Count; i++)
                Console.Write("{0}\n", myList[i]);
        }
        
        static void PrintValues2(IList<string>myList)
        {
            foreach (string item in myList)
                Console.Write("{0}\n", item);
        }

        static void PrintValues3(IEnumerable<string>myList)
        {
            IEnumerator<string> myEnumerator = myList.GetEnumerator();
            while (myEnumerator.MoveNext())
                Console.Write("{0}\n", myEnumerator.Current);
            Console.WriteLine();
        }
    }
}

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

namespace 使用Hashtable
{
    internal class Class1
    {
        public static void Main()
        {
            Hashtable myHT = new Hashtable();
            myHT.Add("Ton V.Bergyk", "023-010-66757");
            myHT.Add("Tom sony", "086-010-27654");
            myHT.Add("Mr.John", "071-222-33445");
            myHT["mr.John"] = "071-222-33445";//加入或修改

            PrintKeysAndValues(myHT);
            PrintByKeys(myHT);

            Console.ReadKey();
        }

        public static void PrintKeysAndValues(Hashtable myList)
        {
            IDictionaryEnumerator myEnumerator = myList.GetEnumerator();
            while (myEnumerator.MoveNext())
                Console.WriteLine("\\t{0}:t{1}",
                    myEnumerator.Key, myEnumerator.Value);
            Console.WriteLine();
        }

        public static void PrintByKeys(Hashtable myList)
        {
            IEnumerator ie = myList.Keys.GetEnumerator();

            while (ie.MoveNext())
            {
                object key = ie.Current;
                object value = myList[key];
                Console.WriteLine("\t{0}:\t{1}", key, value);
            }
            Console.WriteLine();

            foreach (string key in myList.Keys)
            {
                Console.WriteLine("\t{0}:\t{1}", key, myList[key]);
            }
        }
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值