简体   繁体   English

可变长度数组的矩阵的组合

[英]Combination of a matrix of arrays of variable length

I have to obtain all possibile combination of this kind of matrix: 我必须获得这种矩阵的所有可能组合:

String[][] matrix = {
   {"AA-123", "AA-124", "AA-125", "AA-126"},
   {"BB-12", "BB-13"},
   {"CC-1"},
};

After all, that is the final implementation. 毕竟,那是最终的实现。 It's in Java, but the language may be irrilevant: 它使用Java语言,但语言可能不友好:

long nComb = 1;
for (int iMatr = 0; iMatr < matrix.length; iMatr++)
   nComb *= matrix[iMatr].length;

for (int iComb = 0; iComb < nComb; iComb++) {
   System.out.print("|");

   long nSec = 1;
   for (int iSec = 0; iSec < matrix.length; iSec++) {
       String[] sec = matrix[iSec];

       for (int iAtom = 0; iAtom < sec.length; iAtom++) {

           if (iAtom == ((iComb / nSec) % sec.length))
               System.out.print(1);
           else
               System.out.print(0);
       }

       nSec *= sec.length;
       System.out.print("|");
   }

   System.out.println();
}

I have to apply my logic on the if that it prints 1 or 0. I need to know what is the current element (index) of the combination of the array. 我要申请我的逻辑上if它打印1或0。我需要知道什么是阵列的组合的当前元素(指数)。 The expected result: 预期结果:

|1000|10|1|
|0100|10|1|
|0010|10|1|
|0001|10|1|
|1000|01|1|
|0100|01|1|
|0010|01|1|
|0001|01|1|

Regards. 问候。

Edit: 编辑:

I find a possible answer using another variable in the array iteration: nSec . 我在数组迭代中使用另一个变量nSec找到了可能的答案。 It product increse by the lenght of the array over iterations, reaching at the last iteration the value of nComb . 它在迭代过程中乘数组的长度增加乘积,在最后一次迭代时达到nComb的值。

I believe what you're after here is called the cartesianProduct of the multiple collections and this is already supported by a number of collection libraries in Java. 我相信您在这里追求的是多个集合的cartesianProduct,并且Java中的许多集合库已经支持了这一点。

Personally I'd recommend using Guava ( https://code.google.com/p/guava-libraries/ ) which will allow you to define your problem as follows (conversion between Array and Set I will leave out as an exercise :): 就个人而言,我建议您使用Guava( https://code.google.com/p/guava-libraries/ ),该方法可让您定义问题的方式如下(在Array和Set之间的转换将作为练习:) :

import com.google.common.collect.Sets;
import java.util.List;
import java.util.Set;

public class CartesianProduct {

    public static void main(String[] args) {
        Set<List<String>> merged = Sets.cartesianProduct(
                Sets.newHashSet("AA-123", "AA-124", "AA-125", "AA-126"),
                Sets.newHashSet("BB-12", "BB-13"),
                Sets.newHashSet("CC-1")
        );
        System.out.println("Size: " + merged.size());
        System.out.println("Content: " + merged);
    }

}

By executing this code you will get the following result: 通过执行此代码,您将得到以下结果:

Size: 8
Content: [
   [AA-125, BB-13, CC-1], 
   [AA-125, BB-12, CC-1], 
   [AA-124, BB-13, CC-1], 
   [AA-124, BB-12, CC-1], 
   [AA-123, BB-13, CC-1], 
   [AA-123, BB-12, CC-1], 
   [AA-126, BB-13, CC-1], 
   [AA-126, BB-12, CC-1]
]

Then further you can process, sort and format output the way you require (and when printing it another guava class Joiner might come handy). 然后,您可以进一步按照所需的方式处理,排序和格式化输出(在打印时,另一个番石榴类Joiner可能会派上用场)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM