哈希表中存入数组示例代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Hashtable hash1 = new Hashtable(); // 创建哈希表
Hashtable hash2 = new Hashtable(); // 创建哈希表 string kk = "test";
int[] data = { , , , , }; //数组
hash1.Add("key", data); //数组作为值存入哈希表,对应键为key
hash1.Add(kk, data); int[] num1 = (int[])hash1["key"]; //取出键为key的值
int[] num2 = (int[])hash1[kk]; int x = ((int[])hash1["key"])[];
Console.WriteLine("x = " + x); Console.WriteLine("数组1:");
foreach (int i in num1) //打印数组1
Console.Write(i + " ");
Console.WriteLine("\n数组2:");
foreach (int i in num2) //打印数组2
Console.Write(i + " ");
}
}
}
输入结果为:
x =
数组1: 数组2:
请按任意键继续. . .
存入List示例代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
List<int> list = new List<int>();
list.Add();
list.Add();
list.Add();
Console.WriteLine("list :");
foreach (int a in list)
Console.Write(a + " "); Hashtable hash = new Hashtable();
hash.Add("", list); Console.WriteLine("\n从哈希表中取出list:");
List<int> re = (List<int>)hash[""];
foreach (int aPart in re)
Console.Write(aPart + " ");
}
}
}
运行结果:
list : 从哈希表中取出list:
代码简单,可以一下就看懂,类的话与之类似。