简体   繁体   English

如何创建二维集合?

[英]How do you create a two dimensional collection?

有没有简单的方法来创建二维集合?

A two-dimensional collection is essentially having lists within a list. 二维集合实质上是在列表中包含列表。 For example, to create a 2D ArrayList of strings, you would do something like this: 例如,要创建字符串的二维数组列表,您可以执行以下操作:

ArrayList<ArrayList<String>> stringList = new ArrayList<ArrayList<String>>();

To add a new row, you would simply add a new ArrayList: 要添加新行,只需添加一个新的ArrayList即可:

stringList.add(new ArrayList<String>());

And here's how to add an element to the first row: 这是向第一行添加元素的方法:

stringList.get(0).add("example string");

A 2d collection is a bit abstract... what do you mean? 2D集合有点抽象...您是什么意思? A double entry array is a 2d collection. 双输入数组是2d集合。

Why don't you use Multimaps from Guava library? 为什么不使用Guava库中的Multimaps? https://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained , Those collections ads everything that the jdk is missing for dealing with 2d collections. https://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained ,这些集合将jdk处理2D集合时缺少的所有内容都广告化。

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

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