It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array" Watch "Array" New topic
Author

Array

S. Stacey
Greenhorn

Joined: Jul 03, 2003
Posts: 20
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

Joined: Jan 10, 2002
Posts: 60050
    
  65

What have you tried so far that is giving you problems?
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
S. Stacey
Greenhorn

Joined: Jul 03, 2003
Posts: 20
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

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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

Joined: Jul 03, 2003
Posts: 20
Thanks for your help guys!!!
I got it working with the following code:
 
 
subject: Array
 
Similar Threads
Random number problem
set return type as array of integer
Arrays
how to genrate a random number inside the range
Selection Sort?