Hello, I am familiar with how to add array data to 2D vector in C++. (I do realize Java's Vector is essentially replaced with ArrayList, etc....
I just want to clarify the "mechanics" of how to fill a vector of vectors)
An example of a C++ program is:
CODE BEGIN
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// declare arrays to load into 2D vector
int typeArray[4] = {55,66,77,88};
int valArray[13] = {1,2,3,4,5,6,7,8,9,10,10,10,11};
// declare int type vector of vectors
vector< vector <int> > myVector(4, vector<int> (14,0));
// assign one array per ROW
for (int i = 0; i < myVector.size(); i++) {
myVector[i][0] = typeArray[i];
for (int j = 1; j < myVector[i].size(); j++) {
myVector[i][j] = valArray[j - 1];
}
}
// print vector to screen with 4 ROWS, 13 COLUMNS
for (int k = 0; k < myVector.size(); k++) {
for (int l = 0; l < myVector[k].size(); l++) {
cout << myVector[k][l] << ' ';
}
cout << '\n';
}
cout << " yes" << endl; // generic addon to indicate end of program on console screen
system("Pause"); // to view output on console screen
return 0;
} // end main
~~~~~~~~~~~~~~~~~~~~~~~~~~
OUTPUT
55 1 2 3 4 5 6 7 8 9 10 10 10 11
66 1 2 3 4 5 6 7 8 9 10 10 10 11
77 1 2 3 4 5 6 7 8 9 10 10 10 11
88 1 2 3 4 5 6 7 8 9 10 10 10 11
yes
Press any key to continue . . .
~~~~~~~~~~~~~~~~~~~~~~~~~~~
A similar starting example in Java would be:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*****My first question is how do I add row and column definitions within the vector declaration as in Cplusplus?*****
Cplusplus example defined:
//
4 is the length or number of rows;
14 is the width of number of columns;
'0' is the value all cells are initialized to
vector< vector <int> > myVector(
4, vector<int> (
14,0));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I've tried working with the code in the link below and am not able to output rows and columns.
http://www.java2s.com/Tutorial/Java/0140__Collections/MultidimensionalVectorsVectorsofVectors.htm
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The output is only:
A
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Thanks in advance for the feedback.