获取对象数组的所有可能组合

Getting all possible combinations of an array of objects

我的一个应用程序的开发突然停止了。

我需要获取参数数组的所有可能组合,例如数组可能如下所示

[整数、布尔值、字符串]

可能的组合总数为 7(2^X - 1,其中 X 是参数的数量,这是我和朋友在尝试解决此问题时想出的公式)

这是可能组合的可视化。
[整数、布尔值、字符串],
[整数,布尔],
[整数, 字符串],
[整数],
[布尔值,字符串],
[布尔值] 和
[字符串]

正如您在可视化中看到的那样,唯一必要的是条目之间的相对顺序始终相同(整数必须始终位于布尔值和字符串之前,布尔值必须始终位于字符串之前)

我想问的是:
如何找到字符串数组的所有可能组合,其中组合不限于当前条目的任何特定长度,但仅限于相对于每个条目具有相同的顺序其他?

如果有人能在正确的方向上推动我,我将不胜感激。我一直在寻找一些 post 来寻找所有可能的值,但我找不到任何对我有任何帮助的 post。

如果需要有关该问题的任何进一步信息,请随时询问

让我给你提示:

检查十进制数的二进制表示:

0 000
1 001
2 010
3 011
4 100
5 101
6 110
7 111

现在,让我们以这种方式安排您的组合:

[_, _, _]
[_, _, S]
[_, B, _]
[_, B, S]
[I, _, _]
[I, _, S]
[I, B, _]
[I, B, S]

下一步是让你实现N位数

您正在看一道组合题。其中您有 (1..N) 个参数,您可能希望有一个使用输入 (1..N) 的组合序列。检索到组合后 - 您可以使用值 (v1,v2,v3) 作为 argumentList 数组的索引来检索特定的对象组合。 使用 geeksforgeeks 的以下代码作为参考,您可以在其基础上进行构建。

    // Java program to print all combination of size r in an array of size n
import java.io.*;

class Permutation {

    /* arr[]  ---> Input Array
    data[] ---> Temporary array to store current combination
    start & end ---> Staring and Ending indexes in arr[]
    index  ---> Current index in data[]
    r ---> Size of a combination to be printed */
    static void combinationUtil(int arr[], int data[], int start,
                                int end, int index, int r)
    {
        // Current combination is ready to be printed, print it
        if (index == r)
        {
            for (int j=0; j<r; j++)
                System.out.print(data[j]+" ");
            System.out.println("");
            return;
        }

        // replace index with all possible elements. The condition
        // "end-i+1 >= r-index" makes sure that including one element
        // at index will make a combination with remaining elements
        // at remaining positions
        for (int i=start; i<=end && end-i+1 >= r-index; i++)
        {
            data[index] = arr[i];
            combinationUtil(arr, data, i+1, end, index+1, r);
        }
    }

    // The main function that prints all combinations of size r
    // in arr[] of size n. This function mainly uses combinationUtil()
    static void printCombination(int arr[], int n, int r)
    {
        // A temporary array to store all combination one by one
        int data[]=new int[r];

        // Print all combination using temprary array 'data[]'
        combinationUtil(arr, data, 0, n-1, 0, r);
    }

    /*Driver function to check for above function*/
    public static void main (String[] args) {
        int arr[] = {1, 2, 3, 4, 5};
        int r = 3;
        int n = arr.length;
        printCombination(arr, n, r);
    }
}

参考:http://www.geeksforgeeks.org/print-all-possible-combinations-of-r-elements-in-a-given-array-of-size-n/

试试这个。

String[] array = {"Integer","Boolean","String"};
for (int i = 1, max = 1 << array.length; i < max; ++i) {
    for (int j = 0, k = 1; j < array.length; ++j, k <<= 1)
        if ((k & i) != 0)
            System.out.print(array[j] + " ");
    System.out.println();
}

结果

Integer 
Boolean 
Integer Boolean 
String 
Integer String 
Boolean String 
Integer Boolean String