How to create matrix from java vector of vectors (

2019-08-31 12:13发布

问题:

am trying to create vector of Vectors(2d) in java like multidimensional array. then to assign values to specific position in that matrix like what we do using matrix[i][j] in 2D array Any help.

My data are in one Vector Vector n = [a, b, c, d, e, f, g , h]

so i want to create 2D Vector to represent vector m =

  • a b c d
  • e f g h

回答1:

You can create a 2D Vector using the following:

Vector<Vector<Integer>> vector2D = new Vector<Vector<Integer>>(10);

This will create a Vector of size 10 which will contain Vectors with Integer(Vector) values. Before setting a value at a particular index, you need to create a Vector of Integer and set at the row position(2 in your case).

vector2D.add(2, new Vector<Integer>(10));

You can then set a value at the column index using the following syntax:

Vector<Integer> rowVector = vector2D.get(2);
rowVector.add(3, 5);

Here we first get the Vector of Integers (Vector) at index 2. Then add the value at the index 3 in the Vector of Integers(Vector).

Hope this explains.



标签: java vector