C# - ArrayList与Hashtable及其泛型版本

C#的集合类继承关系UML图:

ICollection - ICollection<T>

ICollection:所有非泛型集合的大小、枚举器和同步方法

public interface ICollection : IEnumerable {
	int Count { get; }
	bool IsSynchronized { get; } // 对ICollection的访问是否是同步的(线程安全)
	object SyncRoot { get; } // 获取可用于对ICollection同步访问的对象
	void CopyTo(Array array, int index);
}

ICollection<T>:泛型集合的属性方法

public interface ICollection<T> : IEnumerable<T>, IEnumerable {
	int Count { get; }
	bool IsReadOnly { get; }
	void Add(T item);
	bool Remove(T item);
	void Clear();
	bool Contains(T item);
    void CopyTo(T[] array, int arrayIndex);
}

ArrayList - List<T>

ArrayList: 使用大小可按需动态增加的数组实现IList接口

public class ArrayList : IList, ICollection, IEnumerable, ICloneable {
	public virtual int Capacity { get; set; }
	public virtual int Count { get; }
	public virtual bool IsReadOnly { get; }
	public virtual bool IsSynchronized { get; }
	public virtual object SyncRoot { get; }
	public virtual object this[int index] { get; set; } 

    public ArrayList();
	public ArrayList(int capacity);
	public ArrayList(ICollection c);
	public virtual IEnumerator GetEnumerator([int idx, int cnt]); // 枚举器
	public virtual object Clone(); // 创建ArrayList的浅表副本
	public virtual ArrayList GetRange(int idx, int cnt); // 子集
	public virtual void SetRange(int idx, ICollection c); // 设置ArrayList的值
	public static ArrayList ReadOnly(ArrayList list); // 返回只读的ArrayList包装
	public static IList ReadOnly(IList list); // 返回只读的IList包装
	public static ArrayList Synchronized(ArrayList list); // 返回线程同步的ArrayList包装
	public static IList Synchronized(IList list); // 返回线程同步的IList包装
	public static ArrayList Adapter(IList list);  // 返回IList的ArrayList包装
	public virtual int Add(object val);
	public virtual void AddRange(ICollection c);
	public virtual void Insert(int idx, object val);
	public virtual void InsertRange(int idx, ICollection c);
	public virtual void Remove(object obj);
	public virtual void RemoveAt(int idx);
	public virtual void RemoveRange(int idx, int cnt);
	public virtual void Clear();
	public virtual bool Contains(object item);
	public virtual int IndexOf(object val [, int startIdx, int cnt]);
	public virtual int LastIndexOf(object val [, int startIdx, int cnt]);
	public virtual void Reverse([int idx, int cnt]); // 反转
	public virtual void Sort([IComparer cmp]);       // 排序
	public virtual int BinarySearch(object val [, IComparer cmp]); // 二分查找
	public virtual object[] ToArray();
	public virtual void CopyTo(Array array [, int arrayIdx]);
}

其中,接口IList表示对象的非泛型集合,可按照索引单独访问  

public interface IList : ICollection, IEnumerable {
	bool IsReadOnly { get; }
	object this[int index] { get; set; }  // 获取或设置指定索引处的元素

    int Add(object value);
	void Insert(int index, object value);
	void Remove(object value);
	void RemoveAt(int index);
	void Clear();
	bool Contains(object value);
	int IndexOf(object value);
}

List<T>:  

其中,接口IList<T>表示可按照索引单独访问的一组对象的集合

public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable {
    T this[int index] { get; set; }
    void Insert(int index, T item);
    void RemoveAt(int index);
    int IndexOf(T item);
}  

Hashtable - Dictionary<TKey, TValue>

Hashtable:根据键的哈希代码进行组织的键/值对集合

public class Hashtable : IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback, ICloneable {
	public virtual int Count { get; }
	public virtual bool IsReadOnly { get; }
	public virtual bool IsSynchronized { get; }
	public virtual object SyncRoot { get; }
	public virtual ICollection Keys { get; }
	public virtual ICollection Values { get; }
	public virtual object this[object key] { get; set; }
	protected IComparer comparer { get; set; }  // 返回IComparer对象,用于比较

