简体   繁体   English

选择合适的数据结构

[英]Choosing a suitable data structure

Whenever I start my application I need to iterate over all entries in a matrix, and for each entry construct a variable amount of objects. 每当我启动我的应用程序时,我需要迭代矩阵中的所有条目,并为每个条目构造一个可变数量的对象。

Later on when given a matrix, I need to iterate through the new matrix, and for all non-zero entries i need to retrieve all the objects calculated earlier for that particular entry and iterate through them. 稍后当给定矩阵时,我需要遍历新矩阵,并且对于所有非零条目,我需要检索先前为该特定条目计算的所有对象并迭代它们。

The focus is on getting retrieved and iterated through the objects. 重点是通过对象获取和迭代。 The construction time is less important. 施工时间不太重要。

I was thinking of some kind of map structure, where i map the entry to a linked list. 我在考虑某种地图结构,我将条目映射到链表。 Is this suitable? 这个合适吗? Or can you suggest something other? 或者你能推荐别的东西吗?

As a side note, I am implementing this in Java, so if you know a concrete implementation to solve this problem I would be happy to know! 作为旁注,我在Java中实现它,所以如果你知道解决这个问题的具体实现,我很乐意知道!

Regards Jesper 问候Jesper

I was thinking of some kind of map structure, where i map the entry to a linked list. Is 
this suitable? Or can you suggest something other?

This sounds like a great application to use the Multimap API that is part of google commons. 这听起来像是一个很棒的应用程序,可以使用谷歌公共部分的Multimap API You can also get it as part of the guava libraries. 您也可以将其作为番石榴库的一部分

If you don't want to add a dependency on that, however, maintaining a Map<Object1, List<Object2>> would probably be the cleanest way to do it. 但是,如果您不想在其上添加依赖项,则维护Map<Object1, List<Object2>>可能是最干净的方法。

For the matrices, you could just use arrays, and probably make a class Matrix to abstract away what the matrix is actually made of. 对于矩阵,你可以只使用数组,并且可能使一个类Matrix抽象出矩阵的实际构成。 For the storage of the objects, use a matrix of List<WhateverKindOfObjectYouAreConstructing> s. 要存储对象,请使用List<WhateverKindOfObjectYouAreConstructing>的矩阵。 You can then quickly find the list for a given entry, and then quickly iterate through it. 然后,您可以快速找到给定条目的列表,然后快速迭代它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM