• Post Reply Bookmark Topic Watch Topic
  • New Topic

Selecting five objects from an array  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

i know i should rather be using an array list for what im doing here...i will do it with an array list after i get this right

here is my code:



how can i select five random objects from this array...is it done with a loop?
any ideas...

please dont give me code...just what i should be looking at...maybe a link to a page wih a clue

i understand random functions...i just cant work out how to gt it to select 5 from my array of 10

thanks
 
Frankie Law
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you have an array of with 10 horses in it.
If you want to pick 5 out of the 10 horses. 
Consider what the result will be like.

1/  Do you just want 5 out of 10 horses all different? Meaning, if one horse is pick, it will be eliminated from the array.
If this is what you want, here is how I would go about this question and write code that best describe the pseudo code.
The steps may be differ, I am sure you'll figure it out.



2/  If you just want 5 random pick regardless of duplication, you can just randomly pick them with some form of repetition.
If this is what you want, like the previous pseudo code.  Except no need to remove the horse from the pool.



Does that give you some hints?
 
Frankie Law
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing.  If you choose option one.  The horse pool will shrink after you eliminate the horse from the pool.  If you don't want any horses remove from the pool but still not get duplicate.  Then you'll have to do additional comparison to make sure no duplicate is pick and pick until you get a different horse.

Hope this makes sense to you.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It makes sense to me It is much easier to achieve that sort of thing with a List<Horse> or a Set<Horse> than a Horse[].
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It makes sense to me It is much easier to achieve that sort of thing with a List<Horse> or a Set<Horse> than a Horse[].


Yes... if you use a List<Horse> then you just call Collections.shuffle(thatList) and take the first 5 entries of the shuffled list.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you really had to use a Horse[], you don't need another array to hold the ones you picked out. You just have to move the picked horses to the end of the list, then narrow down the range from which you choose the next horse.

The pseudo code:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!