Skip to content
Advertisement

How to have one array to Store a String and another to cout how many times that String was used?

I’m making an app for an ice cream shop where I need at some point to know how many ice cream from a specific flavor were sold. I have to write the flavor of the ice cream when I add it to the stock, so the ArrayList that contains the flavor is initially empty. Then I need another ArrayList which is going to store the number of times that flavor is sold. I need to do it with two ArrayLists because I don’t understand HashMaps yet.

JavaScript

This is how I create the ice cream.

JavaScript

This is how I sell it. I know I have to work somewhere in these two parts of the program, just can’t figure out how.

Advertisement

Answer

I suggest you use array-indexing to store the relationship between the arrays.

JavaScript

Whenever you add the icecream to the stock, check if the flavour of the ice-cream already exist in the array, if not add the flavour of the ice-cream into the flavourList at the end of the list.

JavaScript

Now if the flavour is already present in the array, find the index and go to the same index in the soldList array and increment the current value by 1.

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement