Java - Get Random Object

Associate
Joined
8 Mar 2007
Posts
88
Hi there,

I have a class which i'll call MyNumbers which basically lets you create an object which is a number with an odd even flag.

Now i have another class which needs to create a range of these numbers and then choose a random odd or even number from the list and remove it.

I originally thought i could store these in an ArrayList but am unsure how i would go about selecting a random odd or even.

Should i be using some other structure to store it?

Any help would be greatly appreciated.

Jon
 
Hm nah it's not really what i'm looking for.

Let's say you have in the arraylist:

1 - odd
2 - Even
3 - Odd
4 - Even

If the program required an odd it could randomly select from 1 and 3 and then remove said element.

I was thinking because i'd made a MyNumber class i could search the objects based on one of the variables.
 
Thanks for the suggestions.

Thanks Una, i thought about that but if i removed an element the array could be left:

13456 which is odd odd even odd even:

So that wouldn't work.

The 2 arraylists is a good idea. Either select random from odd or select random from even.

Thanks again.
 
Back
Top Bottom