数组的 ArrayList 与 ArrayList 的数组与类似的东西
我正在创建一个 TableModel其列数是固定的,但行数会发生变化(大多数情况下,随着时间的函数而增加)。哪种存储数据的方法更好,
ArrayList[] columns = new ArrayList[numberOfColumns];
// Each array element is one column. Fill each of them with a new ArrayList.
...
public Object getValueAt(int row, int column) {
return columns[column].get(row);
}
即创建一个 ArrayList 数组,每个 ArrayList 代表一列,或者:
ArrayList<Object[]> rows = new ArrayList<Object[]>();
// Each ArrayList element is one row.
public Object getValueAt(int row, int column) {
return rows.get(row)[column];
}
即创建一个包含数组的 ArrayList,每个数组代表一行。
有什么想法在速度或存储方面哪个更有效吗?替代方案 1 需要为每个添加的行扩展 N 个 ArrayList,而替代方案 2 只需要扩展一个 ArrayList 但还创建一个长度为 N 的新数组(以表示新行) 。或者有一个明显的、更好的解决方案吗?
I'm creating a TableModel which will have a fixed number of columns, but the number of rows will be changing (mostly, increasing as function of time). Which would be better approach to store the data,
ArrayList[] columns = new ArrayList[numberOfColumns];
// Each array element is one column. Fill each of them with a new ArrayList.
...
public Object getValueAt(int row, int column) {
return columns[column].get(row);
}
i.e. creating an array of ArrayList
s, each ArrayList
representing one column, or:
ArrayList<Object[]> rows = new ArrayList<Object[]>();
// Each ArrayList element is one row.
public Object getValueAt(int row, int column) {
return rows.get(row)[column];
}
i.e. creating one ArrayList that holds arrays, each of which represent one row.
Any ideas which of these is more efficient in terms of speed or storage? Alternative 1 requires extending N ArrayList
s with each added row, while alternative 2 requires extending just one ArrayList
but also creating a new array of length N (to represent the new row). Or is there an obvious, better solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果列数是固定的,那么您的数据可能是面向行的或至少是行变量,此时每行应该是一个数组。固定的列数意味着您不需要重新分配阵列。
所以你的结构是:
数组元素是一行。
然而,对于您的行对象应该是什么,有几个选项:
List
或其他Collection
;或(3) 可能可以通过使用某种允许您查询列的数量、类型和名称的接口来完成。
If the number of columns is fixed then your data is probably row-oriented or at least row-variable at which point each row should be an array. Fixed numbers of columns means you don't need to reallocate your array.
So your structure is:
where the array element is one row.
There are several options for what your row object should be however:
List
or otherCollection
; or(3) can probably be done by using some kind of interface that allows you to query the number, type and name of columns.
使用单个 ArrayList 本身并访问像这样的元素怎么样?
在这种情况下,每个 ArrayList 对象都是表中的一个单元格。并且您不需要任何其他额外的结构
How about using a single ArrayList itself and accessing element like this
In this case, each ArrayList object is a cell in a table. And you need not require any other extra structure
我会选择#2 有几个原因:
首先,数组有固定的长度,而 ArrayList 是灵活的。鉴于您的 #columns 已修复,每行都有数组似乎很自然。
选项 #1 很危险,因为它隐含地要求所有 ArrayList 的长度都相同。您可能会意外地忽略添加到其中任何一个,从而产生错误。使用选项 #2 时不会出现此问题。
最后,常见的约定似乎是首先对行进行索引,然后再对列进行索引。
I would go with option #2 for several reasons
First, arrays have a fixes length whereas ArrayList are flexible. Given that your #columns is fixes it seems natural to have array per row.
Option #1 is dangerous because it incurs the implicit requirement that all ArrayLists will be of the same length. You may accidentally neglect to add to any one of them thereby creating a bug. You will not have this problem in option #2.
Finally, it seems that the common convetion is that you first index the rows and only then the columns.
就我个人而言,我会选择固定长度数组的 ArrayList。如果您正在谈论大量的行,这可能比分配一堆 ArrayList 更节省空间(并且可能更快),ArrayList 由长度为 10 的数组开始。因此,如果列数少于 10你最终会浪费空间。另一方面,如果您有更多列,那么当您添加其他列时,ArrayList 将必须调整其支持数组的大小。
编辑:实际上,您可以在 ArrayList 的构造函数中设置容量,所以我想这可能没有太大区别:
http://java.sun.com/ j2se/1.5.0/docs/api/java/util/ArrayList.html
Personally, I'd go for an ArrayList of fixed-length arrays. If you're talking about a large quantity of rows, this may be more space efficient (and perhaps faster) than allocating a bunch of ArrayLists, which starts out backed by an array of length 10. Thus, if you have fewer columns than 10 you'll end up with wasted space. On the other hand if you have more, then the ArrayList will have to resize its backing array when you add additional columns.
Edit: actually, you can set the capacity in ArrayList's constructor, so I guess it may not make much difference:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html