Link to home
Start Free TrialLog in
Avatar of dearCode
dearCode

asked on

Iterating through a list

I'm working with a craps play. One of the things that I have to do is to store each game of craps in a List<Integer> and then store all the games in List<List<Integer>>. How can I iterate through the list of lists of integers "List<List<Integer>>" to find how many times each of the numbers between 2 and 12 is repeated?
Avatar of sameer_goyal
sameer_goyal
Flag of India image

In my view, there's no easy of doing it. You will have to loop through the first level of list and fetch the List<Integer> and then loop through that and check if you can locate the desired numbers. Once you locate, set flags and counters to maintain the checks.
Avatar of dearCode
dearCode

ASKER

Well I know that I could iterate through all the numbers using two for loops, but I'm confused about how to find how many of number 2 for example is in the List<List<Integer>> and display it and do the same with the other numbers "2-12". Help please!
What paltform or language are you using?
Java
Not sure, but do you have Dictionary Collection in Java?
What do you mean by dictionary collection?
You can use a generic collection like a Dictionary which allows Key-Value pair storage. In the inner loop, you can for each number like 2-12, make 2 as a key and the value can be a counter which you increase every time an occurence of the key is found. So at the end of the loop you will a colelction with Keys which are the numbers and subsequent count that were found in the List<List<Integer>>
i see that there is no Dictionary in Java. Sorry, I am a .NET developer primarily.
Instead, you have a 'Map' collection. You can use Map instead of Dictionary.
ASKER CERTIFIED SOLUTION
Avatar of sameer_goyal
sameer_goyal
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks! That was very helpful.