That can be done a lot easier.
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.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
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 :)
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337