944,037 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5150
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 3rd, 2005
0

using boolean expression on an array

Expand Post »
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:

Java Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JavaFish is offline Offline
19 posts
since Jul 2005
Aug 3rd, 2005
0

Re: using boolean expression on an array

That can be done a lot easier.
Java Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Aug 3rd, 2005
0

Re: using boolean expression on an array

Quote 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:

Java Syntax (Toggle Plain Text)
  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:

Java Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
adi1600 is offline Offline
3 posts
since Aug 2005
Aug 3rd, 2005
0

Re: using boolean expression on an array

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JavaFish is offline Offline
19 posts
since Jul 2005
Aug 3rd, 2005
0

Re: using boolean expression on an array

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:

Java Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JavaFish is offline Offline
19 posts
since Jul 2005
Aug 3rd, 2005
0

Re: using boolean expression on an array

Yes, of course, I did not tested, you should try the following randomNumber method:

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
adi1600 is offline Offline
3 posts
since Aug 2005
Aug 3rd, 2005
0

Re: using boolean expression on an array

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:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JavaFish is offline Offline
19 posts
since Jul 2005
Aug 3rd, 2005
0

Re: using boolean expression on an array

Your solution is wrong anyway, this should work (I even tested before posting ):

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
adi1600 is offline Offline
3 posts
since Aug 2005
Aug 3rd, 2005
0

Re: using boolean expression on an array

Quote 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
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Aug 3rd, 2005
0

Re: using boolean expression on an array

thanks very much adi1600 - have used your code and it definitely looks better than mine(well it works for starters!) :cheesy:

Appreciate your help.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JavaFish is offline Offline
19 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Objects
Next Thread in Java Forum Timeline: Communication Problems





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC