aspose file tools*
The moose likes Beginning Java and the fly likes Shuffling array of ints Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Shuffling array of ints" Watch "Shuffling array of ints" New topic
Author

Shuffling array of ints

russell lloyd
Greenhorn

Joined: Feb 09, 2005
Posts: 18
I am writing a method for a CS assignment that is supposed to take in an array of ints and return the array shuffled. I read elsewhere on these boards where someone explained how to do this but with strings. I tried to follow their code making the appropriate changes for ints but I am having a problem.




the code works fine as long as original doesn't contain 0's but if it does then it results in an infinite loop I was wondering how I could maybe modify the code so that it could avoid this
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
here's a slightly different approach

Kenneth Albertson
Ranch Hand

Joined: Sep 18, 2005
Posts: 59
Originally posted by russell lloyd:
The code works fine as long as original doesn't contain 0's but if it does then it results in an infinite loop I was wondering how I could maybe modify the code so that it could avoid this


The general problem is that you are using the output array elements to hold two separate pieces of information: a not-set-yet flag, and the new value. To do this, the not-set-flag value has to be a value that won't occur in the input array.

If that isn't possible, the algorithm can't work. (You need an extra data structure for the not-set-yet flags.) If it is possible, you need to pre-initialize the output array with the not-set-yet value.
[ September 20, 2005: Message edited by: Kym Thompson ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Shuffling array of ints