在 Java 中使用什么集合而不是二维数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2050490/
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 19:08:06  来源:igfitidea点击:

What collection to use instead of 2D array in Java?

javacollections

提问by Yatendra Goel

I want to use a collection in place of 2D array so that I don't need to give its size at the time of declaration and I can add as many elements as I want dynamically.

我想用一个集合代替 2D 数组,这样我就不需要在声明时给出它的大小,而且我可以动态添加任意数量的元素。

采纳答案by helios

The problem with List> is you have to redimension each row if you want to redimension your matrix.

List> 的问题是,如果您想重新调整矩阵的维度,则必须重新调整每一行的维度。

If you want to use a sparse matrix, or maybe an infinite matrix you can do something like:

如果您想使用稀疏矩阵,或者可能是无限矩阵,您可以执行以下操作:

class SparseMatrix<X> {
  private Map<Coord, X> values = new HashMap<Coord, X>();

  public SparseMatrix() {
  }

  public X get(int x, int y) {
     return values.put(new Coord(x,y)); // null if there's no value
  }

  public void set(int x, int y, X value) { // you can use null (like in a List)
     values.set(new Coord(x,y), value);
  }

  private static class Coord {
    int x; int y;
    public Coord(int x, int y) {
       this.x = x;
       this.y = y;
    }

    @Override
    public boolean equals(Object other) {
       if (other instance of Coord) {
          Coord o = (Coord) other;
          return o.x == x && o.y == y;
       }
       return false;
    }

    @Override
    public int hashCode() {
       return o.x + o.y; // or some more clever implementation :)
    }

  }
}

Edit:Apache Commons HashCodeBuilderis a great tool for generating hash-codes.

编辑:Apache Commons HashCodeBuilder是一个很好的生成哈希码的工具。

回答by Powerlord

The easiest way is to use nested collections... say (assuming your values are Strings) List<List<String>>which can then be used like this:

最简单的方法是使用嵌套集合......说(假设你的值是字符串)List<List<String>>然后可以像这样使用:

List<List<String>> fakeArray = new ArrayList<List<String>>();

// Pretend you fill it with values between these calls
String retrieve = fakeArray.get(0).get(0);

Edit: This was originally a Map<String,List<String>>which really doesn't make sense in this context.

编辑:这最初是 aMap<String,List<String>>这在这种情况下确实没有意义。

However, you may want to see if Google Collectionsor Apache Commons Collectionshave something more specialized that you can use.

但是,您可能想看看Google CollectionsApache Commons Collections是否有您可以使用的更专业的东西。

回答by Rasmus Kaj

What do you want to be able to do with it? I would probably simply use a Collection<Collection<Element>>(where Collection might be replaced by List).

你想用它做什么?我可能会简单地使用一个Collection<Collection<Element>>(其中 Collection 可能被 List 替换)。

Or you might create your own class with metods to iterate over rows or columns or all elements as needed.

或者,您可以使用方法创建自己的类,以根据需要迭代行或列或所有元素。

回答by Eli Acherkan

It depends on the way you want to use the data structure. Your options are:

这取决于您想要使用数据结构的方式。您的选择是:

  • Two lists; it's your job to synchronize between them.
  • A map; instead of a key-value relationship, your map entries will simply be tuples of objects.
  • A list of 2-cell object arrays; each item in the list will be an object array of size 2.
  • 两个清单;在它们之间进行同步是你的工作。
  • 一张地图; 您的映射条目将只是对象元组,而不是键值关系。
  • 2-cell 对象数组的列表;列表中的每一项都是大小为 2 的对象数组。

EDIT:I completely misread the question; I thought it was about a 2D array of width 2.

编辑:我完全误读了这个问题;我认为这是关于宽度为 2 的二维数组。

Having properly read the question (I hope :-)), I agree with those who said list-of-lists.

正确阅读了问题(我希望 :-))后,我同意那些说列表列表的人。

回答by Alex Ntousias

It depends on what you're trying to do, but I would recommend ArrayList. It's faster than Vector. Unless you care about synchronization! If you want it as a 2-dimensional list, then you create an ArrayList and each element of this list would be another ArrayList.

这取决于您要尝试做什么,但我建议使用 ArrayList。它比 Vector 更快。除非你关心同步!如果你想把它作为一个二维列表,那么你创建一个 ArrayList,这个列表的每个元素都是另一个 ArrayList。

回答by rsp

You could do a trial with an ArrayListhaving ArrayLists as items. If that doesn't do what you want, it will give instight in what you need to built yourself.

您可以尝试ArrayListArrayLists 作为项目。如果这不能满足您的需求,它将深入了解您需要构建自己的内容。

回答by moritz

I'm personally using the Vectorclass for that purpose, though different requirements may eventually dictate the use of other, more specialized classes.

我个人为此目的使用Vector类,尽管不同的要求可能最终决定使用其他更专业的类。

回答by Ben Hanzl

回答by sap

Import java.util.ArrayList;

进口 java.util.ArrayList;

ArrayListis what you want, you don't need to set up its size at creation time and you can add elements dynamically using the addmethod.

ArrayList就是你想要的,你不需要在创建时设置它的大小,你可以使用该add方法动态添加元素。