Here you can find the source of randomizeIndex(char[] array)
Parameter | Description |
---|---|
array | the array to randomize. |
private static char[] randomizeIndex(char[] array)
//package com.java2s; /*/*from w w w .jav a2 s . co m*/ This file is part of Cuber. Cuber is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Cuber is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Cuber. If not, see <http://www.gnu.org/licenses/>. */ import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Queue; public class Main { /** * @param array * the array to randomize. * @return an array that is a result of mixing the index values of the first array. * @author wonka * @since 23/04/2013 */ private static char[] randomizeIndex(char[] array) { char[] randomArray = new char[array.length]; List<Integer> set = new ArrayList<>(); while (set.size() < array.length) { int index = (int) (Math.random() * array.length); if (!set.contains(index)) { set.add(index); } } Queue<Integer> queue = new LinkedList<>(set); int i = 0; while (!queue.isEmpty()) { randomArray[i++] = array[queue.poll()]; } return randomArray; } }