C# NameValueCollection 初探


/*
Test For Jagged ArrayList From NameValueCollection
(The Upgraded Hashmap Type, Key-->Value1, Value2,ValueN)
sQxJp-m@QiNtOsHilOvExJp4EvEr..2007/10
Copy Right By K.Shi
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using System.Collections;

namespace testNamevalueCollection
{
class testNamevalueCollection
{
static void Main(string[] args)
{
NameValueCollection nvcn
= new NameValueCollection();

nvcn.Add(
"FujiXerox", "WorkShop");
nvcn.Add(
"FujiXerox", "Teak");
nvcn.Add(
"FujiXerox", "DocuWorks");
nvcn.Add(
"FujiXerox", "PineHurst");

nvcn.Add(
"IBM", "T42");
nvcn.Add(
"IBM", "T43");
nvcn.Add(
"IBM", "T60");
nvcn.Add(
"IBM", "T60p");
nvcn.Add(
"IBM", "T61");
nvcn.Add(
"IBM", "T61p");

nvcn.Add(
"iPod", "Classic");
nvcn.Add(
"iPod", "nano");
nvcn.Add(
"iPod", "nano2");
nvcn.Add(
"iPod", "video");
nvcn.Add(
"iPod", "photo");
nvcn.Add(
"iPod", "touch");
nvcn.Add(
"iPod", "phone");


nvcn.Add(
"ATH", "ES7");
nvcn.Add(
"ATH", "CK9");

/*
// Displays the values in the NameValueCollection in two different ways.
Console.WriteLine("Displays the elements using the AllKeys property and the Item (indexer) property:");
printKeyAndValues(nvcn);
Console.WriteLine("Displays the elements using GetKey and Get:");
printKeyAndValues2(nvcn);

// Gets a value either by index or by key.
Console.WriteLine("Index 1 contains the value: {0}.", nvcn[1]);
Console.WriteLine("Key \"Ipod\" has the value: {0}.", nvcn["iPod"]);
Console.WriteLine();

// Copies the values to a string array and displays the string array.
String[] myStrArr = new String[nvcn.Count];
nvcn.CopyTo(myStrArr, 0);
Console.WriteLine("The string array contains:");
foreach (String s in myStrArr)
Console.WriteLine("\t {0}", s);
Console.WriteLine();

// Searches for a key and deletes it.
nvcn.Remove("ATH");
Console.WriteLine("The collection contains the following elements after removing \"ATH\":");
printKeyAndValues(nvcn);

// Clears the entire collection.
nvcn.Clear();
Console.WriteLine("The collection contains the following elements after it is cleared:");
printKeyAndValues(nvcn);
*/

string[][] sa = new string[nvcn.Count][];
//= new string[nvcn.Count,];

for (int m = 0; m < nvcn.Count; m++)
{
Console.WriteLine(nvcn.GetValues(m).Length);
sa[m]
= new string[nvcn.GetValues(m).Length];
for (int n = 0; n < nvcn.GetValues(m).Length; n++)
{
sa[m][n]
= nvcn.GetValues(m)[n];
Console.WriteLine(sa[m][n]);
}
}

Console.WriteLine(
"\n\n");

for (int i = 0; i < nvcn.Count; i++)
{
Console.Write(
"\nThe {0}: \n", i+1);
for (int j = 0; j < nvcn.GetValues(i).Length; j++)
{
Console.WriteLine(sa[i][j]);
}
}

}

public static void printKeyAndValues(NameValueCollection NVCN)
{
IEnumerator ietr
= NVCN.GetEnumerator();
Console.WriteLine(
"\n#####################################" +
"\n# The Table Contains: [" + NVCN.Count + "] Elements. #\n"
+ "#####################################\n");
Console.WriteLine(
"\t KEY \t VALUE");

/*
foreach (String str in NVCN.AllKeys)
{
Console.WriteLine("\t {0} \t {1}", str, NVCN[str]);
}
*/

while (ietr.MoveNext())
{
Console.WriteLine(
"\t {0} \t {1}", ietr.Current, NVCN[ietr.Current.ToString()]);
}
Console.WriteLine();
}

public static void printKeyAndValues2(NameValueCollection NVCN)
{
Console.WriteLine(
"\n#####################################" +
"\n# The Table Contains: [" + NVCN.Count + "] Elements. #\n"
+ "#####################################\n");
Console.WriteLine(
"\t [INDEX] \t KEY \t VALUE");
for (int i = 0; i < NVCN.Count; i++)
Console.WriteLine(
"\t [{0}] \t {1} \t {2}", i, NVCN.GetKey(i), NVCN.Get(i));
Console.WriteLine();
}

}
}

 

运行结果:

 

6
Win3.1
Win98
Win2k
WinXP
Longhorn
WinVista
6
T42
T43
T60
T60p
T61
T61p
7
Classic
nano
nano2
video
photo
touch
phone
2
ES7
CK9

 


The 1:
Win3.1
Win98
Win2k
WinXP
Longhorn
WinVista

The 2:
T42
T43
T60
T60p
T61
T61p

The 3:
Classic
nano
nano2
video
photo
touch
phone

The 4:
ES7
CK9
続行するには何キーを押してください . . .

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值