2014-07-09 3 views
0

"combinatoricslib"을 사용하여 객체 배열에서 조합을 생성했습니다. 그러나 결과는 벡터로 표시됩니다. 하나의 값만 읽는 법을 알고 싶습니다.자바에서 벡터를 읽는 방법

다음은 코드입니다.

// 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) 

그러나 조합 배열과 크기를 모두 가지고 있습니다. 하지만 배열 만 가져오고 싶습니다. 그것을 얻는 방법. 도와주세요. 나는 자바를 처음 사용합니다.

미리 감사드립니다.

답변

1

나는 당신이 여기에 사용하는 것은 combinatorics.CombinatoricsVector

의 인스턴스 인 이해 그것은이 같은 벡터의 모든 요소의 List을 반환하는 getVector 방법이 있습니다 (이 경우, 모든 색상) 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 인터페이스에는 toArray() 메소드가 없습니다. javadoc을 읽습니다. –

관련 문제