java 数组的 ArrayList vs. ArrayLists 的数组 vs. 类似的东西

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2316632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:35:19  来源:igfitidea点击:

ArrayList of arrays vs. array of ArrayLists vs. something similar

javadata-structuresarraysarraylisttablemodel

提问by Joonas Pulakka

I'm creating a TableModelwhich 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,

我正在创建一个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);
}

i.e. creating an array of ArrayLists, each ArrayListrepresenting one column, or:

即创建一个ArrayLists数组,每个数组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];
}

i.e. creating one ArrayList that holds arrays, each of which represent one row.

即创建一个包含数组的 ArrayList,每个数组代表一行。

Any ideas which of these is more efficient in terms of speed or storage? Alternative 1 requires extending N ArrayLists with each added row, while alternative 2 requires extending just one ArrayListbut also creating a new array of length N (to represent the new row). Or is there an obvious, better solution?

任何想法在速度或存储方面更有效?备选方案 1 需要ArrayList对每个添加的行扩展 N s,而备选方案 2 只需要扩展一个,ArrayList但还需要创建一个长度为 N 的新数组(以表示新行)。或者是否有明显的更好的解决方案?

采纳答案by cletus

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:

所以你的结构是:

List<Object[]> rows;

where the array element is one row.

其中数组元素是一行。

There are several options for what your row object should be however:

但是,对于您的行对象应该是什么,有几个选项:

  1. An array;
  2. A Listor other Collection; or
  3. A custom object.
  1. 数组;
  2. 一个List或其他Collection;或者
  3. 自定义对象。

(3) can probably be done by using some kind of interface that allows you to query the number, type and name of columns.

(3) 大概可以通过使用某种接口来完成,该接口允许您查询列的数量、类型和名称。

回答by vpram86

How about using a single ArrayList itself and accessing element like this

如何使用单个 ArrayList 本身并像这样访问元素

public Object getValueAt(int row, int column) { 
    return data.get(row*NUMBER_OF_COLUMNS+column); 
} 

In this case, each ArrayList object is a cell in a table. And you need not require any other extra structure

在这种情况下,每个 ArrayList 对象都是表格中的一个单元格。而且您不需要任何其他额外的结构

回答by Itay Maman

I would go with option #2 for several reasons

出于多种原因,我会选择选项 #2

First, arrays have a fixes length whereas ArrayList are flexible. Given that your #columns is fixes it seems natural to have array per row.

首先,数组的长度是固定的,而 ArrayList 是灵活的。鉴于您的 #columns 已修复,每行都有数组似乎很自然。

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.

选项 #1 是危险的,因为它会导致所有 ArrayList 的长度相同的隐式要求。您可能会不小心忽略添加到其中任何一个,从而造成错误。在选项#2 中您不会遇到此问题。

Finally, it seems that the common convetion is that you first index the rows and only then the columns.

最后,似乎普遍的做法是首先索引行,然后才是列。

回答by Tom

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.

就个人而言,我会选择固定长度数组的 ArrayList。如果您正在谈论大量行,这可能比分配一堆 ArrayLists 更节省空间(并且可能更快),后者由长度为 10 的数组支持。因此,如果您的列数少于 10你最终会浪费空间。另一方面,如果您有更多列,那么当您添加其他列时,ArrayList 将不得不调整其支持数组的大小。

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

编辑:实际上,您可以在 ArrayList 的构造函数中设置容量,所以我想它可能没有太大区别:http: //java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html