C#中的哈希表ArrayList#

问题描述:

我想有一个ArrayList它包含HashTables。我创建了一个Hashtable并添加了值。然后我将它添加到ArrayList。然后我改变了哈希表的值,并再次添加了数组列表。它不会保存第一个值并结束与上一个值完全相同的重复值!C#中的哈希表ArrayList#

有什么建议吗?这里是我的代码

namespace ValuesTest 
{ 
    internal class Class1 
    {  
     public static ArrayList StartList = new ArrayList(); 
     public static Hashtable Start = new Hashtable();  

     static void Main(string[] args) 
     {  
      Start["active"] = true; 
      Start["name"] = "prog1"; 
      Start["path"] = @"C:\programfiles\prog1"; 
      Start["parameter"] = string.Empty; 

      StartList.Add(Start); 

      Start["active"] = false; 
      Start["name"] = "prog2"; 
      Start["path"] = @"C:\programfiles\prog2"; 
      Start["parameter"] = "/q"; 

      StartList.Add(Start); 

      foreach (Hashtable HT in StartList) 
      { 
       Console.WriteLine(HT["active"] + " - " + HT["name"] + " - " + HT["path"] + " - " + HT["parameter"]); 
       // it will always gives 
       // False - prog2 - C:\programfiles\prog2 - /q  
      } 

      Console.ReadLine(); 
     } 
    } 
} 
+5

我所看到的最近有很多ArrayList的。相反,除非您使用.Net框架1/1.1,否则应该使用列表。列表将提供更好的性能并避免投射(这种'foreach'确实隐含在您的案例中)。问候 – 2010-08-05 12:21:16

+1

您不会创建一个新的Hashtable实例,而是覆盖这些值。 P.S.:如果您期待答案,在主题中使用多个感叹号并不会有帮助:p – 2010-08-05 12:21:33

+0

但是,人们应该对问题投票,而不是语法。 – 2010-08-05 12:28:54

将开始移动到您的Main函数中,并重新初始化它以进行第二次添加(并且@lasseespeholt表示使用List<T>)。

static List<Hashtable> StartList = new List<Hashtable>(); 

    static void Main(string[] args) 
    { 
     Hashtable Start = new Hashtable(); 
     Start["active"] = true; 
     Start["name"] = "prog1"; 
     Start["path"] = @"C:\programfiles\prog1"; 
     Start["parameter"] = string.Empty; 

     StartList.Add(Start); 

     Start = new Hashtable(); 
     Start["active"] = false; 
     Start["name"] = "prog2"; 
     Start["path"] = @"C:\programfiles\prog2"; 
     Start["parameter"] = "/q"; 

     StartList.Add(Start); 

你修改唯一StartHashtable对象,你必须创建一个新的:

Start = new Hashtable();

之后第一:

StartList.Add(Start);

请记住,您只是将参考添加到对象到ArrayList:因此,您的代码执行的操作是:填充散列表,将其引用添加到列表中,再修改它,然后添加相同的引用再次。


想添加,为什么你使用散列表?如果你想使用一个新的课程,那么他们可以有一个PrintInfo,或者一个ToString覆盖来获取你需要的信息 - 大概是一个Execute方法。

+1

我不确定,这是我知道的方式,所以我可以稍后序列化:-) 你有没有类似于我的例子?这使我能够序列化它? – 2010-08-05 12:42:47

+0

是的,取决于你如何以及为什么要序列化,你只需在顶部用'[Serializable]'标记该类。请参阅此处的示例:http://msdn.microsoft.com/en-us/library/system.serializableattribute.aspx并在此处指导:http://msdn.microsoft.com/en-us/library/ms973893.aspx。 – 2010-08-05 12:50:55

一些这方面的更多反馈:尽管尼克斯已经回答了你的问题,我就把你的Hashtable以及(就如同ArrayList的它的日),虽然你的概念不借给自己漂亮吧:你存储串反对..

所以,复制和改变,我们会最终

static void Main(string[] args) 
{ 
     Dictionary<string, object> Start = new Dictionary<string, object>(); 
     Start["active"] = true; 
     Start["name"] = "prog1"; 
     Start["path"] = @"C:\programfiles\prog1"; 
     Start["parameter"] = string.Empty; 

     StartList.Add(Start); 

     Dictionary<string, object> Start = new Dictionary<string, object>(); 
     Start["active"] = false; 
     Start["name"] = "prog2"; 
     Start["path"] = @"C:\programfiles\prog2"; 
     Start["parameter"] = "/q"; 

     StartList.Add(Start); 

但我会走的更远:如果你只是想创建子进程,这就是类Process是。它只保留StartInfo属性中的相同信息(“有效”除外)。

另外,(?更好)的方法是创建一个值类这组信息:

class YourStartInfo 
{ 
    public bool Active { get; set; } 
    public string Name { get; set; } 
    public string Path { get; set; } 
    public string Parameter { get; set; } 
} 

,改变你的代码使用的是:

static List<YourStartInfo> StartList = new List<YourStartInfo>(); 

static void Main(string[] args) 
{  
     StartList.Add(new YourStartInfo { 
      Active = true, 
      Name = "prog1", 
      Path = @"C:\programfiles\prog1"; 
      Parameter = string.Empty 
     }); 

     StartList.Add(new YourStartInfo { 
      Active = false, 
      Name = "prog2", 
      Path = @"C:\programfiles\prog2"; 
      Parameter = "/q" 
     }); 

     foreach (YourStartInfo startInfo in StartList) 
     { 
      // Access the information in a sane way, not as object here 
      if (startInfo.Active) 
      { 
       // Probably launch it? 
      } 
     } 

     Console.ReadLine(); 
    } 
+0

其实,我发现我自己现在在做:-) 但感谢您的建议:-) 它给了我一个很好的推动,我在正确的道路上, 欢呼声。 – 2010-08-05 14:52:40