1

I would like to convert the following multidimensional array

[[ChestPain, (Chest), Heartburn], [Day2NumberofRefluxes, 2, 1, 5], [SIDay2, 15.4, 3.8, 71.4], [SAPDay2, 70.2, 0.0, 99.9]]

into a HashMap as follows

mapReflDayOneandTwo{Day2NumberofRefluxesChestPain=2, Day2NumberofRefluxes(Chest)=1, Day2NumberofRefluxesHeartburn=5, SIDay2ChestPain=15.4, SIDay2(Chest)=3.8, SIDay2Heartburn=71.4, SAPDay2ChestPain=70.2, SAPDay2(Chest)=0.0, SAPDay2Heartburn=99.9}

I am trying to do this with the following code:

for (int ff=1;ff<Symptoms.size();ff++){
         for (int fx=0;fx<Symptoms.get(0).size();fx++){
             for (int fxr=1;fxr<Symptoms.get(fx).size();fxr++){
             mapReflDayOneandTwo.put(Symptoms.get(ff).get(0)+Symptoms.get(0).get(fx),Symptoms.get(ff).get(fxr));
                                                              } 
                                                       }
                                       }

However when I do this the values seem to be incorrect and I get the following results with the values associated with the wrong keys. What am I doing wrong in the loop? (and shifting the ff to start at 0 or fxr to start at 0 doesn't work either)

mapReflDayOneandTwo{Day2NumberofRefluxesChestPain=1, Day2NumberofRefluxes(Chest)=5, Day2NumberofRefluxesHeartburn=5, SIDay2ChestPain=3.8, SIDay2(Chest)=71.4, SIDay2Heartburn=71.4, SAPDay2ChestPain=0.0, SAPDay2(Chest)=99.9, SAPDay2Heartburn=99.9}

1 Answer 1

1

Problem is, you have 3 loops so you will combine everything with everything (but you will see only the final result, since in map, old combos are overwritten). What you do is, for example, combine (Chest) with Day2NumberofRefluxes 3 times, but only one result will stay in the map, the wrong one:

  1. values of for variables will be 1, 1, 1, so you place value Day2NumberofRefluxes(Chest)=2 in the map
  2. values of for variables will be 1, 1, 2, so you place value Day2NumberofRefluxes(Chest)=1 in the map
  3. values of for variables will be 1, 1, 3, so you place value Day2NumberofRefluxes(Chest)=5 in the map

So you are left with Day2NumberofRefluxes(Chest)=5 in the map.

You need only 2 for loops, first to go through first list, and second one to go through other lists in the matrix, so when you combine (Chest) with Day2NumberofRefluxes, in that moment you know coordinates of their combo value (i,j + 1), and you place that value in the map:

   for (int i = 0; i < Symptoms.get(0).size(); i++) {
        for (int j = 1; j < Symptoms.size(); j ++) {
            mapReflDayOneandTwo.put(Symptoms.get(j).get(0) + Symptoms.get(0).get(i), 
                 Symptoms.get(j).get(i + 1));
        }
   }

Now you get:

{Day2NumberofRefluxes(Chest)=1, Day2NumberofRefluxesChestPain=2, Day2NumberofRefluxesHearthburn=5, SAPDay2(Chest)=0.0, SAPDay2ChestPain=70.2, SAPDay2Hearthburn=99.9, SIDay2(Chest)=3.8, SIDay2ChestPain=15.4, SIDay2Hearthburn=71.4}
Sign up to request clarification or add additional context in comments.

2 Comments

If I shift them back to 0 I get the following output: HeartburnHeartburn Heartburn, HeartburnHeartburn (Chest), HeartburnHeartburn ChestPain, Heartburn(Chest) Heartburn, Heartburn(Chest) (Chest), Heartburn(Chest) ChestPain,
OK. Perfect. Thanks so much

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.