Простой алгоритм:
- создать массив, содержащий числа от 0 до 10
- перетасовать
- перебрать этот массив и получить соответствующий индекс в исходной коллекции
В Java :
public static void main(String[] args) throws Exception {
List<Integer> random = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Collections.shuffle(random);
List<String> someList = Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k");
for (int i : random) {
System.out.print(someList.get(i));
}
}
выходы:
ihfejkcadbg
EDIT
Теперь, когда я перечитываю его, вы можете также просто перетасовать первоначальный сбор и цикл:
public static void main(String[] args) throws Exception {
List<String> someList = Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j");
//make a copy if you want the initial collection intact
List<String> random = new ArrayList<> (someList);
Collections.shuffle(random);
for (String s : random) {
System.out.print(s);
}
}
Только C# и Java? –