Pregunta

Cómo mezclar los elementos en los pares? El programa siguiente, generar todos los pares posibles y luego barajar los pares. p.ej. posibles pares antes shuffle es ab,ac,ae,af..etc barajan a ac,ae,af,ab etc ...

Como hacer que no sólo arrastrando los pies en parejas, pero dentro de los elementos de la misma pareja? p.ej. en lugar de ab, ac, cómo puedo hacer 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;
    }
}
¿Fue útil?

Solución

Sólo tiene que barajar la lista temp antes de añadirlo a pic2. Aquí está el código fijo (nota que di vuelta a la variable de pic2 en un campo de la clase ListGenerator y le cambió el nombre a 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;
     }
  }

Sin embargo, esto es sólo el primer paso hacia una solución. Actualmente, cada par contendrá enteros en el rango [0..size-1] por lo que sus parejas se ven así: <0,3>, <1,2>, etc. Lo que probablemente quiere es conseguir un pares que son de dos letras de cadena, tales como: "ab", "dc", etc. En esta versión I getList() renombrado a getPairs() que transmiten mejor su significado. Además, hice el constructor de ListGenerator aceptar un arreglo de caracteres por lo que sólo necesita llamar con sus caracteres deseados, de la siguiente manera:

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

Y aquí es ListGenerator si mismo:

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

Otros consejos

ejemplo de permiten tener estos objetos:

Red dress
Blue shirt
Pink panties

Y desea barajar tanto los colores y los artículos de ropas para conseguir cosas como:

Pink shirt
Blue panties
... etc

¿Cómo lo haces?

Es muy sencillo, en realidad:. Simplemente barajar la lista de colores y artículos de ropas separado, y luego unirse a ellos de nuevo

Red, Blue, Pink            -->  Pink, Blue, Red
dress, shirt, panties      -->  shirt, panties, dress
                               ------------------------ pair
                                Pink shirt
                                Blue panties
                                Red dress
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top