散分200 讨论泛型类和动态加载树形!~
LIST<>
和
Dictionary<TKey, TValue>
哈西表?
LIST<>我会用 希望前辈们 给点Dictionary<TKey, TValue>
和哈西表的例子
来这有分!~
[解决办法]
UP本人还是刚学习ASP.NET 所以还不能回答你
但是我还是要顶!
[解决办法]
泛型是 C#2.0 语言和公共语言运行库 (CLR) 中的一个新功能。泛型将类型参数的概念引入 .NET Framework,类型参数使得设计如下类和方法成为可能:这些类和方法将一个或多个类型的指定推迟到客户端代码声明并实例化该类或方法的时候。例如,通过使用泛型类型参数 T,可以编写其他客户端代码能够使用的单个类,而不致引入运行时强制转换或装箱操作.
使用泛型类型可以最大限度地重用代码、保护类型的安全以及提高性能。
泛型最常见的用途是创建集合类。
.NET Framework 类库在 System.Collections.Generic 命名空间中包含几个新的泛型集合类。应尽可能地使用这些类来代替普通的类,如 System.Collections 命名空间中的 ArrayList,HashTable等。
下面我们就来说下,几个泛型集合类的用法:
一.Dictionary
此类在 .NET Framework 2.0 版中是新增的。表示键和值的集合。命名空间:System.Collections.Generic,程序集:mscorlib(在 mscorlib.dll 中)
class TestGenericList
{
static void Main()
{
//声明对象,参数表示,键是int类型,值是string类型
Dictionary<int, string> fruit = new Dictionary<int, string>();
try{
//加入重复键会引发异常
fruit.Add(1, "苹果");
fruit.Add(2, "桔子");
fruit.Add(3, "香蕉");
fruit.Add(4, "菠萝");
//参数错误将引发异常,如下所示
//fruit.Add("5", "aa");
}
catch (ArgumentException)
{
Console.WriteLine("添加错误!!!");
}
//因为引入了泛型,所以键取出后不需要进行Object到int的转换,值的集合也一样
foreach (int i in fruit.Keys)
{
Console.WriteLine("键是:{0} 值是:{1}",i,fruit);
}
//删除指定键,值
fruit.Remove(1);
//判断是否包含指定键
if (fruit.ContainsKey(1))
{
Console.WriteLine("包含此键");
}
//清除集合中所有对象
fruit.Clear();
}
}
Dictionary遍历输出的顺序,就是加入的顺序,这点与Hashtable不同,其它方法如:ContainsKey ,ContainsValue ,Remove 等,使用方法基本一致。
二、List类
注意:此类在 .NET Framework 2.0 版中是新增的。表示可通过索引访问的对象的强类型列表。提供用于对列表进行搜索、排序和操作的方法。命名空间:System.Collections.Generic,程序集:mscorlib(在 mscorlib.dll 中),List 类是 ArrayList 类的泛型等效类。
//声明一个泛型类
class TestGenericList
{
static void Main()
{
//声明一个List对象,只加入string参数
List<string> names = new List<string>();
names.Add("乔峰");
names.Add("欧阳峰");
names.Add("马蜂");
//遍历List
foreach (string name in names)
{
Console.WriteLine(name);
}
//向List中插入元素
names.Insert(2, "张三峰");
//移除指定元素
names.Remove("马蜂");
}
}
在决定使用 List 还是使用 ArrayList 类(两者具有类似的功能)时,记住 List 类在大多数情况下执行得更好并且是类型安全的。如果对 List 类的类型 T 使用引用类型,则两个类的行为是完全相同的。但是,如果对类型 T 使用值类型,则需要考虑实现和装箱问题。
如果对类型 T 使用值类型,则编译器将特别针对该值类型生成 List 类的实现。这意味着不必对 List 对象的列表元素进行装箱就可以使用该元素,并且在创建大约 500 个列表元素之后,不对列表元素装箱所节省的内存将大于生成该类实现所使用的内存。
其实我们也可以自己定义一个泛型类,如下所示:
//声明一个泛型类
public class ItemList<T>
{
void Add(T item) { }
}
class TestGenericList
{
private class ExampleClass { }
static void Main()
{
// 声明一个对象,只能加入int型
ItemList<int> list1 = new ItemList<int>();
//声明一个对象,只能加入Student类型,Student类为自定义类
ItemList<Student> list2 = new ItemList<Student>();
}
}
泛型的用法还有很多种,如泛型方法,泛型委托,泛型接口等。
------解决方案--------------------
up~
[解决办法]
Up
[解决办法]
帮顶 泛型用过 动态树还没用
[解决办法]
up !!!!!!!
[解决办法]
up
[解决办法]
up
[解决办法]
学习
[解决办法]
帮顶一下
[解决办法]
讲个简单的例子:
//主表类:ClassA,子表类: ClassB
//定义数据字典
Dictionary<ClassA, List<ClassB>> dictionaryDemo= new Dictionary<ClassA, List<ClassB>>();
//主表类存在一个对象 objClassA和一个对象列表 List<ClassA> listClassA,子表类存在一个对象列表 List<ClassB> listClassB
//数据字典赋值
dictionaryDemo[objClassA] = listClassB;
//为子列表指定值
listClassB = dictionaryDemo[objClassA];
//主表列表赋值
listClassA = new List<ClassA>(dictionaryDemo.Keys);
[解决办法]
dictionary不是哈希表,哈希表是hashtable。下面是这几个的区别和用法:
Hashtable, ArrayList, List, Dictionary
Hashtable用法
在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似key/value的键值对,其中key通常可用来快速查找,同时key是区分大小写;value用于存储对应于key的值。Hashtable中key/value键值对均为object类型,所以Hashtable可以支持任何类型的key/value键值对.。
在哈希表中添加一个key/value键值对:HashtableObject.Add(key,value);
在哈希表中去除某个key/value键值对:HashtableObject.Remove(key);
从哈希表中移除所有元素: HashtableObject.Clear();
判断哈希表是否包含特定键key: HashtableObject.Contains(key);
Hashtable ht = new Hashtable();
ht.Add("a", 123);
ht.Add("b", 456);
// 遍历哈希表需要用到DictionaryEntry Object
foreach (DictionaryEntry de in ht)
{
MessageBox.Show(de.Key.ToString() + " " + de.Value.ToString());
}
//对哈希表进行排序
ArrayList akeys = new ArrayList(ht.Keys); //别忘了导入System.Collections
akeys.Sort(); //按字母顺序进行排序
foreach (string skey in akeys)
{
MessageBox.Show(skey + ":");
MessageBox.Show(ht[skey].ToString());//排序后输出
}
ArrayList用法
private static void AddToList(ArrayList list, string p)
{
if (list.Contains(p) == false)
list.Add(p);
}
private void button1_Click(object sender, EventArgs e)
{
ArrayList list = new ArrayList();
AddToList(list, "Table1");
AddToList(list, "Table4");
AddToList(list, "Table1");
AddToList(list, "Table3");
AddToList(list, "Table2");
AddToList(list, "Table2");
foreach (string s in list)
{
MessageBox.Show(s);
}
}
List
List<string> listStr = new List<string>();
listStr.Add("123");
listStr.Add("456");
listStr.Add("789");
MessageBox.Show(listStr[2]);//”789”
Dictionary
泛型的优点(C# 编程指南)
C# 中典型的范型结构除了熟悉的 IList , HashTable之外还有一个并不常见的Dictionary集合。
相比较而言,Dictionary的性能是最好的,也属于轻便型的集合。效率要大于HashTable,其主要原因是Dictionary支持强类型声明的。
在公共语言运行库和 C# 语言的早期版本中,通用化是通过在类型与通用基类型 Object 之间进行强制转换来实现的,泛型提供了针对这种限制的解决方案。通过创建泛型类,您可以创建一个在编译时类型安全的集合。
添加到 ArrayList 中的任何引用或值类型都将隐式地向上强制转换为 Object。如果项是值类型,则必须在将其添加到列表中时进行装箱操作,在检索时进行取消装箱操作。强制转换以及装箱和取消装箱操作都会降低性能;在必须对大型集合进行循环访问的情况下,装箱和取消装箱的影响非常明显。
对于客户端代码,与 ArrayList 相比,使用 List<T> 时添加的唯一语法是声明和实例化中的类型参数。虽然这稍微增加了些编码的复杂性,但好处是您可以创建一个比 ArrayList 更安全并且速度更快的列表,特别适用于列表项是值类型的情况。
Dictionary 泛型类提供了从一组键到一组值的映射。字典中的每个添加项都由一个值及其相关联的键组成。通过键来检索值的速度是非常快的,接近于 O(1),这是因为 Dictionary 类是作为一个哈希表来实现的。
1、
Dictionary<int, string> fruit = new Dictionary<int, string>();
//加入重复键会引发异常
fruit.Add(1, "苹果");
fruit.Add(2, "桔子");
fruit.Add(3, "香蕉");
fruit.Add(4, "菠萝");
//因为引入了泛型,所以键取出后不需要进行Object到int的转换,值的集合也一样
foreach (int i in fruit.Keys)
{
MessageBox.Show("键是:"+i.ToString()+ "值是:"+ fruit[i]);
}
[解决办法]
学习
[解决办法]
用递归的方式动太添加就行了
[解决办法]
学习
[解决办法]
类型参数TKey 字典中的键的类型。TValue 字典中的值的类型。Dictionary<(Of <(TKey, TValue>)>) 泛型类提供了从一组键到一组值的映射。字典中的每个添加项都由一个值及其相关联的键组成。通过键来检索值的速度是非常快的,接近于 O(1),这是因为 Dictionary<(Of <(TKey, TValue>)>) 类是作为一个哈希表来实现的。注意:检索速度取决于为 TKey 指定的类型的哈希算法的质量。只要对象用作 Dictionary<(Of <(TKey, TValue>)>) 中的键,它就不能以任何影响其哈希值的方式更改。使用字典的相等比较器比较时,Dictionary<(Of <(TKey, TValue>)>) 中的任何键都必须是唯一的。键不能为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing),但是如果值类型 TValue 为引用类型,该值则可以为空。Dictionary<(Of <(TKey, TValue>)>) 需要一个相等实现来确定键是否相等。可以使用一个接受 comparer 参数的构造函数来指定 IEqualityComparer<(Of <(T>)>) 泛型接口的实现;如果不指定实现,则使用默认的泛型相等比较器 EqualityComparer<(Of <(T>)>)..::.Default。如果类型 TKey 实现 System..::.IEquatable<(Of <(T>)>) 泛型接口,则默认相等比较器会使用该实现。注意:例如,您可以使用 StringComparer 类提供的不区分大小写的字符串比较器来创建带不区分大小写的字符串键的字典。Dictionary<(Of <(TKey, TValue>)>) 的容量是 Dictionary<(Of <(TKey, TValue>)>) 可以包含的元素数。当向 Dictionary<(Of <(TKey, TValue>)>) 中添加元素时,将通过重新分配内部数组来根据需要自动增大容量。对于枚举而言,字典中的每一项都被视为一个表示值及其键的 KeyValuePair<(Of <(TKey, TValue>)>) 结构进行处理。项返回的顺序未定义。C# 语言的 foreach 语句(在 C++ 中为 for each,在 Visual Basic 中为 For Each)需要集合中每个元素的类型。由于 Dictionary<(Of <(TKey, TValue>)>) 是键和值的集合,因此元素类型并非键类型或值类型。相反,元素类型是键类型和值类型的 KeyValuePair<(Of <(TKey, TValue>)>)。例如:foreach (KeyValuePair<int, string> kvp in myDictionary) {...}下面的代码示例创建一个空的带有字符串键的字符串 Dictionary<(Of <(TKey, TValue>)>),并使用 Add 方法添加一些元素。该示例演示在尝试添加重复的键时 Add 方法引发 ArgumentException。该示例使用 Item 属性(在 C# 中为 索引器)来检索值,演示当请求的键不存在时将引发 KeyNotFoundException,并演示与键相关联的值可被替换。该示例演示,如果程序必须经常尝试词典中不存在的键值,如何使用 TryGetValue 方法作为更有效的检索值的方法,该示例还演示在调用 Add 方法之前如何使用 ContainsKey 方法来测试键是否存在。该示例演示如何枚举字典中的键和值,以及如何分别使用 Keys 属性和 Values 属性来单独枚举键和值。最后,该示例演示 Remove 方法using System;using System.Collections.Generic;public class Example{ public static void Main() { // Create a new dictionary of strings, with string keys. // Dictionary<string, string> openWith = new Dictionary<string, string>(); // Add some elements to the dictionary. There are no // duplicate keys, but some of the values are duplicates. openWith.Add("txt", "notepad.exe"); openWith.Add("bmp", "paint.exe"); openWith.Add("dib", "paint.exe"); openWith.Add("rtf", "wordpad.exe"); // The Add method throws an exception if the new key is // already in the dictionary. try { openWith.Add("txt", "winword.exe"); } catch (ArgumentException) { Console.WriteLine("An element with Key = \"txt\" already exists."); } // The Item property is another name for the indexer, so you // can omit its name when accessing elements. Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]); // The indexer can be used to change the value associated // with a key. openWith["rtf"] = "winword.exe"; Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]); // If a key does not exist, setting the indexer for that key // adds a new key/value pair. openWith["doc"] = "winword.exe"; // The indexer throws an exception if the requested key is // not in the dictionary. try { Console.WriteLine("For key = \"tif\", value = {0}.", openWith["tif"]); } catch (KeyNotFoundException) { Console.WriteLine("Key = \"tif\" is not found."); } // When a program often has to try keys that turn out not to // be in the dictionary, TryGetValue can be a more efficient // way to retrieve values. string value = ""; if (openWith.TryGetValue("tif", out value)) { Console.WriteLine("For key = \"tif\", value = {0}.", value); } else { Console.WriteLine("Key = \"tif\" is not found."); } // ContainsKey can be used to test keys before inserting // them. if (!openWith.ContainsKey("ht")) { openWith.Add("ht", "hypertrm.exe"); Console.WriteLine("Value added for key = \"ht\": {0}", openWith["ht"]); } // When you use foreach to enumerate dictionary elements, // the elements are retrieved as KeyValuePair objects. Console.WriteLine(); foreach( KeyValuePair<string, string> kvp in openWith ) { Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); } // To get the values alone, use the Values property. Dictionary<string, string>.ValueCollection valueColl = openWith.Values; // The elements of the ValueCollection are strongly typed // with the type that was specified for dictionary values. Console.WriteLine(); foreach( string s in valueColl ) { Console.WriteLine("Value = {0}", s); } // To get the keys alone, use the Keys property. Dictionary<string, string>.KeyCollection keyColl = openWith.Keys; // The elements of the KeyCollection are strongly typed // with the type that was specified for dictionary keys. Console.WriteLine(); foreach( string s in keyColl ) { Console.WriteLine("Key = {0}", s); } // Use the Remove method to remove a key/value pair. Console.WriteLine("\nRemove(\"doc\")"); openWith.Remove("doc"); if (!openWith.ContainsKey("doc")) { Console.WriteLine("Key \"doc\" is not found."); } }}/* This code example produces the following output:An element with Key = "txt" already exists.For key = "rtf", value = wordpad.exe.For key = "rtf", value = winword.exe.Key = "tif" is not found.Key = "tif" is not found.Value added for key = "ht": hypertrm.exeKey = txt, Value = notepad.exeKey = bmp, Value = paint.exeKey = dib, Value = paint.exeKey = rtf, Value = winword.exeKey = doc, Value = winword.exeKey = ht, Value = hypertrm.exeValue = notepad.exeValue = paint.exeValue = paint.exeValue = winword.exeValue = winword.exeValue = hypertrm.exeKey = txtKey = bmpKey = dibKey = rtfKey = docKey = htRemove("doc")Key "doc" is not found. */此类型的公共静态(在 Visual Basic 中为 Shared)成员是线程安全的。但不能保证任何实例成员是线程安全的。只要不修改该集合,Dictionary<(Of <(TKey, TValue>)>) 就可以同时支持多个阅读器。 即便如此,从头到尾对一个集合进行枚举本质上并不是一个线程安全的过程。当出现枚举与写访问互相争用这种极少发生的情况时,必须在整个枚举过程中锁定集合。若要允许多个线程访问集合以进行读写操作,则必须实现自己的同步。
[解决办法]
using System;using System.Collections;namespace program{ class wangjun { /// <summary> /// 对哈希表进行遍历,使用了两种 方法(foreach dictionaryentry和 foreach string) /// </summary> /// <param name="args"></param> static void Main(string[] args) { //建立哈希表对象 Hashtable hs = new Hashtable(); hs.Add("13", "1234123"); hs.Add("2", "sadfgasd"); hs.Add("4", "23123fs"); hs.Add("3", "12312sdf"); hs.Add("5", "sdfgasdfg"); //删除元素 hs.Remove("13"); //使用字典方法遍历哈希表 foreach (DictionaryEntry dic in hs) { Console.WriteLine("key={0} value={1}",(string)dic.Key,(string)dic.Value); } //空白两行 for (int i = 0; i < 2; i++) { Console.WriteLine(); } //建立arraylist对象并得到了哈希表的键的集合实例,用于对哈希表的的排序+遍历 ArrayList al = new ArrayList(hs.Keys); //对arraylist中的值(哈希表键)进行名称排序 al.Sort(); //使用foreach进行遍历 foreach (string s in al) { //这里用到了(string)hs[s]因为哈希表存放的是object所以这里要进行强制转换 Console.WriteLine("key={0} value={1}",s,(string)hs[s]); } //空白两行 for (int i = 0; i < 2; i++) { Console.WriteLine(); } //对arraylist的值(哈希表的键)进行反向排序 al.Reverse(); foreach (string s in al) { //这里用到了(string)hs[s]因为哈希表存放的是object所以这里要进行强制转换 Console.WriteLine("key={0} value={1}",s,(string)hs[s]); } } }}
[解决办法]
up
[解决办法]
学习了
[解决办法]
接分
[解决办法]
关于泛型,msdn里已解释得很清楚
至于动态加载树形,给个讨论方向。。。
[解决办法]
UP
[解决办法]
接着UP
[解决办法]
学习
[解决办法]
学习
jf
[解决办法]
Dictionary(Of TKey, TValue)是一个公共类,那么使用他必须要创建一个实例,Dictionary(Of TKey, TValue)是存储一对键和值的集合,通过键可以访问对应的值,Dictionary(Of TKey, TValue)主要的优势是指定了参数类型,意味着不存在类型转换,不需要使用Ctype之类的转换,相对于后期绑定性能明显提高了。但是也显的没有后期绑定那样灵活
http://www.cnblogs.com/lsxqw2004/archive/2009/01/29/1381339.html
[解决办法]
帮顶了
[解决办法]
学习
[解决办法]
这么多的例子。。。。
不错。。。
[解决办法]
不错..学习了..帮顶
[解决办法]
再接再厉
[解决办法]
up study
------解决方案--------------------
Dictionary <TKey, TValue> 和LIST <> 地用法差不多,只不过多一个元素,TKey不能重复就是了
Dictionary <string, string> dict = new Dictionary <string, string> ();
dict.Add("1", "test");
.
.
.
foreach (KeyValuePair<string, string> col in dict)
{
string strKey = col.Key;
string strValue = col.Value;
}
[解决办法]
学习~
[解决办法]
dictionary不是哈希表
[解决办法]
帮顶一下
[解决办法]
高手如云啊…………本来想顶2楼的,可是看了12楼的觉得12楼的更细致,结果后面的楼层一个比一个说得细致
我的神啊………………
还是顶楼主吧…………
[解决办法]
帮顶
[解决办法]
up
[解决办法]
学习了。
[解决办法]
一般都用List<>,看来以后简单的列表可以用Dictionary
[解决办法]
学习学习,友情帮顶
[解决办法]
共同学习!向高手学习!
[解决办法]
UP
[解决办法]
很少用的到,,学习了,,
[解决办法]
mark
不过我觉得用法上都一样
只不过给优化了下
[解决办法]
每个元素都是一个存储在 DictionaryEntry 对象中的键/值对。键不能为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing),但值可以。
要重写 Object..::.GetHashCode 方法(或 IHashCodeProvider 接口)和 Object..::.Equals 方法(或 IComparer 接口),需要有被 Hashtable 用作键的对象。方法和接口的实现必须以相同的方式处理大小写;否则,Hashtable 的行为可能不正确。例如,创建 Hashtable 时,您必须配合使用 CaseInsensitiveHashCodeProvider 类(或任何不区分大小写的 IHashCodeProvider 实现)和 CaseInsensitiveComparer 类(或任何不区分大小写的 IComparer 实现)。
此外,如果该键存在于 Hashtable 中,那么当使用相同参数调用这些方法时,这些方法必须生成相同的结果。还有一种方式是使用具有 IEqualityComparer 参数的 Hashtable 构造函数。如果键相等性只是引用相等性,则 Object..::.GetHashCode 和 Object..::.Equals 的继承实现将满足需要。
只要键对象用作 Hashtable 中的键,它们就必须是永远不变的。
当把某个元素添加到 Hashtable 时,将根据键的哈希代码将该元素放入存储桶中。该键的后续查找将使用键的哈希代码只在一个特定存储桶中搜索,这将大大减少为查找一个元素所需的键比较的次数。
Hashtable 的加载因子确定元素与存储桶的最大比率。加载因子越小,平均查找速度越快,但消耗的内存也增加。默认的加载因子 1.0 通常提供速度和大小之间的最佳平衡。当创建 Hashtable 时,也可以指定其他加载因子。
当向 Hashtable 添加元素时,Hashtable 的实际加载因子将增加。当实际加载因子达到指定的加载因子时,Hashtable 中存储桶的数目自动增加到大于当前 Hashtable 存储桶数两倍的最小质数。
Hashtable 中的每个键对象必须提供其自己的哈希函数,可通过调用 GetHash 访问该函数。但是,可将任何实现 IHashCodeProvider 的对象传递到 Hashtable 构造函数,而且该哈希函数用于该表中的所有对象。
Hashtable 的容量是 Hashtable 可拥有的元素数。随着向 Hashtable 中添加元素,容量通过重新分配按需自动增加。
[解决办法]
学习!
[解决办法]
学习
[解决办法]
来学习了。前辈们辛苦了~~
[解决办法]
构建html代码而已 根据数据库中你自己的编号规则来
[解决办法]
//Dictionary<Tkey,TValue>是泛型字典,里面的key值不能为空、不能修改、值必须唯一,Tvalue就和使用泛型一样的了,如: Dictionary<int, String> test = new Dictionary<int, string>(); for (int i = 0; i < 10;i++ ) { test.Add(i, "Test" + i.ToString()); } (test.OrderBy(u => u.Key)).ToList().ForEach(delegate(KeyValuePair<int, String> obj) { MessageBox.Show("TKey:"+obj.Key.ToString() + "\nTValue:" + obj.Value); });
[解决办法]
#region 填充树目录
public void BuildConstItems(string selectIndex)
{
///应该写出递归程序但是还是没有写出来,郁闷!
string strSele="";
DataSet ds;
DataTable dt;
FillTree ft=new FillTree();
ds=ft.GetLayer();
dt=ds.Tables[0];
TreeView1.Nodes.Clear();
for(int i=0;i<dt.Rows.Count;i++)
{
//father
TreeNode subnode1= new TreeNode();
subnode1.Text=dt.Rows[i][1].ToString();
subnode1.ID=dt.Rows[i][0].ToString();
subnode1.ImageUrl=Request.ApplicationPath+"/img/folder.gif";
subnode1.ExpandedImageUrl=Request.ApplicationPath+"/img/folderopen.gif";
subnode1.NavigateUrl="yoururl";
subnode1.Target="right" ;
//创建子目录
//son
}
dts=null;
TreeView1.Nodes.Add(subnode1);
}
// TreeView1.ExpandLevel=2;
// TreeView1.SelectedNodeIndex=selectIndex;
ds=null;
ft=null;
}
权限在数据库里设置
[解决办法]
建一张树形节点的表
根据表里的数据循环生成节点
[解决办法]
关注 顶
[解决办法]
友情帮顶………………
[解决办法]
一般都用List <>,看来以后简单的列表可以用Dictionary
[解决办法]