来自2个列表的唯一排列对(两个列表的可能组合)

时间:2015-05-06 14:23:56

标签: java algorithm probability

我一直在研究Java中的大量排列,但是我没有接近我的具体要求。 看,我有两个列表如下,列表1和列表2.这些列表1和列表2大小可以更改(但我这里给出例如list1携带大小7和列表2作为大小3)

  //introducing two lists 
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = new ArrayList<String>();

//adding data two 1 list
        list1.add("1");
        list1.add("2");
        list1.add("3");
        list1.add("4");
        list1.add("5");
        list1.add("6");
        list1.add("7");

//adding data two 2 list        

        list2.add("a");
    list2.add("b");
    list2.add("c");

预期输出如下组合,可以添加到列表或任何集合对象

    1:a,2:b,3:c,4:a,5:b,6:c,7:a 
    1:b,2:c,3:a,4:b,5:c,6:a,7:b
    1:c,2:a,3:b,4:c,5:a,6:b,7:c
    so on so....

但是这些值(行)不应该重复。最后应涵盖所有可能的组合。我尝试了很少的循环逻辑,但它没有帮助。

GUYS,
你的帮助很感激,但我知道这个组合: 1:一
1:乙 1:C 2: 2:乙 2:C 三: 3:乙 3:C 4: 4:乙 4:C 5:一 5:乙 5:C 6:一个 6:乙 6:C 7:一 7:乙 7:C

但我在这里遇到的问题是 1:A,2:B,3:C,4:A,5:B,6:C,7:一个
1:B,2:C,3:A,4:B,5:C,6:A,7:乙 1:C,2:A,3:B,4:C,5:A,6:B,7:C 等等......

这就是我想从这些列表中得到的。有什么帮助吗?

3 个答案:

答案 0 :(得分:1)

您的困惑似乎是您同时在两个列表上循环。如果将循环设置为外循环和内循环,它应该更好用

伪代码:

foreach (e1: elementsOfList1) {
  foreach (e2: elementsOfList2) {
     System.out.println(e1 + ":" + e2);
  }
}

答案 1 :(得分:1)

      //introducing two lists 
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = new ArrayList<String>();

//adding data two 1 list
list1.add("1");
list1.add("2");
list1.add("3");
list1.add("4");
list1.add("5");
list1.add("6");
list1.add("7");

//adding data two 2 list        

list2.add("a");
list2.add("b");
list2.add("c");

for (String i : list1 ) {
      for (String p : list2) {
           System.out.print(i + ":" + p);
      }
}

答案 2 :(得分:1)

经过精心编写的Iterable可以删除所有未来的排列问题:

// A tiny implemetation of a Pair.
public class Pair<P, Q> {

    // Exposing p & q directly for simplicity. They are final so this is safe.
    public final P p;
    public final Q q;

    public Pair(P p, Q q) {
        this.p = p;
        this.q = q;
    }

}

class Permute<P, Q> implements Iterable<Pair<P, Q>> {

    final Iterable<P> ps;
    final Iterable<Q> qs;

    public Permute(Iterable<P> ps, Iterable<Q> qs) {
        this.ps = ps;
        this.qs = qs;
    }

    @Override
    public Iterator<Pair<P, Q>> iterator() {
        return new Iterator<Pair<P, Q>>() {
            // Iterates over the P list.
            Iterator<P> pIterator = ps.iterator();
            // The current p we are pairing with.
            P currentP = null;
            // The current Q iterator - rewinds back after each new P - goes null at end
            Iterator<Q> qIterator = qs.iterator();
            // The next pair to return.
            Pair<P, Q> next = null;

            @Override
            public boolean hasNext() {
                while (next == null && qIterator != null) {
                    // Make sure there's a current P.
                    if (currentP == null) {
                        currentP = pIterator.hasNext() ? pIterator.next() : null;
                    }
                    if (currentP != null) {
                        // Find the next Q to compare it with.
                        if (qIterator.hasNext()) {
                            // Make the new one.
                            next = new Pair<>(currentP, qIterator.next());
                        } else {
                            // Reset the q Itertor.
                            qIterator = qs.iterator();
                            currentP = null;
                        }
                    } else {
                        // Its all over.
                        qIterator = null;
                    }

                }
                return next != null;
            }

            @Override
            public Pair<P, Q> next() {
                if (hasNext()) {
                    Pair<P, Q> nxt = next;
                    next = null;
                    return nxt;
                }
                // No more!
                throw new NoSuchElementException();
            }

        };
    }

}