    public Hashtable();
	public Hashtable(int capacity);
	public Hashtable(IDictionary d);
	public virtual IDictionaryEnumerator GetEnumerator(); // 枚举器
	public virtual object Clone(); // 创建Hashtable的浅表副本
	public static Hashtable Synchronized(Hashtable table); // 返回线程同步的Hashtable包装
	public virtual void Add(object key, object value);
	public virtual void Remove(object key);
	public virtual void Clear();
	public virtual bool Contains(object key);
	public virtual bool ContainsKey(object key);
	public virtual bool ContainsValue(object value);
	protected virtual int GetHash(object key);
	protected virtual bool KeyEquals(object item, object key); // 与键比较
	public virtual void CopyTo(Array array, int arrayIndex);
}

其中,接口IDictionary表示键/值对的非泛型集合

public interface IDictionary : ICollection, IEnumerable {
	bool IsReadOnly { get; }
	object this[int index] { get; set; }
	ICollection Keys { get; }
	ICollection Values { get; }

    void Add(object key, object value);
	void Remove(object key);
	void Clear();
	bool Contains(object key);
	IDictionaryEnumerator GetEnumerator();
}

其中,IDictionaryEnumerator表示非泛型字典集的枚举器

public interface IDictionaryEnumerator : IEnumerator {
	DictionaryEntry Entry { get; }
    object Key { get; }
    object Value { get; }
}

其中,DictionaryEntry表示字典集的元素(键/值对)  

public struct DictionaryEntry {
	public DictionaryEntry(object key, object value);
	public object Key { get; set; }
	public object Value { get; set; }
}

Dictionary<TKey, TValue>:键值对的泛型集合  

public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback {
	public int Count { get; }
    public Dictionary<TKey, TValue>.KeyCollection Keys { get; }
    public Dictionary<TKey, TValue>.ValueCollection Values { get; }
    public TValue this[TKey key] { get; set; }

    public Dictionary();
	public Dictionary(int capacity);
	public Dictionary(IDictionary<TKey, TValue> dictionary);
	public Dictionary<TKey, TValue>.Enumerator GetEnumerator(); // 枚举器
	public void Add(TKey key, TValue value);
	public bool Remove(TKey key);
    public void Clear();
    public bool ContainsKey(TKey key);
    public bool ContainsValue(TValue value);
    public bool TryGetValue(TKey key, out TValue value);

    public struct Enumerator : IEnumerator<KeyValuePair<TKey, TValue>>, IDisposable, IDictionaryEnumerator, IEnumerator {}
    public sealed class KeyCollection : ICollection<TKey>, IEnumerable<TKey>, ICollection, IEnumerable {}
    public sealed class ValueCollection : ICollection<TValue>, IEnumerable<TValue>, ICollection, IEnumerable {}
}

其中,接口IDictionary<TKey, TValue>表示键/值对的泛型集合  

public interface IDictionary<TKey, TValue> : ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable {
	TValue this[TKey key] { get; set; }
	ICollection<TKey> Keys { get; }
    ICollection<TValue> Values { get; }

    void Add(TKey key, TValue value);
    bool Remove(TKey key);
    bool ContainsKey(TKey key);
    bool TryGetValue(TKey key, out TValue value);
} 


总结

  • 单线程程序中推荐使用Dictionary,多线程程序中推荐使用Hashtable;  
时间: 2024-08-25 06:13:02

C# - ArrayList与Hashtable及其泛型版本的相关文章

ArrayList、HashTable、List、Dictionary的演化及如何选择使用

在C#中,数组由于是固定长度的,所以常常不能满足我们开发的需求. 由于这种限制不方便,所以出现了ArrayList. ArrayList.List<T> ArrayList是可变长数组,你可以将任意多的数据Add到ArrayList里面.其内部维护的数组,当长度不足时,会自动扩容为原来的两倍. 但是ArrayList也有一个缺点,就是存入ArrayList里面的数据都是Object类型的,所以如果将值类型存入和取出的时候会发生装箱.拆箱操作(就是值类型与引用类型之间的转换),这个会影响程序性能

c#重点[集合类型]异常,数组,集合ArrayList,List&lt;&gt;,hashTable,hashtable泛型(Dictionary)

1.foreach[对一些数组或集合进行遍历] foreach(类型 变量名 in 集合对象){语句体} 1 //定义一个数组 2 int [] sNum1={19,33,27,57,45,43 }; 3 foreach(var i in sNum1) 4 { 5 Console.WriteLine(i); 6 } foreach for循环 1 int[] str1 = {19,33,27,57,45,43 }; 2 //for循环遍历 3 for (int i=0;i<str1.length

C#中集合ArrayList与Hashtable的使用

C#中集合ArrayList与Hashtable的使用 http://blog.csdn.net/linukey/article/details/42506819 ArrayList: 一. 注意事项: 1.可以指定ArrayList集合的初始大小 var list = new ArrayList(10);//容纳10个元素 若不指定大小,则默认大小为0,添加一个后为4,然后以倍数递增. 2.ArrayList是Array的复杂版本,ArrayList内部封装了一个Object类型的数组,从一般

各种数值集合的类型(Array、ArrayList、Hashtable、List&lt;T&gt;)

主要有:1.数组array    2.ArrayList    3.HashTable     4.泛型集合List<T> 1.Array 数组的特点是存储的类型统一,长度固定. 在我们一开始声明数组的时候就得确定他的长度,堆内存就要分配相应大小的内存空间.所以比较静态,不灵活. 2.解决Array的缺点,出现了ArrayList类型 ArrayList的长度是动态变化的,随着我们Add的对象的增加,他的数量(Count)逐一增加,他的容量(Capacity)成倍(2的指数倍)的增加.同时他存

ArrayList,Hashtable,List&lt;T&gt;,Dictionary&lt;K,V&gt;

1.ArrayList ArrayList list = new ArrayList(); //for遍历 for (int i = 0; i < list.Count; i++) { SE se=(SE)list[i]; Console.WriteLine(se.Name); } //foreach遍历 foreach(Object obj in list) { SE se=(SE)list[i]; Console.WriteLine(se.Name); } 2.Hashtable Hasht

[转]ArrayList和HashTable 使用

ArrayList HashTable System.Collections.ArrayList类是一个特殊的数组.通过添加和删除元素,就可以动态改变数组的长度. 一.优点 1.支持自动改变大小的功能 2.可以灵活的插入元素 3.可以灵活的删除元素 二.局限性 跟一般的数组比起来,速度上差些 三.添加元素 1.  public virtual int Add(object value); 将对象添加到 ArrayList 的结尾处 ArrayList aList = new ArrayList(

(转载)Java里新建数组及ArrayList java不允许泛型数组

java中新建数组: String[] s;//定义的时候不需要设置大小 s = new String[5];//为数组分配空间时就要设置大小 对于ArrayList, ArrayList<String> result = new ArrayList<String>(); //这时候就不需要设置大小了,result.add(string)添加一个元素,result.remove(string)删除一个元素,大小可以由result.size()得到 再来看看 java里允许Map[]

.Net学习笔记----2015-06-24(还是继承:ArrayList 和 Hashtable 集合)

protected 受保护的:可以在当前类的内部以及该类的子类中访问. ArrayList 集合:很多数据的一个集合 数组:长度不可变,类型单一 集合的好处:长度可以任意改变,类型随便放 ArrayList list = new ArrayList(); list.Add();添加单个元素 list.AddRange();添加集合 罗列ArrayList的各种用法 using System; using System.Collections.Generic; using System.Linq;

C# ArrayList、HashSet、HashTable、List、Dictionary的区别(转)

转自:https://blog.csdn.net/wildlifeking/article/details/58605587 在C#中,数组由于是固定长度的,所以常常不能满足我们开发的需求. 由于这种限制不方便,所以出现了ArrayList. ArrayList.List<T> ArrayList是可变长数组,你可以将任意多的数据Add到ArrayList里面.其内部维护的数组,当长度不足时,会自动扩容为原来的两倍. 但是ArrayList也有一个缺点,就是存入ArrayList里面的数据都是