Hello everybody...I'm new in C# programming but recently I have a problem and need to solve it. I hope you guys can help me. This problem only can solved with List and Dictionary.

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

namespace ApplicantTestin
{
	/// The DataObject class stored with a key
	class DataObject
	{
		// Populate
	}

	class Program
	{
		static Hashtable Data = new Hashtable();
		static string[] StaticData = new string[] { "X-Ray","Echo","Alpha", "Yankee","Bravo", "Charlie", 
			"Delta",    "Hotel", "India", "Juliet", "Foxtrot","Sierra",
			"Mike","Kilo", "Lima",  "November", "Oscar", "Papa", "Qubec", 
			"Romeo",  "Tango","Golf", "Uniform", "Victor", "Whisky",  
			 "Zulu"};

		static void Main(string[] args)
		{
			for(int i=0;i<StaticData.Length; i++)
				Data.Add(StaticData[i].ToLower(), new DataObject(StaticData[i]) );
			while(true)
			{
				PrintSortedData();
				Console.WriteLine();
				Console.Write("> ");
				string str = Console.ReadLine();
				string[] strs = str.Split(' ');

				if(strs[0]=="q")
					break;
				else if(strs[0]=="printv")
					PrintSortedDataByValue();
				else if(strs[0]=="print")
					PrintSortedData();
				else if(strs[0]=="inc")
					Increase(strs[1]);
				else if(strs[0]=="dec")
					Decrease(strs[1]);
				else if(strs[0] == "swap")
					Swap(strs[1], strs[2]);
				else if (strs[0] == "ref")
					Ref(strs[1], strs[2]);
				else if (strs[0] == "unref")
					UnRef(strs[1]);
			}
		}

		/// <summary>
		/// Create a reference from one data object to another.
		/// </summary>
		/// <param name="key1">The object to create the reference on</param>
		/// <param name="key2">The reference object</param>
		static void Ref(string key1, string key2)
		{
			// Populate
		}

		/// <summary>
		/// Removes an object reference on the object specified.
		/// </summary>
		/// <param name="key">The object to remove the reference from</param>
		static void UnRef(string key)
		{
			// Populate
		}

		/// <summary>
		/// Swap the data objects stored in the keys specified
		/// </summary>
		static void Swap(string key1, string key2)
		{
			// Populate

		}

		/// <summary>
		/// Decrease the Value field by 1 of the 
		/// data object stored with the key specified
		/// </summary>
		static void Decrease(string key)
		{
			// Populate
		}

		/// <summary>
		/// Increase the Value field by 1 of the 
		/// data object stored with the key specified
		/// </summary>
		static void Increase(string key)
		{
			// Populate
		}


		/// <summary>
		/// Prints the information in the Data hashtable to the console.
		/// Output should be sorted by key 
		/// References should be printed between '<' and '>'
		/// The output should look like the following : 
		/// 
		/// 
		/// Alpha...... -3
		/// Bravo...... 2
		/// Charlie.... <Zulu>
		/// Delta...... 1
		/// Echo....... <Alpha>
		/// --etc---
		/// 
		/// </summary>
		static void PrintSortedData()
		{
			// Populate
		}
	

		/// <summary>
		/// Prints the information in the Data hashtable to the console.
		/// Output should be sorted by stored value
		/// References should be printed between '<' and '>'
		/// Sorting order start from max to min, larger value takes priority.
		/// The output should look like the following : 
		/// 
		/// 
		/// Bravo...... 100
		/// Echo...... 99
		/// Zulu...... 98
		/// Charlie.... <Zulu>
		/// Delta...... 34
		/// Echo....... 33
		/// Alpha...... <Echo>
		/// --etc---
		/// 
		/// </summary>
		static void PrintSortedDataByValue()
		{
			// Populate
		}
	}
}

Hi,
would you mind telling us some more about what would you like to achive?
I mean what is the source, and what to put together and the output of course.

Otherwise salving problems when you have a key and multiple value can really be salved with the Dictionary collections, and for the Dictionary`s value you can use generic list<T>:

Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>();
//1st key:
dic.Add("key1", new List<string>{"value1_of_key1"};
dic["key1"].Add("value2_of_key1");
// and so on for the key 1

//for 2nd key:
dic.Add("key2", new List<string>{"value1_of_key2"};
dic["key2"].Add("value2_of_key2");
//and so on..
//and for the rest of the keys...

Edited 5 Years Ago by Mitja Bonca: n/a

Hi,
Actually I have been given with this source code and I need to fill in its procedure.
The explanation of its procedure is in the bookmark.
But i think this code is just to print a key and its value in the console.
But every key has a value that can be number or string.
How could we make dictionary has 2 types of value?

Simple, use object for the value. This how you initialize a new Dictionary:

Dictionary<int, object> dic = new Dictionary<int, object>();
dic.Add(1, "some value");
dic.Add(2, 3.4);
dic.Add83, true);
dic.Add("other value");

As you can see, if you use object, you can use any type of value as you like.

Dictionary Vs List

The Dictionary uses hashing to search for the data. A Dictionary first calculated a hash value for the key and this hash value leads to the target data bucket. After that, each element in the bucket needs to be checked for equality. But actually the list will be faster than the dictionary on the first item search because nothing to search in the first step. But in the second step, the list has to look through the first item, and then the second item. So each step the lookup takes more and more time. The larger the list, the longer it takes.

More about .... Dictionary Vs List Dictionary Vs List with example.

Walsh

This article has been dead for over six months. Start a new discussion instead.