番石榴表与二维数组 [英] Guava Table vs. 2D array
问题描述
有什么用Guava表实施和一个二维数组,如果我事先知道数组的大小区别?
What is the difference between using a Guava Table implementation and a 2D array if I know the size of the array beforehand?
是其中一个比另一个更有效?怎么样?
难道让运行中的时间差?
Is one more efficient than the other? How? Would it make a difference in running time?
推荐答案
最明显和最关键的区别是,一个数组总是索引,以 INT
S,而一个表可与任意对象进行索引。
The most obvious and crucial difference is that an array is always indexed, with int
s, whereas a Table can be indexed with arbitrary objects.
从番石榴网站表实例>:
Consider the Table Example from the Guava site:
Table<Vertex, Vertex, Double> weightedGraph = HashBasedTable.create();
weightedGraph.put(v1, v2, 4);
...
索引这里通过顶点
对象发生。如果你想要做同样的一个数组,你将不得不一些 getIndex
方法添加到顶点
类,和accesss阵列像这样
The indexing here happens via Vertex
objects. If you wanted to do the same with an array, you would have to add some getIndex
method to the Vertex
class, and accesss the array like this
double array[][] = new double[4][5];
array[v1.getIndex()][v2.getIndex()] = 4;
这是不方便的,特别是难以维持 - 尤其是,当指数可能会改变(或顶点时必须添加/删除,虽然你提到这是不适合你的情况下)
This is inconvenient and particularly hard to maintain - especially, when the indices may change (or when vertices have to be added/removed, although you mentioned that this is not the case for you).
此外,该番石榴表允许获得的行的或的列的作为独立的实体。在一个二维数组,你可以随时访问任何一个的行的或者一个的列的数组的 - 这取决于你如何跨preT的2维数组的。该表允许的形式,访问这两个,每个地图
。
Additionally, the Guava table allows obtaining rows or columns as separate entities. In a 2D array, you can always access either one row or one column of the array - depending on how you interpret the 2 dimensions of the array. The table allows accessing both, each in form of a Map
.
关于性能:会有,你就会有性能上的 noticable 的差异情况。特别是当你有一个的大的二维数组的原始的类型( INT
,双击
等)和替代将是一个大表与相应的参考的类型(整数
,双
等)。但同样,这只会是noticable当阵列/表是真正的大。
Concerning the performance: There will be cases where you'll have a noticable difference in performance. Particularly when you have a large 2D array of primitive types (int
, double
etc), and the alternative would be a large table with the corresponding reference types (Integer
, Double
etc). But again, this will only be noticable when the array/table is really large.
这篇关于番石榴表与二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!