Java Random String randomize(String[] orderedList)

Here you can find the source of randomize(String[] orderedList)

Description

Returns the input Strings in random order.

License

Open Source License

Declaration


public static String[] randomize(String[] orderedList) 

Method Source Code

//package com.java2s;
/*/*from w w  w .  ja  v a 2  s. c om*/
 * Copyright: (c) 2002-2006 Mayo Foundation for Medical Education and
 * Research (MFMER).  All rights reserved.  MAYO, MAYO CLINIC, and the
 * triple-shield Mayo logo are trademarks and service marks of MFMER.
 *
 * Except as contained in the copyright notice above, the trade names, 
 * trademarks, service marks, or product names of the copyright holder shall
 * not be used in advertising, promotion or otherwise in connection with
 * this Software without prior written authorization of the copyright holder.
 * 
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 * 
 *       http://www.eclipse.org/legal/epl-v10.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.*;

public class Main {
    /** Returns the input Strings in random order.
    */

    public static String[] randomize(String[] orderedList) {
        ArrayList list = stringArrayToArrayList(orderedList);
        Random random = new Random(System.currentTimeMillis());
        ArrayList randomList = new ArrayList(orderedList.length);
        System.out.println("list.size():" + list.size());
        int index;
        while (list.size() > 0) {
            index = random.nextInt(list.size());
            randomList.add(list.get(index));
            list.remove(index);
        }
        System.out.println("randomList.size():" + randomList.size());
        return (String[]) (randomList.toArray(new String[randomList.size()]));
    }

    /** Put in a string array and get back a ArrayList*/

    //public static List asList(Object[] a)

    public static ArrayList stringArrayToArrayList(String[] array) {
        if (array == null)
            return new ArrayList(0);

        ArrayList arrayList = new ArrayList(array.length);
        for (int i = 0; i < array.length; i++) {
            arrayList.add(array[i]);
        }
        return arrayList;
    }

    public static String[] toArray(ArrayList arrayList) {
        return arrayListToStringArray(arrayList);
    }

    /**
     * This method is here because my first implementation used the brute force approach.
     * Then I realized that you could accomplish this in one line of code - and it is
     * more efficient because it uses System.arraycopy().
     */
    public static String[] arrayListToStringArray(ArrayList arrayList) {
        if (arrayList == null)
            return new String[0];
        return (String[]) (arrayList.toArray(new String[arrayList.size()]));
    }
}

Related

  1. getString(int n, int arg[])
  2. randomCharacter(String availableValues)
  3. randomFileString()
  4. randomFixedByteLengthUnicodeString(Random r, int length)
  5. randomFixedLengthUnicodeString(Random random, char[] chars, int offset, int length)
  6. randomizeMap(Map map)
  7. randomlyRecaseCodePoints(Random random, String str)
  8. randomNumberString(int in)
  9. randomRealisticUnicodeString(Random r)