C#在锯齿状数组中快速查找值的方法 [英] C# Fast way to find value in a jagged Array
问题描述
我有一个锯齿状数组 String[][]
.现在我需要在 String[n][0]
中找到具有特定值的数组我现在有一个简单的
I have a jagged Array String[][]
. Now I need to find the array with a particular value in String[n][0]
what i have a the moment is a simple
foreach foo in bar{
if(foo[0]==needle){
return foo;
}
}
正如您所见,由于显而易见的原因,这非常慢.我是 C# 新手,刚看到 indexOf,但如何在锯齿状数组中使用 indexOf?
As you may see this is very slow for obvious reasons. I'm new to C# and just saw the indexOf, but how can i use indexOf in a jagged array?
我想到的另一种方法是按String[n][0]
对数组进行排序,转到中间的记录,检查我的值是更大还是更大,跳入一半上/下区域等等,可能是 3 到 4 次,这样我就可以更快地找到记录.
Another way i tought of is Sorting the Array by String[n][0]
, going to the record in the middle, check if my value is larger or bigger, jump in the half of the upper/lower area and so on, maybe 3 or 4 times so i can find the record faster.
那么在我只知道 [][0]
的锯齿状数组中获取数组的最快方法是什么?
So whats the fastest way of getting the Array in a jagged Array where I only know [][0]
?
推荐答案
我会使用 Dictionary
,其中键是数组的第一项.字典有固定的时间访问,如果整个数据适合内存,随机访问的速度非常快.
I would use a Dictionary<int, int[]>
where the key is the first item of the array. Dictionaries have constant time access, and are very fast for random access if the entire data fits in memory.
这篇关于C#在锯齿状数组中快速查找值的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!