• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Junilu Lacar
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst
  • Peter Rooke
  • Mikalai Zaikin
Bartenders:
  • Himai Minh

How would you shuffle and array in J2ME ?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
I want to create a method for shuffling array in J2ME. Can you suggest some ways?
Thank you
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure. You mean like randomize the order of the array?

To make random numbers you need:
java.util.Random
java.lang.Math
% (Modulo operator)


Create a Random object, which is your random number generator, and call its nextInt() method.

The Math.abs() method keeps your random numbers in the positive range.

The '%' (Modulo) will help you constrain the range of random numbers, such as to the length of an array.


Example:



You need the array length for random number generation. You need a way to copy values to a new temporary array so you don't mess with the original.

Also, Somehow keep track of which ones you've already moved so your application doesn't copy two or more values from the array or you loose some.

If you have an object array you can skip all the double checking. Just copy your array into the method argument, then into the Vector using its copyInto(Object[] anArray) method, then keep calling, size() (for the Modulo), and elementAt(int index) (to move this object into the temp. array), and removeElementAt(int index) to lower the size of the Vector.

Keep doing randoms to the Vector until its empty, then you know your Temp array is ready to replace the original.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Random#nextInt(int n) will return a number in the range 0..n (inclusive, exclusive) obviating the need for Math.abs and modulo.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can avoid the need for a temporary array. If you look at the documentation for Collections.shuffle(), it describes a simple algorithm for randomly shuffling in situ.

(Of course, if java.util.Collections is available in J2ME, and if you use a List instead of an array, you can just call the method direct and let it do the work for you).
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, the Collections API isn't part of Java ME.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks - I guessed it probably wasn't. But the approach that method describes is a good one.
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic