2014-07-09 38 views
0

我已經使用「combinatoricslib」從對象數組中生成組合。但結果顯示爲向量。我想知道如何只讀取一個值。如何在java中讀取矢量

這是代碼。

// Create the initial vector 
    ICombinatoricsVector<String> initialVector = Factory.createVector(
     new String[] { "red", "black", "white", "green", "blue" }); 

    // Create a simple combination generator to generate 3-combinations of the initial vector 
    Generator<String> gen = Factory.createSimpleCombinationGenerator(initialVector, 3); 

    // Print all possible combinations 
    for (ICombinatoricsVector<String> combination : gen) { 
     System.out.println(combination); 
    } 

這就是結果。

CombinatoricsVector=([red, black, white], size=3) 
    CombinatoricsVector=([red, black, green], size=3) 
    CombinatoricsVector=([red, black, blue], size=3) 
    CombinatoricsVector=([red, white, green], size=3) 
    CombinatoricsVector=([red, white, blue], size=3) 
    CombinatoricsVector=([red, green, blue], size=3) 
    CombinatoricsVector=([black, white, green], size=3) 
    CombinatoricsVector=([black, white, blue], size=3) 
    CombinatoricsVector=([black, green, blue], size=3) 
    CombinatoricsVector=([white, green, blue], size=3) 

但它有陣列和大小的組合。但我只想得到數組。如何得到它。 請幫幫我。我是新來的Java。

在此先感謝。

回答

1

據我所知,你在這裏使用是什麼combinatorics.CombinatoricsVector

一個實例它有一個getVector方法,它返回在這樣的矢量的所有元素的List(在這種情況下,所有的顏色)和一個getValue(int index)方法,它允許您檢索特定索引處的對象。

0

你可以試試這個:

Generator<String> gen = Factory.createSimpleCombinationGenerator(initialVector, 3); 

// Print all possible combinations 
for (ICombinatoricsVector<String> combination : gen) { 
    System.out.println(combination.getValue(0)); // This gets the first value from the vector 
    System.out.println(combination.iterator().next()); // This is another way to do it 
} 

檢查Javadoc瞭解詳情。

-1

也許這會爲你工作得好:

// Print all possible combinations 
    for (ICombinatoricsVector<String> combination : gen) { 
     System.out.println(Arrays.toString(combination.toArray())); 
    } 
+0

的列表目前在ICombinatoricsVector接口沒有指定者()方法。閱讀javadoc。 –