0

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?

5
  • Please follow below link you will get an idea stackoverflow.com/questions/37078635/… Commented Nov 5, 2018 at 18:08
  • I have seen this, but this did not really help Commented Nov 5, 2018 at 18:13
  • So far you have only the row and column "headers" (at leasts that's what it looks like in your demonstration). What should be the contents? Commented Nov 5, 2018 at 18:20
  • In the end, I want to compare Fruit1 to Vegetable1, Fruit1 to Vegetable2 and so on... I should have 12 comparisons Commented Nov 5, 2018 at 18:24
  • I just don't know how to put this into matrix format to later write code that compares each variable in one arraylist to the other equaling 12 comparisons Commented Nov 5, 2018 at 18:25

1 Answer 1

0

Assuming you want the following matrix:

Vegetable1 | Fruit1, Fruit2, Fruit3
Vegetable2 | Fruit1, Fruit2, Fruit3
Vegetable3 | Fruit1, Fruit2, Fruit3
Vegetable4 | Fruit1, Fruit2, Fruit3

You can use the following code to use ArrayLists to do all the comparisons:

List<String> vegetables = new ArrayList<>(); // Fill the lists somehow
List<String> fruits = new ArrayList<>();

for(String vegetable : vegetables) {
    for(String fruit : fruits) {
        System.out.printf("Compare %s to %s%n", vegetable, fruit);
    }
}

If that's all you want, you don't need nested lists. If you want to really have a matrix then you'd require a slight modification of the code:

List<String> vegetables = new ArrayList<>(); // Fill the lists somehow
List<String> fruits = new ArrayList<>();
List<List<String>> matrix = new ArrayList<>();    

for(String vegetable : vegetables) {
    List<String> row = new ArrayList<String>();
    row.add(vegetable);
    for(String fruit : fruits) {
        row.add(fruit);
    }
    matrix.add(row);
}

That would create rows with items VegetableN, Fruit1, Fruit2, Fruit3 where N is the number of the vegetable row.

Sign up to request clarification or add additional context in comments.

2 Comments

Wow! The first code was exactly what I needed. I am very new to programming so I am unfamiliar with some things. My main goal is to compare strings of different groups (fruits & vegetables as an example) for textual similarity. I believe I can move forward now
@TomWilson If my answer helped you, you can mark it as accepted using the green check mark under the voting buttons so that it disappears from the unanswered section.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.