An effective way to shuffle JSON arrays in Java?

Is this the best way? Now, I convert my jsonarray to an ArrayList of a custom class, using collections Shuffle () to perform the operation and convert back to jsonarray, which seems to be too much overhead

The answer may just be to implement a fisher Yates shuffle, but my guess is that this may have been completed, so I want to avoid reinventing the wheel I looked at the standard JSON API and Google's gson, but they don't seem to have any implementation

The standard array in this question also has simple options that can be easily ported to Java, but I'd love to hear your input I'm surprised http://www.google.com/search?q=java +Shuffle + jsonarray didn't fill me with methods

Solution

I'm sorry to release the answers to my own questions, but now, because there is no quick solution out of the box, I'm implementing my own static shuffle function: random shuffling of an array Still looking forward to hearing the best implementation This is what I do:

public static JSONArray shuffleJsonArray (JSONArray array) throws JSONException {
    // Implementing Fisher–Yates shuffle
        Random rnd = new Random();
        for (int i = array.length() - 1; i >= 0; i--)
        {
          int j = rnd.nextInt(i + 1);
          // Simple swap
          Object object = array.get(j);
          array.put(j,array.get(i));
          array.put(i,object);
        }
    return array;
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>