1

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 in one Vector:

n = [a, b, c, d, e, f, g , h]

I want to create 2D Vector to represent vector m =

  • a b c d
  • e f g h
beastlyCoder
  • 2,349
  • 4
  • 25
  • 52
Ally Bitebo
  • 37
  • 2
  • 3
  • 10
  • What type are you trying to store in the Vector? If you're looking for the syntax of a multi-dimensional array, it's `int[][] array = new int[5][6];` – EpicPandaForce Jul 04 '14 at 14:48
  • i know how to work with multi-dimentional array. What i need is to g´creat matrix above using 2D vector and how to assign values in sppecific index. – Ally Bitebo Jul 04 '14 at 14:54
  • You assign new values using `array[2][3] = 6;` for example. Please note that arrays in Java are indexed from zero (so in case of an [5][6] array, the valid indices are [0..4][0..5]) – EpicPandaForce Jul 04 '14 at 14:54
  • i know in array i want to implement in vector. – Ally Bitebo Jul 04 '14 at 15:07
  • oh you mean THAT kind of Vector. The lack of capital letters on the class name really confused me! In that case, http://stackoverflow.com/questions/24526019/how-to-make-arraylist-that-work-as-two-dimentional-array-in-java/24526104#24526104 – EpicPandaForce Jul 04 '14 at 18:48
  • Yes i meant Vector...sorry for the confusion. And your suggestion link is ArrayList but my problem is to use Vector of Vectors or in other words 2D vectors please help. – Ally Bitebo Jul 04 '14 at 19:02
  • Vectors work pretty much exactly the same way as ArrayLists, so eh. – EpicPandaForce Jul 04 '14 at 19:23

2 Answers2

2

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.

Priyesh
  • 2,041
  • 1
  • 17
  • 25
  • thank you for this solution but still i need some explanation if you can show me conversion of this matrix[2][3]= 5 which means assigning 5 at the row index 2 and column index 3 or some example tutorial i tried to check online different sites i didnt manage to find good solution please! @priyesh – Ally Bitebo Jul 04 '14 at 15:40
  • can i get your contacts please so as we can chat and you can help me out of this problem. @ Priyesh – Ally Bitebo Jul 04 '14 at 18:20
  • @AllyBitebo, I have created a chat here http://chat.stackoverflow.com/rooms/56951/matrix-creation in case you want to discuss this. – Priyesh Jul 08 '14 at 13:07
  • thank you for your help i managed to do it. sorry for the late reply – Ally Bitebo Jul 16 '14 at 10:12
  • Can you please accept the answer if it helped? Or add your implementation to the question, so that it can be useful for others. – Priyesh Jul 16 '14 at 10:19
1

For whatever reasons, SO bumped your question just a couple minutes ago. In 2021 you probably would not use Vector any more, but List. Actually both of them have subList(start,end) method (even in Java 7 what the link points to), so practically you would just loop over vector/list in row-sized steps and use this method.
Also, you might fancy using streams so instead of initializing the result variable in a separate line, the stream collector does that for you:

List<Integer> vector=Arrays.asList(1,2,3,4,5,6,7,8);
int rowSize=4;
List<List<Integer>> matrix=IntStream.range(0, vector.size()/rowSize)
        .mapToObj(row->vector.subList(row*rowSize, (row+1)*rowSize))
        .collect(Collectors.toList());
System.out.println(matrix);

will output [[1, 2, 3, 4], [5, 6, 7, 8]].

Also, as Vector can be constructed from List, after all you can do that too:

Vector<Integer> vector=new Vector<Integer>(Arrays.asList(1,2,3,4,5,6,7,8));
int rowSize=4;
Vector<Vector<Integer>> matrix=new Vector<Vector<Integer>>(
        IntStream.range(0, vector.size()/rowSize)
        .mapToObj(row->new Vector<Integer>(vector.subList(row*rowSize, (row+1)*rowSize)))
        .collect(Collectors.toList()));
System.out.println(matrix);

And of course in the background both of them do pretty much the same as a for loop would have done:

Vector<Integer> vector=new Vector<Integer>(Arrays.asList(1,2,3,4,5,6,7,8));
int rowSize=4;
Vector<Vector<Integer>> matrix=new Vector<Vector<Integer>>();
for(int row=0;row<vector.size()/rowSize;row++)
    matrix.add(new Vector<Integer>(vector.subList(row*rowSize, (row+1)*rowSize)));
System.out.println(matrix);
tevemadar
  • 12,389
  • 3
  • 21
  • 49