您的位置:首页 > 产品设计 > UI/UE

C# NameValueCollection 初探

2008-09-10 09:45 267 查看
/*
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]
= nvcn.GetValues(m)
;
Console.WriteLine(sa[m]
);
}
}

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
続行するには何キーを押してください . . .
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: