2016-11-07 85 views
-1

我對Java和編程相當陌生,一直在試圖找到一種方法來實現我現在想要做的事。我想從這個字符串數組中排除一些組合,例如(1,2,3,4,5,6)或(1,2,3,4,5,7)。我嘗試過濾或.remove,但似乎他們不適用於字符串數組。基本上我希望能夠過濾刪除不需要的組合的結果。這是我到目前爲止。從排列中刪除組合

import java.util.Arrays; 

public class Permutations { 
    public static void main(String[] args) { 

    String[] arraylist = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"}; 

    combinations2(arraylist, 6, 0, new String[6]); 
    } 

    static void combinations2(String[] arraylist, int len, int startPosition, String[] result) { 

    if (len == 0) { 

     System.out.println(Arrays.toString(result)); 
     return; 
    } 
    for (int i = startPosition; i <= arraylist.length - len; i++) { 
     result[result.length - len] = arraylist[i]; 
     combinations2(arraylist, len - 1, i + 1, result); 
    } 
    } 
} 
+0

「我嘗試過濾或.remove」請在您的代碼中顯示。 –

回答

0

除非您特別需要使用數組,否則我會建議切換到標準集合。它們很容易使用,你不需要傳遞索引,因爲你可以使用子列表。下面的代碼使用標準的contains方法來過濾不需要的組合。

public class Combos { 
    private static final List<List<String>> FILTER = Arrays.asList(
     Arrays.asList("1", "2", "3", "4", "5", "6"), 
     Arrays.asList("1", "2", "3", "4", "5", "7")); 

    static void combos(List<String> input, List<String> result) { 
     if (result.size() == 6) { 
      if (!FILTER.contains(result)) 
       System.out.println(result); 
     } else { 
      for (int i = 0; i < input.size(); i++) { 
       result.add(input.get(i)); 
       combos(input.subList(i + 1, input.size()), result); 
       result.remove(input.get(i)); 
      } 
     } 
    } 

    public static void main(String[] args) { 
     combos(IntStream.rangeClosed(1, 13) 
       .mapToObj(Integer::toString) 
       .collect(Collectors.toList()), 
       new ArrayList<>()); 
    } 
} 
+0

當我嘗試使用您的代碼時,它說數組無法解析,並且List無法解析爲某個類型。如果我使用排列,集合或列表,這並不重要。我只想實現創建6個元素長度的組合並過濾掉諸如2,3,4,5,6,7和1,3,4,5,6,8的證書組合的結果。 –

+0

您需要添加導入語句才能訪問這些類。 – sprinter

0

你在正確的軌道上。我注意到的一件事是,在您的main方法中,您可以使用行combinations2(arraylist, 6, 0, new String[6]);來調用combinations2方法。我想知道爲什麼你可能會將你的結果存儲在一個new String[]中,在你調用它之後就會丟失範圍(實際上你並沒有在這裏存儲解析的數組 - 我建議將它存儲到另一個數組中以便有目的地使用它例如,你可以看到下面的代碼片段)。

按照回答這個問題,我會尋找到System.arrayCopy方法,並使用它像這樣:

public class App { 
    public static void main(String[] args) { 
     String[] arraylist = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13" }; 
     System.out.println("arraylist: " + java.util.Arrays.toString(arraylist)); 
     String[] result= new String[arraylist.length - 6]; 
     System.arraycopy(arraylist, 6, result, 0, arraylist.length - 6); 
     System.out.println("result: " + java.util.Arrays.toString(result)); 
    } 
} 

輸出;

arraylist: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] 
result: [7, 8, 9, 10, 11, 12, 13] 

此外,由於1.5及更高版本有Arrays.copyOfRange(Object[] src, int from, int to)

然而,在你沒有刪除順序的情況下,這將變得相當繁瑣/重複進行多次System.arraycopy()調用(*例如在你的第二種情況下(1,2,3,4,5,7)*)。

+0

你建議的作品,但它刪除了6元素格式。我想保留我的格式,但刪除了我不想保留的特定組合。我不知道如何從你在這裏給我看的東西實現這一點。 –

+0

您能否解釋更多,或者在原始帖子的示例之前/之後進行編輯?與System.arraycopy相比,特定的組合可能會更好地處理Collections。 –