views:

93

answers:

2

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;
    }
}
+1  A: 

You just have to shuffle the temp list before you add it to pic2. Here's the fixed code (note that I turned the pic2 variable into a field of the ListGenerator class and renamed it to 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;
     }
  }

However this is just the first step towards a solution. Currently, each pair will contain integers in the range [0..size-1] so your pairs look like this: <0,3>, <1,2>, etc. What you probably want is to get a pairs that are two-letter String such as: "ab", "dc", etc. In this version I renamed getList() to getPairs() which convey its meaning better. Also, I made the constructor of ListGenerator accept an array of characters so you just need to call it with your desired characters, as follows:

  List<String> pairs = new ListGenerator('a', 'b', 'c', 'd', 'e', 'f', 'g').getPairs();

And here is ListGenerator it self:

  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;
     }
  }
Itay
Thank you Italy :-)
Jessy
A: 

Let's say you have these objects:

Red dress
Blue shirt
Pink panties

And you want to shuffle both the colors and the articles of clothings to get things like:

Pink shirt
Blue panties
... etc

How do you do it?

It's simple, really: just shuffle the list of colors and articles of clothings separately, and then join them again.

Red, Blue, Pink            -->  Pink, Blue, Red
dress, shirt, panties      -->  shirt, panties, dress
                               ------------------------ pair
                                Pink shirt
                                Blue panties
                                Red dress
polygenelubricants