0

如何洗牌对中的元素?下面的程序,生成所有可能的对,然后打乱这些对。例如 shuffle 之前可能的对ab,ac,ae,af..etc 改组为ac,ae,af,ab...etc

如何使它不仅成对洗牌,而且在成对本身的元素内洗牌?例如,而不是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;
    }
}
4

2 回答 2

1

您只需在将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>"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;
     }
  }
于 2010-04-13T04:56:47.870 回答
0

假设您有这些对象:

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
于 2010-04-13T04:57:27.883 回答