using boolean expression on an array

Reply

Join Date: Jul 2005
Posts: 19
Reputation: JavaFish is an unknown quantity at this point 
Solved Threads: 0
JavaFish JavaFish is offline Offline
Newbie Poster

using boolean expression on an array

 
0
  #1
Aug 3rd, 2005
Hi

I have created an array of random numbers but I need to stop duplicate numbers being added to the array so I have added the boolean 'fresh'. however, I somehow need to tell my 'fresh' to only check the number of elements added to the array so far and I am embarrassed to say i can't figure this out as I am sure it is easy.

I have been searching books and the internet but just cannot figure it out. :rolleyes:

Any help in the right direction would be really appreciated

my code is:

  1. int amount = 7; // number of integers needed to generate
  2. int[] nums = new int[amount];
  3. int i;
  4.  
  5. // method for searching the array for duplicate numbers
  6. boolean fresh (int [] nums, int val){
  7. for (int i = 0; i < amount; i++){
  8. if (nums[i] == val) return true;
  9. }
  10. {
  11. return false;
  12. }
  13. }// end fresh method
  14.  
  15. //method to generate random numbers to insert into the tree
  16. public void randomNumber()
  17. {
  18. do
  19. {
  20. for (int i=0; i<amount; i++) {
  21. nums[i] = (int) Math.floor(Math.random() * 100);
  22. nums[i] = nums[i] -50;
  23. System.out.println("random is " + nums[i]);
  24. }
  25. }
  26. while(!(fresh(nums,i)));
  27. }// end of randomNumber method
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: using boolean expression on an array

 
0
  #2
Aug 3rd, 2005
That can be done a lot easier.
  1. List<Integer> l = new ArrayList<Integer>();
  2. for (int i=0;i<amount;i++) {
  3. int q = (int) Math.floor(Math.random() * 100);
  4. if (!l.contains(q)) {
  5. l.add(q);
  6. }
  7. }
  8. Integer[] dummy = l.toArray(new Integer[] {});

You'll end up with an array of Integer instead of int but with autounboxing that doesn't matter.

Most books won't mention tricks like this, and older books won't even mention the language features used here because they didn't exist until a few months ago.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 3
Reputation: adi1600 is an unknown quantity at this point 
Solved Threads: 0
adi1600 adi1600 is offline Offline
Newbie Poster

Re: using boolean expression on an array

 
0
  #3
Aug 3rd, 2005
Originally Posted by JavaFish
Hi

I have created an array of random numbers but I need to stop duplicate numbers being added to the array so I have added the boolean 'fresh'. however, I somehow need to tell my 'fresh' to only check the number of elements added to the array so far and I am embarrassed to say i can't figure this out as I am sure it is easy.

I have been searching books and the internet but just cannot figure it out. :rolleyes:

Any help in the right direction would be really appreciated

my code is:

  1. int amount = 7; // number of integers needed to generate
  2. int[] nums = new int[amount];
  3. int i;
  4.  
  5. // method for searching the array for duplicate numbers
  6. boolean fresh (int [] nums, int val){
  7. for (int i = 0; i < amount; i++){
  8. if (nums[i] == val) return true;
  9. }
  10. {
  11. return false;
  12. }
  13. }// end fresh method
  14.  
  15. //method to generate random numbers to insert into the tree
  16. public void randomNumber()
  17. {
  18. do
  19. {
  20. for (int i=0; i<amount; i++) {
  21. nums[i] = (int) Math.floor(Math.random() * 100);
  22. nums[i] = nums[i] -50;
  23. System.out.println("random is " + nums[i]);
  24. }
  25. }
  26. while(!(fresh(nums,i)));
  27. }// end of randomNumber method
First of all, you send to the fresh method the index 'i' and not the value nums[i]. A solution I think:

  1. int amount = 7; // number of integers needed to generate
  2. int[] nums = new int[amount];
  3. int i;
  4.  
  5. // method for searching the array for duplicate numbers
  6. boolean fresh (int [] nums, int val, int index){
  7. for (int i = 0; i < index; i++){
  8. if (nums[i] == val) return true;
  9. }
  10. {
  11. return false;
  12. }
  13. }// end fresh method
  14.  
  15. //method to generate random numbers to insert into the tree
  16. public void randomNumber()
  17. {
  18. do
  19. {
  20. for (int i=0; i<amount; i++) {
  21. nums[i] = (int) Math.floor(Math.random() * 100);
  22. nums[i] = nums[i] -50;
  23. System.out.println("random is " + nums[i]);
  24. }
  25. }
  26. while(!(fresh(nums, nums[i], i)));
  27. }// end of randomNumber method
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 19
Reputation: JavaFish is an unknown quantity at this point 
Solved Threads: 0
JavaFish JavaFish is offline Offline
Newbie Poster

Re: using boolean expression on an array

 
0
  #4
Aug 3rd, 2005
Hi adi1600

