2017-07-18 61 views
-4

我已經突然停止開發我的應用程序。獲取一組對象的所有可能的組合

我需要得到的參數的陣列的所有可能的組合,例如該陣列可以是這樣的

[整數,布爾,字符串]

總可能的組合將被7(2^X - 1,其中X是參數的數量,這是我和朋友在嘗試解決此問題時提出的公式)

下面是可能的組合的可視化。
[整數,布爾,字符串],
[整數,布爾值],
[整數,字符串],
[整數],
[布爾,字符串],
[布爾]和
[字符串]

正如您在可視化中看到的,唯一需要的是條目始終具有相同的相對順序(整數必須始終在布爾和字符串之前,布爾必須始終在字符串之前)

我在問什麼是:
如何找到字符串數組的每個可能的組合,其中組合不限於當前條目的任何特定長度,而僅限於具有與每個條目相同的順序其他?

如果有人能給我一個正確的方向推動,將不勝感激。我一直在尋找幾個關於尋找每個可能的價值的帖子,但是我找不到任何對我有幫助的帖子。

如果需要有關該問題的任何進一步的信息隨時問

+0

對於大小爲n的陣列,枚舉所有整數0至2^N-1。對於此迭代中的第k個整數,當且僅當k的第i位爲1時,輸出數組[i]。在C中,通過以下方式檢查此條件:if((k >> i)&1){printf (「%s」,數組[i]); }。現在你把它轉換成Java。 – TheGreatContini

+2

Stack Overflow是一個問答網站,不是代碼編寫服務。請把你的問題縮小到具體的和關於編程的東西。 –

+0

我沒有要求你寫代碼,我想推向正確的方向@JoeC – Jockie

回答

2

讓我給你提示:十進制數的

檢查二進制介紹:

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位數

0

您正在查看組合問題。其中你有(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/

1

試試這個。

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