| | |
using boolean expression on an array
![]() |
•
•
Join Date: Jul 2005
Posts: 19
Reputation:
Solved Threads: 0
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:
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)
int amount = 7; // number of integers needed to generate int[] nums = new int[amount]; int i; // method for searching the array for duplicate numbers boolean fresh (int [] nums, int val){ for (int i = 0; i < amount; i++){ if (nums[i] == val) return true; } { return false; } }// end fresh method //method to generate random numbers to insert into the tree public void randomNumber() { do { for (int i=0; i<amount; i++) { nums[i] = (int) Math.floor(Math.random() * 100); nums[i] = nums[i] -50; System.out.println("random is " + nums[i]); } } while(!(fresh(nums,i))); }// end of randomNumber method
That can be done a lot easier.
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.
Java Syntax (Toggle Plain Text)
List<Integer> l = new ArrayList<Integer>(); for (int i=0;i<amount;i++) { int q = (int) Math.floor(Math.random() * 100); if (!l.contains(q)) { l.add(q); } } 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.
•
•
Join Date: Aug 2005
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
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)
int amount = 7; // number of integers needed to generate int[] nums = new int[amount]; int i; // method for searching the array for duplicate numbers boolean fresh (int [] nums, int val){ for (int i = 0; i < amount; i++){ if (nums[i] == val) return true; } { return false; } }// end fresh method //method to generate random numbers to insert into the tree public void randomNumber() { do { for (int i=0; i<amount; i++) { nums[i] = (int) Math.floor(Math.random() * 100); nums[i] = nums[i] -50; System.out.println("random is " + nums[i]); } } while(!(fresh(nums,i))); }// end of randomNumber method
Java Syntax (Toggle Plain Text)
int amount = 7; // number of integers needed to generate int[] nums = new int[amount]; int i; // method for searching the array for duplicate numbers boolean fresh (int [] nums, int val, int index){ for (int i = 0; i < index; i++){ if (nums[i] == val) return true; } { return false; } }// end fresh method //method to generate random numbers to insert into the tree public void randomNumber() { do { for (int i=0; i<amount; i++) { nums[i] = (int) Math.floor(Math.random() * 100); nums[i] = nums[i] -50; System.out.println("random is " + nums[i]); } } while(!(fresh(nums, nums[i], i))); }// end of randomNumber method
•
•
Join Date: Jul 2005
Posts: 19
Reputation:
Solved Threads: 0
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:
but it says several errors, one of which is arraylist cannot be resolved to type.
Any ideas?
Thanks
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)
public void randomNumber() { List<Integer> l = new ArrayList<Integer>(); for (int i=0;i<amount;i++) { int q = (int) Math.floor(Math.random() * 100); if (!l.contains(q)) { l.add(q); } } Integer[] dummy = l.toArray(new Integer[] {}); }// end of randomNumber method
but it says several errors, one of which is arraylist cannot be resolved to type.
Any ideas?
Thanks
•
•
Join Date: Aug 2005
Posts: 3
Reputation:
Solved Threads: 0
Yes, of course, I did not tested, you should try the following randomNumber method:
Java Syntax (Toggle Plain Text)
public void randomNumber() { int i; do { for (i = 0; i < amount; i++) { nums[i] = (int) Math.floor(Math.random() * 100); nums[i] = nums[i] - 50; System.out.println("random is " + nums[i]); } } while (i < amount && !(fresh(nums, nums[i], i))); }
•
•
Join Date: Aug 2005
Posts: 3
Reputation:
Solved Threads: 0
Your solution is wrong anyway, this should work (I even tested before posting
):
): Java Syntax (Toggle Plain Text)
package ro.csc.jccg; /** * Created by IntelliJ IDEA. * User: Adrian Cioaca * Date: Aug 3, 2005 * Time: 2:21:19 PM */ public class Test { int amount = 99; // number of integers needed to generate int[] nums = new int[amount]; int i; // method for searching the array for duplicate numbers boolean fresh(int[] nums, int val, int index) { for (int i = 0; i < index; i++) { if (nums[i] == val) return false; } return true; }// end fresh method //method to generate random numbers to insert into the tree public void randomNumber() { int i; //do { for (i = 0; i < amount; i++) { int aux = (int) Math.floor(Math.random() * 100); aux = aux - 50; if (fresh(nums, aux, i)) { System.out.println("random is " + aux); nums[i] = aux; } else { i--; //break; } } //} while (i < amount && !(fresh(nums, nums[i], i))); }// end of randomNumber method public static void main(String[] args) { Test test = new Test(); test.randomNumber(); } }
•
•
•
•
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
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.
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: Objects
- Next Thread: Communication Problems
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle physics plazmic print problem program programming project recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree trolltech unlimited utility webservices windows