I tried your suggestion, but the loop doesn't stop! I take it that's because I haven't told it what index is, so it doesn't know when to stop looping?

Any further suggestions on this greatly appreciated
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 19
Reputation: JavaFish is an unknown quantity at this point 
Solved Threads: 0
JavaFish JavaFish is offline Offline
Newbie Poster

Re: using boolean expression on an array

 
0
  #5
Aug 3rd, 2005
Hi Jwenting

thanks for the reply. I tried your code but couldn't get it to compile as i wasn't sure if you meant just replace my two methods with this new code or not?

So I tried:

  1. public void randomNumber()
  2. {
  3. List<Integer> l = new ArrayList<Integer>();
  4. for (int i=0;i<amount;i++) {
  5. int q = (int) Math.floor(Math.random() * 100);
  6. if (!l.contains(q)) {
  7. l.add(q);
  8. }
  9. }
  10. Integer[] dummy = l.toArray(new Integer[] {});
  11. }// end of randomNumber method

but it says several errors, one of which is arraylist cannot be resolved to type.

Any ideas?

Thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 3
Reputation: adi1600 is an unknown quantity at this point 
Solved Threads: 0
adi1600 adi1600 is offline Offline
Newbie Poster

Re: using boolean expression on an array

 
0
  #6
Aug 3rd, 2005
Yes, of course, I did not tested, you should try the following randomNumber method:

  1. public void randomNumber() {
  2. int i;
  3. do {
  4. for (i = 0; i < amount; i++) {
  5. nums[i] = (int) Math.floor(Math.random() * 100);
  6. nums[i] = nums[i] - 50;
  7. System.out.println("random is " + nums[i]);
  8. }
  9. } while (i < amount && !(fresh(nums, nums[i], i)));
  10. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 19
Reputation: JavaFish is an unknown quantity at this point 
Solved Threads: 0
JavaFish JavaFish is offline Offline
Newbie Poster

Re: using boolean expression on an array

 
0
  #7
Aug 3rd, 2005
Hi

Thanks for keep trying with this. Just crashed that and it also crashed out.

Any further help would be appreciate - thank you for your time :mrgreen:
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 3
Reputation: adi1600 is an unknown quantity at this point 
Solved Threads: 0
adi1600 adi1600 is offline Offline
Newbie Poster

Re: using boolean expression on an array

 
0
  #8
Aug 3rd, 2005
Your solution is wrong anyway, this should work (I even tested before posting ):

  1. package ro.csc.jccg;
  2.  
  3. /**
  4.  * Created by IntelliJ IDEA.
  5.  * User: Adrian Cioaca
  6.  * Date: Aug 3, 2005
  7.  * Time: 2:21:19 PM
  8.  */
  9. public class Test {
  10.  
  11. int amount = 99; // number of integers needed to generate
  12. int[] nums = new int[amount];
  13. int i;
  14.  
  15. // method for searching the array for duplicate numbers
  16. boolean fresh(int[] nums, int val, int index) {
  17.  
  18. for (int i = 0; i < index; i++) {
  19. if (nums[i] == val) return false;
  20. }
  21.  
  22. return true;
  23.  
  24. }// end fresh method
  25.  
  26. //method to generate random numbers to insert into the tree
  27. public void randomNumber() {
  28. int i;
  29. //do {
  30. for (i = 0; i < amount; i++) {
  31. int aux = (int) Math.floor(Math.random() * 100);
  32. aux = aux - 50;
  33. if (fresh(nums, aux, i)) {
  34. System.out.println("random is " + aux);
  35. nums[i] = aux;
  36. } else {
  37. i--;
  38. //break;
  39. }
  40. }
  41. //} while (i < amount && !(fresh(nums, nums[i], i)));
  42. }// end of randomNumber method
  43.  
  44. public static void main(String[] args) {
  45.  
  46. Test test = new Test();
  47.  
  48. test.randomNumber();
  49. }
  50. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: using boolean expression on an array

 
0
  #9
Aug 3rd, 2005
Originally Posted by JavaFish
Hi Jwenting

thanks for the reply. I tried your code but couldn't get it to compile as i wasn't sure if you meant just replace my two methods with this new code or not?

but it says several errors, one of which is arraylist cannot be resolved to type.

Any ideas?

Thanks
Yes, that replaces all the code you use to create the array.
I didn't bother to post the import statements you need, leaving that up to your imagination.
Code only works with the latest compiler also. While you can get it working with older compilers, it takes some work and isn't as easy to use.
Of course you will need to do something with the array once you've created it also
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 19
Reputation: JavaFish is an unknown quantity at this point 
Solved Threads: 0
JavaFish JavaFish is offline Offline
Newbie Poster

Re: using boolean expression on an array

 
0
  #10
Aug 3rd, 2005
thanks very much adi1600 - have used your code and it definitely looks better than mine(well it works for starters!) :cheesy:

Appreciate your help.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC