如何洗牌对

发布于 2024-08-28 11:05:14 字数 1144 浏览 7 评论 0原文

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

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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魔法唧唧 2024-09-04 11:05:14

您只需在将 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;
     }
  }

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;
     }
  }
桃扇骨 2024-09-04 11:05:14

假设您有这些对象:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文