【问题标题】:How to shuffle pairs如何洗牌对
【发布时间】:2010-04-13 04:42:42
【问题描述】:

如何打乱对中的元素? 下面的程序,生成所有可能的对,然后打乱这些对。 例如洗牌前可能的配对是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;
    }
}

【问题讨论】:

    标签: java list arraylist generator shuffle


    【解决方案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] 范围内的整数,所以你的对看起来像这样:&lt;0,3&gt;&lt;1,2&gt; 等。你可能想要的是得到一对是两个字母的字符串,例如:@987654330 @等。在这个版本中,我将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;
         }
      }
    

    【讨论】:

      【解决方案2】:

      假设你有这些对象:

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-28
        • 2011-10-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多