数据结构设计问题

发布于 2024-10-03 13:15:50 字数 740 浏览 4 评论 0原文

我计划设计一个包含这样的元素的数据结构,

{1, 13, 15, 113, 117, 145}
{2, 23, 27}
{5, 53, 55, 511, 519}
{9, 11}

到目前为止我有两个想法。

第一。建立一个字典(键,值);

When Key = 1, Value = ArrayList(13, 15, 113, 117, 145)
When Key = 2, Value = ArrayList(23, 27)
When Key = 5, Value = ArrayList(53, 55, 511, 519)
When Key = 9, Value = ArrayList(11)

第二。构建一个 ArrayList(SubArrayLists)

subArrayList1 {1, 13, 15, 113, 117, 145}
subArrayList2 {2, 23, 27}
subArrayList3 {5, 53, 55, 511, 519}
subArrayList4 {9, 11}

我计划在运行时访问该元素。哪种解决方案更好?

感谢您的评论。

[已更新]
1. 上面列出的int数字可能代表某些对象。像字典对象等
2. 应用程序启动后,所有数据都会加载到内存中。然后只读允许。不需要写入/添加/删除等。

I plan to design a data structure including elements as this,

{1, 13, 15, 113, 117, 145}
{2, 23, 27}
{5, 53, 55, 511, 519}
{9, 11}

I have two ideas till now.

1st. Build an Dictionary(Key, Value);

When Key = 1, Value = ArrayList(13, 15, 113, 117, 145)
When Key = 2, Value = ArrayList(23, 27)
When Key = 5, Value = ArrayList(53, 55, 511, 519)
When Key = 9, Value = ArrayList(11)

2nd. Build an ArrayList(SubArrayLists)

subArrayList1 {1, 13, 15, 113, 117, 145}
subArrayList2 {2, 23, 27}
subArrayList3 {5, 53, 55, 511, 519}
subArrayList4 {9, 11}

I plan to access the element when running time. Which solution is better?

Thanks for your comments.

[Updated]
1. The int numbers above listed may stand for some objects. like dictionary object, etc.
2. All the data will be load to memory after application launched. Then read only permitted. Not need to write/add/delete etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甩你一脸翔 2024-10-10 13:15:55

如果您打算进行随机访问,那么字典将是一个不错的选择。但请记住,您需要具有唯一的键(因为您要访问每个列表的第一个元素,因为每个列表的键第一个元素应该是唯一的)

如果您要按顺序访问数据结构的元素,则 ArrayList 更好

If you are going to do random access then Dictionary will be a good option. But please keep in mind that you need to have unique keys ( since you are going first element of each list as the key first element of each list should be unique )

If you are going to access elements of the data structure sequentially ArrayList is better

恏ㄋ傷疤忘ㄋ疼 2024-10-10 13:15:55

这取决于对您来说哪个更重要,更快的读取速度还是更快的写入?字典可以非常快速地访问存储的值,但同时将值添加到字典中将需要更长的时间。

如果您有很多需要多次访问的值,那么字典就是您的最佳选择,如果您发现自己在另一个 for 循环示例中循环访问值列表:

foreach(var item in ListA)
{
  foreach(var item in ListB)
  {
     // Match against all values in list B        

  }
}

在上面的示例中,最好是列表 B 是字典而不是列表,因为您在 ListA 的每次迭代中都循环遍历列表。随着 ListB 中元素数量的增加,上述代码的执行时间也会增加,但是如果它是字典,则不会有太大区别。

It depends on what is more important to you, faster read speeds or faster writes? A Dictionary will give really quick access to stored values but at the same time adding values to dictionary will take longer.

If you have a lot of values that need to be accessed numerous times, then dictionary is the way to go, if you find yourself looping through a list of values inside another for loop example:

foreach(var item in ListA)
{
  foreach(var item in ListB)
  {
     // Match against all values in list B        

  }
}

In the example above, it'll be best if List B is a dictionary instead of a list since you are looping through the list on every iteration of ListA. As the number of elements in ListB increases the execution time of the above code will increase, however if instead it is a dictionary, it won't make much of a difference.

彡翼 2024-10-10 13:15:54

锯齿状数组怎么样? http://msdn.microsoft.com/en-us/library/2s05feca.aspx

通过数组索引访问元素的时间复杂度为 O(1)。

How about a Jagged Array ? http://msdn.microsoft.com/en-us/library/2s05feca.aspx

Access to elements via an array index is O(1).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文