public void test() {
    List<String> s = Arrays.asList("1", "2", "3", "4", "5", "6", "7");
    List<String> t = Arrays.asList("a", "b", "c");
    for (Pair<String, String> perm : new Permute<>(s, t)) {
        System.out.println(perm.p + ":" + perm.q + ",");
    }
}

第二次尝试 - 让订单正确。

// A tiny implemetation of a Pair.
public static class Pair<P, Q> {

    // Exposing p & q directly for simplicity. They are final so this is safe.
    public final P p;
    public final Q q;

    public Pair(P p, Q q) {
        this.p = p;
        this.q = q;
    }

    public String toString() {
        return "<" + p + "," + q + ">";
    }

}

// My special pair to track permutations already seen.
private static class IntPair extends Pair<Integer, Integer> {

    public IntPair(int p, int q) {
        super(p, q);
    }

    @Override
    public boolean equals(Object it) {
        if (!(it instanceof IntPair)) {
            return false;
        }
        IntPair ip = (IntPair) it;
        return p.equals(ip.p) && q.equals(ip.q);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(p, q);
    }

}

class Permute<P, Q> implements Iterable<Pair<P, Q>> {

    final Iterable<P> ps;
    final Iterable<Q> qs;

    public Permute(Iterable<P> ps, Iterable<Q> qs) {
        this.ps = ps;
        this.qs = qs;
    }

    @Override
    public Iterator<Pair<P, Q>> iterator() {
        return new Iterator<Pair<P, Q>>() {
            // Iterates over the P list.
            Iterator<P> pi = ps.iterator();
            // Where we are in the p list.
            int pn = 0;
            // Iterates over the Q list.
            Iterator<Q> qi = qs.iterator();
            // Where we are in the Q list.
            int qn = 0;
            // The next pair to return.
            Pair<P, Q> next = null;
            // All the pairs we've seen.
            Set<IntPair> seen = new HashSet<>();

            private P nextP() {
                // Step the P list.
                if (!pi.hasNext()) {
                    // Restart p.
                    pi = ps.iterator();
                    pn = 0;
                }
                pn += 1;
                return pi.next();
            }

            private Q nextQ() {
                // Step the P list.
                if (!qi.hasNext()) {
                    // Restart p.
                    qi = qs.iterator();
                    qn = 0;
                }
                qn += 1;
                return qi.next();
            }

            @Override
            public boolean hasNext() {
                if (next == null) {
                    // Make it.
                    next = new Pair<>(nextP(), nextQ());
                    // Have we seen it before?
                    int pCycles = 0;
                    int qCycles = 0;
                    boolean finished = false;
                    IntPair key = new IntPair(pn, qn);
                    while (!finished && seen.contains(key)) {
                        // Add a stagger.
                        if (qi.hasNext()) {
                            next = new Pair<>(next.p, nextQ());
                            if (qn == 1) {
                                qCycles += 1;
                            }
                        } else {
                            next = new Pair<>(nextP(), next.q);
                            if (pn == 1) {
                                pCycles += 1;
                            }
                        }
                        // Finished if we've been around the houses twice.
                        finished = pCycles > 1 || qCycles > 1;
                        key = new IntPair(pn, qn);
                    }
                    if (finished || seen.contains(key)) {
                        next = null;
                    } else {
                        seen.add(key);
                    }
                }
                return next != null;
            }

            @Override
            public Pair<P, Q> next() {
                if (hasNext()) {
                    Pair<P, Q> nxt = next;
                    next = null;
                    return nxt;
                }
                // No more!
                throw new NoSuchElementException();
            }

        };
    }

}

public void test() {
    for (Pair<String, String> perm
            : new Permute<>(
                    Arrays.asList("1", "2", "3", "4", "5", "6", "7"),
                    Arrays.asList("a", "b", "c"))) {
        System.out.print(perm.p + ":" + perm.q + ",");
    }
    System.out.println();
    for (Pair<String, String> perm
            : new Permute<>(
                    Arrays.asList("1", "2", "3"),
                    Arrays.asList("a", "b", "c"))) {
        System.out.print(perm.p + ":" + perm.q + ",");
    }
    System.out.println();
}

打印

1:a,2:b,3:c,4:a,5:b,6:c,7:a,1:b,2:c,3:a,4:b,5:c,6:a,7:b,1:c,2:a,3:b,4:c,5:a,6:b,7:c,
1:a,2:b,3:c,1:b,2:c,3:a,1:c,2:a,3:b,