Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#在锯齿数组中查找值的快速方法_C#_Performance_Jagged Arrays - Fatal编程技术网

C#在锯齿数组中查找值的快速方法

C#在锯齿数组中查找值的快速方法,c#,performance,jagged-arrays,C#,Performance,Jagged Arrays,我有一个锯齿状数组字符串[][]。现在我需要在String[n][0] 我现在的想法很简单 foreach foo in bar{ if(foo[0]==needle){ return foo; } } 正如你可能看到的,由于明显的原因,这是非常缓慢的。我是C#新手,刚刚看到了indexOf,但是如何在锯齿数组中使用indexOf呢 另一种方法是用 String [n](0)< /代码>对数组进行排序,在中间记录,检查值是否较大,在上/下区域的一半跳跃,等等,可能是3次或4次,这样我就

我有一个锯齿状数组
字符串[][]
。现在我需要在
String[n][0]
我现在的想法很简单

foreach foo in bar{
 if(foo[0]==needle){
  return foo;
 }
}
正如你可能看到的,由于明显的原因,这是非常缓慢的。我是C#新手,刚刚看到了indexOf,但是如何在锯齿数组中使用indexOf呢

另一种方法是用<代码> String [n](0)< /代码>对数组进行排序,在中间记录,检查值是否较大,在上/下区域的一半跳跃,等等,可能是3次或4次,这样我就能更快地找到记录。


那么,在我只知道
[[0]
的锯齿状数组中获取数组的最快方法是什么呢?

我会使用
字典,其中键是数组的第一项。字典有固定的时间访问,如果整个数据都放在内存中,则随机访问的速度非常快。

那么,您正在为从
0
n
的每一个
i
搜索
数组[i][0]
上可以搜索的值,对吗?
然后,它与交错数组无关,而是在集合中查找一个值(在您的例子中是
{array[i][0]的集合),对于每一个0可能比锯齿状数组更适合使用字典,其中键是每个数组的第一项。因为字典是哈希表,查找应该很快

// Some test data.    
var dictionary = new Dictionary<string, string[]>
{
    { "foo1", new string[] { "foo1", "bar1" }},
    { "foo2", new string[] { "foo2", "bleh" }},
    { "foo3", new string[] { "foo3", "bar3", "hello", "world" }},
    { "foo4", new string[] { "foo4",  }},
    { "foo5", new string[] { "foo5", "bar5", "test" }},
};

string[] item = dictionary["foo3"]; // Fast look up.
//一些测试数据。
var字典=新字典
{
{“foo1”,新字符串[]{“foo1”,“bar1”},
{“foo2”,新字符串[]{“foo2”,“bleh”},
{“foo3”,新字符串[]{“foo3”,“bar3”,“hello”,“world”},
{“foo4”,新字符串[]{“foo4”,},
{“foo5”,新字符串[]{“foo5”,“bar5”,“test”},
};
string[]item=dictionary[“foo3”];//快速查找。

你说的“慢”是什么意思?“bar”表示多少个1D数组contain,你需要多久进行一次这样的搜索,你得到了什么性能,你想要达到什么性能?你描述的排序和跳转方法叫做,我有大约25k foo和25k针。我不知道具体需要多长时间,但我猜大约需要30分钟。我需要把它缩短到5分钟。当foo[0]不会在搜索之间改变,创建一个像Elian Ebbing建议的字典将在几秒钟内完成。@user##,你的意思是30分钟还是30毫秒?字典不允许你拥有相同的键。如果两个数组的第一个整数元素相同,会发生什么?你的字典中有两个不同的数组这是同一把钥匙!