1

I want to achieve the following vector initialisation in C++ using ArrayList in Java. I need to use ArrayList as I might want to change the size later on. I DO NOT want to use arrays.

vector<vector<int>> arr(M,vector<int>(N,X));

I want to do this using ArrayList in Java hopefully in a single line as above.

  • I suppose the array elements initialization is the core of you question, and therefore this question shouldn't have been closed. I've added an answer here, with code example for complex and primitive types (Java sadly still handles them differently): https://stackoverflow.com/a/56714650/265954 Hope this helps. – t0r0X Jun 22 '19 at 10:49

1 Answers1

0

Java isn't c++.

You simply can't initialize a two dimensional ArrayList with known dimensions and populated with 0 for example.

You can declare a list like:

List<List<Integer>> matrix = new ArrayList<>():

But as said: that is the closest you get with collection classes.

You can use Arrays.asList() to write down literal statements, but again, that is clumsy and not what c++ has to offer.

GhostCat
  • 137,827
  • 25
  • 176
  • 248