簡體   English   中英

來自2個列表的唯一排列對(兩個列表的可能組合)

[英]Unique permutation pairs from 2 lists (probable combination of two lists)

我一直在研究Java中的許多排列方式,但沒有得到滿足我特定要求的任何東西。 看到,我有以下兩個列表,分別是list1和list2。這些list1和list 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....

但是這些值(行)不應重復。 最后,應涵蓋所有可能的組合。 我嘗試了很少的for循環邏輯,但沒有幫助。

各位,
感謝您的幫助,但我知道這種結合:1:a
1:b 1:c 2:a 2:b 2:c 3:a 3:b 3:c 4:a 4:b 4:c 5:a 5:b 5:c 6:a 6:b 6: c 7:a 7:b 7: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依此類推...

這就是我從這些列表中想要的。 有什么幫助嗎?

您的困惑似乎是您同時遍歷兩個列表。 如果將循環設置為外部循環和內部循環,則效果會更好

偽代碼:

foreach (e1: elementsOfList1) {
  foreach (e2: elementsOfList2) {
     System.out.println(e1 + ":" + e2);
  }
}
      //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);
      }
}

精心編寫的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,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM