How to shuffle pairs
Posted
by Jessy
on Stack Overflow
See other posts from Stack Overflow
or by Jessy
Published on 2010-04-13T04:42:42Z
Indexed on
2010/04/13
5:03 UTC
Read the original article
Hit count: 350
How to shuffle the elements in the pairs?
The program below, generate all possible pairs and later shuffle the pairs.
e.g. possible pairs before shuffle is ab,ac,ae,af
..etc shuffled to ac,ae,af,ab
...etc
How to make it not only shuffled in pairs but within the elements in the pair itself?
e.g. instead of ab, ac,
how can I make ba, ac
?
String[] pictureFile = {"a.jpg","b.jpg","c.jpg","d.jpg","e.jpg","f.jpg","g.jpg"};
List <String> pic1= Arrays.asList(pictureFile);
...
ListGenerator pic2= new ListGenerator(pic1);
ArrayList<ArrayList<Integer>> pic2= new ArrayList<ArrayList<Integer>>();
public class ListGenerator {
public ListGenerator(List<String> pic1) {
int size = pic1.size();
// create a list of all possible combinations
for(int i = 0 ; i < size ; i++) {
for(int j = (i+1) ; j < size ; j++) {
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.add(i);
temp.add(j);
pic2.add(temp);
}
}
Collections.shuffle(pic2);
}
//This method return the shuffled list
public ArrayList<ArrayList<Integer>> getList() {
return pic2;
}
}
© Stack Overflow or respective owner