Reputation: 11
I am very new to Java. I have a 5*5 matrix in a file. When I am trying to import the elements of the matrix in a matrix variable and printing it, it prints in 25*1, not 5*5. How can I convert the individual column vector into the corresponding row vector?
It would be great if someone helps. Thanks
Upvotes: 1
Views: 1075
Reputation: 33941
Store your data for the matrix in a 2D array. If your matrix is 5x5 you'll need 5 an array of 5 arrays. If I were doing this, I'd write my own Matrix class to store and manipulate the matrix's contents, using 2d arrays internally.
Upvotes: 1
Reputation: 597362
You can have vector of vectors. But instead of Vector
use ArrayList
(it's the same but without an unnecessary synchronization).
List<List<?>> 2dVector = new ArrayList<List<?>>()
Then you can call 2dVector.get(0).get(1)
for example. You will just have to initialize each item with new ArrayList<?>
. Note that you can replace ?
with String
, Integer
or whatever your type is.
Upvotes: 1
Reputation: 3640
Sounds like homework,
// Assuming you have a Vector holding Elements
Vector<Element> vector = new Vector<Element>();
// Populate vector
// Print vector contents in 5 X 5
int i = 0;
for (Element e : vector) {
// If already printed 5 elements
if (i % 5 == 0) {
System.out.print("\n");
}
System.out.print(e.toString() + ' ');
i++;
}
This is if you're storing in a 1D collection. You can always use 2D array, or a self-containing List or Vector.
Upvotes: 1