简体   繁体   中英

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:

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? A double entry array is a 2d collection.

Why don't you use Multimaps from Guava library? https://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained , Those collections ads everything that the jdk is missing for dealing with 2d collections.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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