I am reading contents from a text file and parsing them into separate ArrayLists.
For example, the text file reads:
Fruit1
Fruit2
Fruit3
Vegetable1
Vegetable2
Vegetable3
Vegetable4
Currently, I have a code that separates each group into its own array
fruits = [Fruit1, Fruit2, Fruit3]
vegetables = [Vegetable1, Vegetable2, Vegetable3, Vegetable4]
How do I make a matrix with n rows and m columns from these two existing ArrayLists?
My goal output is to generate a 3x4 matrix like so
| Fruit1, Fruit2, Fruit3
Vegetable1|
Vegetable2|
Vegetable3|
Vegetable4|
|
I have seen examples demonstrating initializing a matrix, however, if I update my text file to lets say a 3x20 matrix, or a 5x20 matrix, I want the code to run the same, which is where I am struggling.
Here is the code I've written for the matrix:
List<List<String>> matrix = new ArrayList<List<String>>();
matrix.add(fruits);
matrix.add(vegetables);
System.out.println(matrix);
However, this is the output, which just combines them
[Fruit1, Fruit2, Fruit3, Vegetable1, Vegetable2, Vegetable3, Vegetable4]
How do I create a matrix, making one ArrayList the rows, and the other ArrayList, the columns?