双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

HashTable的Values为ArrayList应该怎么转换?

ghostwolf_78 ghostwolf_78 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
using System;
public class Class1
{
static void Main()
{
System.Collections.Hashtable ht = new ystem.Collections.Hashtable();
System.Collections.ArrayList al2 = new System.Collections.ArrayList();
while(true)
{
string str = Console.ReadLine();

if(str.Equals("exit"))System.Environment.Exit(1);
string [] s = str.Split(' ');
if(ht.ContainsKey(s[0]))
{
System.Collections.ArrayList al = (System.Collections.ArrayList) ht[s[0]] as System.Collections.ArrayList;
al.Add(s[1]);
ht.Remove(s[0]);
ht.Add(s[0],al);
}
else
{
ht.Add(s[0],al2.Add(s[1]));
}
System.Collections.IDictionaryEnumerator ide = ht.GetEnumerator();
while(ide.MoveNext())
{
System.Collections.ArrayList al = (System.Collections.ArrayList)ide.Value as System.Collections.ArrayList;
Console.WriteLine("ht[key]={0}=======================>",ide.Key);
for(int i=0;i<al.Count;i++)
{
Console.WriteLine("ht[key]={0},ht[value]={1}",ide.Key,al[i]);
}

}
}
}
}
说明:hashtable[key=string][value=ArrayList]
我想打印key下的所有Value元素,而Value放的是ArrayList.
所以Value需要转换一下.我用 as 的方式转换报错!

未处理的异常: System.InvalidCastException: 指定的转换无效。
...全文
评论 4
收藏 已收藏
分享
返回顶部
ghostwolf_78
C# 111,129 成员 642,540 内容