쌍의 원소를 섞는 방법? 아래의 프로그램은 모든 가능한 쌍을 생성하고 나중에 쌍을 뒤섞습니다. 예 : 셔플 전 가능한 페어는 ab,ac,ae,af
입니다. ac,ae,af,ab
... 등으로 셔플했습니다.쌍을 뒤섞는 방법
페어 자체의 요소가 아니라 쌍 자체의 요소로 만드는 방법은 무엇입니까? 예 : ab, ac,
대신 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;
}
}
이탈리아 :-) – Jessy