-4

I want to know if I can create a 2D collection object just as a 2D array. Kindly advice.

Saif Tak
  • 11
  • 1
  • 3
  • 2
    You can have a list of lists or collection of collections E.g. `List>`. This way you can simulate a multi-dimensional collection. – VPK Dec 21 '17 at 04:23
  • @VPK, 2D array guarantees same size for all of it's elements. The list of lists does not and is same as jagged array. – tsolakp Dec 21 '17 at 04:29
  • Trivial, but if you just need a 2D array of objects, you can of course use `SomeObject[][]`. – Tim Biegeleisen Dec 21 '17 at 04:34
  • Google's Table is another approach. Just look for Guava, it has a collection of very nice, well collections and data structures. – Jaco Van Niekerk Dec 21 '17 at 04:36
  • @tsolakp, there's no doubt about `2D array guarantees same size for all of it's elements.`, but that can be the limitation of arrays and a good point for using the collection instead (depending upon specific req), isn't it? – VPK Dec 21 '17 at 04:56
  • @VPK. It depends. One can assume that arraylist of arraylists is two dimensional and just use first element`s size when iterating over all elements. – tsolakp Dec 21 '17 at 05:11
  • @tsolakp, yup that's true. – VPK Dec 21 '17 at 05:13

2 Answers2

1

Yes, you can. Example:

ArrayList<ArrayList<Object>> list = new ArrayList<ArrayList<Object>>();
Cardinal System
  • 2,749
  • 3
  • 21
  • 42
-1

Your question does not illustrate your intention clearly. But if you need a tuple, you should implement it or use external libraries. For example this is an implementation of Pair (Tuple with two elements):

A Java collection of value pairs? (tuples?)

IT man
  • 563
  • 6
  • 9