<legend id='civMv'><style id='civMv'><dir id='civMv'><q id='civMv'></q></dir></style></legend>
<i id='civMv'><tr id='civMv'><dt id='civMv'><q id='civMv'><span id='civMv'><b id='civMv'><form id='civMv'><ins id='civMv'></ins><ul id='civMv'></ul><sub id='civMv'></sub></form><legend id='civMv'></legend><bdo id='civMv'><pre id='civMv'><center id='civMv'></center></pre></bdo></b><th id='civMv'></th></span></q></dt></tr></i><div id='civMv'><tfoot id='civMv'></tfoot><dl id='civMv'><fieldset id='civMv'></fieldset></dl></div>

        <bdo id='civMv'></bdo><ul id='civMv'></ul>

      <tfoot id='civMv'></tfoot>
    1. <small id='civMv'></small><noframes id='civMv'>

      任意数量集合的笛卡尔积

      Cartesian product of an arbitrary number of sets(任意数量集合的笛卡尔积)
    2. <small id='vFuRB'></small><noframes id='vFuRB'>

    3. <tfoot id='vFuRB'></tfoot>

        • <bdo id='vFuRB'></bdo><ul id='vFuRB'></ul>
            <tbody id='vFuRB'></tbody>
          <i id='vFuRB'><tr id='vFuRB'><dt id='vFuRB'><q id='vFuRB'><span id='vFuRB'><b id='vFuRB'><form id='vFuRB'><ins id='vFuRB'></ins><ul id='vFuRB'></ul><sub id='vFuRB'></sub></form><legend id='vFuRB'></legend><bdo id='vFuRB'><pre id='vFuRB'><center id='vFuRB'></center></pre></bdo></b><th id='vFuRB'></th></span></q></dt></tr></i><div id='vFuRB'><tfoot id='vFuRB'></tfoot><dl id='vFuRB'><fieldset id='vFuRB'></fieldset></dl></div>

            <legend id='vFuRB'><style id='vFuRB'><dir id='vFuRB'><q id='vFuRB'></q></dir></style></legend>

              1. 本文介绍了任意数量集合的笛卡尔积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                您知道一些简洁的 Java 库,它们可以让您制作两个(或更多)集合的笛卡尔积吗?

                Do you know some neat Java libaries that allow you to make cartesian product of two (or more) sets?

                例如:我有三套.一个是 Person 类的对象,第二个是 Gift 类的对象,第三个是 GiftExtension 类的对象.

                For example: I have three sets. One with objects of class Person, second with objects of class Gift and third with objects of class GiftExtension.

                我想生成一组包含所有可能的三元组 Person-Gift-GiftExtension.

                I want to generate one set containing all possible triples Person-Gift-GiftExtension.

                集合的数量可能会有所不同,因此我无法在嵌套的 foreach 循环中执行此操作.在某些情况下,我的应用程序需要制作 Person-Gift pair 的乘积,有时它是三重 Person-Gift-GiftExtension,有时甚至可能会设置 Person-Gift-GiftExtension-GiftSecondExtension-GiftThirdExtension 等.

                The number of sets might vary so I cannot do this in nested foreach loop. Under some conditions my application needs to make a product of Person-Gift pair, sometimes it is triple Person-Gift-GiftExtension, sometimes there might even be sets Person-Gift-GiftExtension-GiftSecondExtension-GiftThirdExtension, etc.

                推荐答案

                删除了以前的两套解决方案.有关详细信息,请参阅编辑历史记录.

                Previous solutions for two sets removed. See edit history for details.

                这是一种对任意数量的集合递归执行的方法:

                Here is a way to do it recursively for an arbitrary number of sets:

                public static Set<Set<Object>> cartesianProduct(Set<?>... sets) {
                    if (sets.length < 2)
                        throw new IllegalArgumentException(
                                "Can't have a product of fewer than two sets (got " +
                                sets.length + ")");
                
                    return _cartesianProduct(0, sets);
                }
                
                private static Set<Set<Object>> _cartesianProduct(int index, Set<?>... sets) {
                    Set<Set<Object>> ret = new HashSet<Set<Object>>();
                    if (index == sets.length) {
                        ret.add(new HashSet<Object>());
                    } else {
                        for (Object obj : sets[index]) {
                            for (Set<Object> set : _cartesianProduct(index+1, sets)) {
                                set.add(obj);
                                ret.add(set);
                            }
                        }
                    }
                    return ret;
                }
                

                请注意,不可能在返回的集合中保留任何泛型类型信息.如果您事先知道要获取多少个集合的乘积,则可以定义一个通用元组来保存这么多元素(例如 Triple),但是有在 Java 中无法拥有任意数量的泛型参数.

                Note that it is impossible to keep any generic type information with the returned sets. If you knew in advance how many sets you wanted to take the product of, you could define a generic tuple to hold that many elements (for instance Triple<A, B, C>), but there is no way to have an arbitrary number of generic parameters in Java.

                这篇关于任意数量集合的笛卡尔积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Compiling C++ for the JVM(为 JVM 编译 C++)
                Compile to java bytecode (without using Java)(编译成java字节码(不使用Java))
                How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000 at compile time?(如何在编译时驱动 C#、C++ 或 Java 编译器计算 1+2+3+...+1000?)
                Java ClassLoader: load same class twice(Java ClassLoader:两次加载相同的类)
                How to debug .class files in ECLIPSE?(如何在 ECLIPSE 中调试 .class 文件?)
                Java quot;The blank final field may not have been initializedquot; Anonymous Interface vs Lambda Expression(Java“可能尚未初始化空白的最终字段匿名接口与 Lambda 表达式)

                <legend id='Dn10f'><style id='Dn10f'><dir id='Dn10f'><q id='Dn10f'></q></dir></style></legend>
              2. <small id='Dn10f'></small><noframes id='Dn10f'>

                <i id='Dn10f'><tr id='Dn10f'><dt id='Dn10f'><q id='Dn10f'><span id='Dn10f'><b id='Dn10f'><form id='Dn10f'><ins id='Dn10f'></ins><ul id='Dn10f'></ul><sub id='Dn10f'></sub></form><legend id='Dn10f'></legend><bdo id='Dn10f'><pre id='Dn10f'><center id='Dn10f'></center></pre></bdo></b><th id='Dn10f'></th></span></q></dt></tr></i><div id='Dn10f'><tfoot id='Dn10f'></tfoot><dl id='Dn10f'><fieldset id='Dn10f'></fieldset></dl></div>
                  <bdo id='Dn10f'></bdo><ul id='Dn10f'></ul>

                        <tfoot id='Dn10f'></tfoot>

                            <tbody id='Dn10f'></tbody>