• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array

 
S. Stacey
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
I have the following code the generate an array of 40 random numbers between 1 & 10:

Now I am looking to determine how many times each number appears....Would someone be able to assit me on that?
Thanks for your help
Stacey
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65126
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried so far that is giving you problems?
bear
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
S. Stacey
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
S. Stacey
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help guys!!!
I got it working with the following code:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic