ペアをシャッフルする方法
質問
ペア内の要素をシャッフルするにはどうすればよいですか?以下のプログラムは、考えられるすべてのペアを生成し、後でペアをシャッフルします。例えばシャッフル前の可能なペアは ab,ac,ae,af
..etc にシャッフル 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;
}
}
解決
シャッフルするだけです temp
リストに追加する前に pic2
. 。これが修正されたコードです(私が pic2
変数をフィールドに代入する ListGenerator
クラスに名前を変更しました result
)
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);
public class ListGenerator {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
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);
Collections.shuffle(temp);
result.add(temp);
}
}
Collections.shuffle(result);
}
//This method return the shuffled list
public ArrayList<ArrayList<Integer>> getList() {
return result;
}
}
ただし、これは解決への第一歩にすぎません。現在、各ペアには次の範囲の整数が含まれます。 [0..size-1]
したがって、ペアは次のようになります。 <0,3>
, <1,2>
, 、など。おそらく必要なのは、次のような 2 文字の文字列のペアを取得することです。 "ab", "dc"
, 、など。このバージョンでは名前を変更しました getList()
に getPairs()
その意味がより良く伝わります。また、コンストラクターも作りました ListGenerator
文字の配列を受け入れるため、次のように目的の文字を指定して呼び出すだけで済みます。
List<String> pairs = new ListGenerator('a', 'b', 'c', 'd', 'e', 'f', 'g').getPairs();
そして、こちらが ListGenerator
それ自体:
public class ListGenerator {
ArrayList<String> result = new ArrayList<String>();
public ListGenerator(char... letters) {
int size = letters.length;
// create a list of all possible combinations
for(int i = 0 ; i < size ; i++) {
for(int j = (i+1) ; j < size ; j++) {
ArrayList<Character> temp = new ArrayList<Character>();
temp.add(letters[i]);
temp.add(letters[j]);
Collections.shuffle(temp);
result.add("" + temp[0] + temp[1]);
}
}
Collections.shuffle(result);
}
//This method return the shuffled list
public ArrayList<ArrayList<Integer>> getPairs() {
return result;
}
}
他のヒント
これらのオブジェクトがあるとします。
Red dress
Blue shirt
Pink panties
そして、衣服の色と品物の両方をシャッフルして、次のようなものを取得したいとします。
Pink shirt
Blue panties
... etc
どうやってやるのですか?
実にシンプルです。服の色と記事のリストを別々にシャッフルし、再度結合するだけです。
Red, Blue, Pink --> Pink, Blue, Red
dress, shirt, panties --> shirt, panties, dress
------------------------ pair
Pink shirt
Blue panties
Red dress