我使用"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)
但是它同时具有组合数组和大小。但我只想得到数组。如何得到它。请帮帮我。我是java新手。
您只需要读取javadoc。我花了5秒钟谷歌它,找到它:http://combinatoricslib.googlecode.com/svn/tags/release21/doc/org/paukov/combinatorics/ICombinatoricsVector.html
java.util.List<T> getVector()
返回vector作为元素列表
我知道你在这里使用的是combinatorics.CombinatoricsVector
的一个实例
它有一个getVector
方法,它返回像这样的向量中所有元素的List
(在这种情况下,所有颜色)和一个getValue(int index)
方法,它允许您在特定索引处检索对象。
你可以试试:
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
也许这对你很有用:
// Print all possible combinations
for (ICombinatoricsVector<String> combination : gen) {
System.out.println(Arrays.toString(combination.toArray()));
}