Pow*_*ord 452
Dictionary可能是最接近的.System.Collections.Generic.Dictionary实现System.Collections.Generic.IDictionary接口(类似于Java的Map接口).
您应该注意的一些值得注意的差异:
put和get用于设置/获取项目的方法
myMap.put(key, value)MyObject value = myMap.get(key)[]索引来设置/获取项目
myDictionary[key] = valueMyObject value = myDictionary[key]null 按键
HashMap允许使用null键Dictionary抛出一个ArgumentNullExceptionHashMap将用新的值替换现有值.Dictionary如果使用[]索引,.NET 将用新的值替换现有值.如果你使用这个Add方法,它会抛出一个ArgumentException.HashMap将返回null.Dictionary会抛出一个KeyNotFoundException.您可以使用该TryGetValue方法而不是[]索引来避免这种情况:MyObject value = null;
if (!myDictionary.TryGetValue(key, out value)) { /* key doesn't exist */ }Dictionary有一种ContainsKey方法可以帮助解决前两个问题.
小智 36
我需要一个接受"null"键的字典,但似乎没有本地字,所以我写了自己的.实际上,它非常简单.我从Dictionary继承,添加了一个私有字段来保存"null"键的值,然后覆盖了索引器.它是这样的:
public class NullableDictionnary : Dictionary<string, string>
{
string null_value;
public StringDictionary this[string key]
{
get
{
if (key == null)
{
return null_value;
}
return base[key];
}
set
{
if (key == null)
{
null_value = value;
}
else
{
base[key] = value;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助将来的某个人.
==========
我把它修改为这种格式
public class NullableDictionnary : Dictionary<string, object>
Run Code Online (Sandbox Code Playgroud)
Aja*_*iki 15
让我通过"codaddict算法"的例子来帮助你理解它
" C#中的字典 "是并行Universe中的"Java中的Hashmap ".
一些实现是不同的.请参阅下面的示例以更好地了解.
声明Java HashMap:
Map<Integer, Integer> pairs = new HashMap<Integer, Integer>();
Run Code Online (Sandbox Code Playgroud)
声明C#字典:
Dictionary<int, int> Pairs = new Dictionary<int, int>();
Run Code Online (Sandbox Code Playgroud)
从某个位置获取值:
pairs.get(input[i]); // in Java
Pairs[input[i]]; // in C#
Run Code Online (Sandbox Code Playgroud)
在位置设置值:
pairs.put(k - input[i], input[i]); // in Java
Pairs[k - input[i]] = input[i]; // in C#
Run Code Online (Sandbox Code Playgroud)
从Codaddict的算法下面可以观察到一个总体例子.
Java中的codaddict算法:
import java.util.HashMap;
public class ArrayPairSum {
public static void printSumPairs(int[] input, int k)
{
Map<Integer, Integer> pairs = new HashMap<Integer, Integer>();
for (int i = 0; i < input.length; i++)
{
if (pairs.containsKey(input[i]))
System.out.println(input[i] + ", " + pairs.get(input[i]));
else
pairs.put(k - input[i], input[i]);
}
}
public static void main(String[] args)
{
int[] a = { 2, 45, 7, 3, 5, 1, 8, 9 };
printSumPairs(a, 10);
}
}
Run Code Online (Sandbox Code Playgroud)
Codaddict在C#中的算法
using System;
using System.Collections.Generic;
class Program
{
static void checkPairs(int[] input, int k)
{
Dictionary<int, int> Pairs = new Dictionary<int, int>();
for (int i = 0; i < input.Length; i++)
{
if (Pairs.ContainsKey(input[i]))
{
Console.WriteLine(input[i] + ", " + Pairs[input[i]]);
}
else
{
Pairs[k - input[i]] = input[i];
}
}
}
static void Main(string[] args)
{
int[] a = { 2, 45, 7, 3, 5, 1, 8, 9 };
//method : codaddict's algorithm : O(n)
checkPairs(a, 10);
Console.Read();
}
}
Run Code Online (Sandbox Code Playgroud)
小智 8
使用字典 - 它使用哈希表,但类型安全。
另外,您的 Java 代码
int a = map.get(key);
//continue with your logic
Run Code Online (Sandbox Code Playgroud)
最好用 C# 编码,如下所示:
int a;
if(dict.TryGetValue(key, out a)){
//continue with your logic
}
Run Code Online (Sandbox Code Playgroud)
这样,您可以将变量“a”的需求范围限制在块内,并且如果以后需要它,仍然可以在块外访问它。