Welcome to Java Ranch, "S!" This is clearly a homework problem, but I'll bite, anyway. Here's a nice, straightforward algorithm: Use any implementation of the java.util.Map interface. Create Integer objects to be both the keys and the values. The keys would be the numbers in the array, the values would be the counts. To add a number to the map, you would fetch the key; if it's not there, add it with a value of 1. If it is there, get the value, add one, replace it. At the end, get an iterator over the Map keys, and you'll get a list of the numbers that appeared; you can then fetch the count for each from the Map. Hint: one particular choice of Map implementation would be better than any others, because it would assure that in this step, you'd fetch the keys in order.
Yes this is a homeowrk question but this is only a small part of an assigment that I am working on. I have come up with the following code: for (int i = 0; i< service.length; i++)
if (service[i] == 1) match ++; that will only count the number of ones in the array. The only thing that I can come up with is doing that code 10 times but that seems pretty silly to me, there must be an easier way. Ernest thanks for your help but I am so new to java that when you suggested we have not even touched on in school. We have not got to the java.until.Map yet so I am not even sure how to implement what you suggested. Stacey
Another possibility would be to create an int array of 10 buckets and simply add 1 to the bucket represented by the random number (don't forget to subtract one). So if you look in your service array and find 4 in the first bucket add one to the third bucket (4-1=3) of your counting array.
Re: my "Map" solution: Sorry, I didn't read your original mail closely enough -- didn't realize that the numbers were limited to be between one and ten. My solution is good if the integers can have any value, but if there are only ten different values, the Sheriff's proposed method is way better. Just use an array to hold ten counters, and as you find each number in the original array, increment the counter in the counter array.
Joined: Jul 03, 2003
Thanks for your help guys!!! I got it working with the following code: