2017-10-05 60 views
2

我被困在下面的问题:使用番石榴Sets.cartesianProduct的未知数量的参数

我有这样定义的项目的动态列表:

List<Object> itemList = ArrayList(ArrayList<Object1, Object2, Double[]>) 

Object1Object2是这里不感兴趣,但数组Double[]包含任意数量的条目。

在我的代码中,我遍历外部ArrayList并尝试计算番石榴的cartesianProduct。到现在我有这样的事情(部分不工作的代码,对不起......):

private Set<List<Set<Double>>> getValueCombinations() { 
    List<Set<Double>> valuesOfInnerArrays = new ArrayList<>(); 
    // Loop over the list of device data sets in the class and add the trim value vectors to a list for further 
    // processing and cartesian prooduct generation. 
    for (Integer indexCounter = 0; indexCounter < OuterArrayList.size(); indexCounter++) { 
     final List<Object> innerDataSet = (List<Object>) OuterArrayList.get(indexCounter); 
     final Set<Double> innerDoubleArray = ImmutableSet.of(((List<Double>) innerDataSet.get(2)).toArray(new Double[])); 
     valuesOfInnerArrays.add(innerDoubleArray); 
    } 
    ImmutableList<Set<Double>> test = ImmutableList.of(valuesOfInnerArrays) 
    // generate Cartesian product of all trim vectors = a n x m matrix of all combinations of settings 
    final Set<List<Set<Double>>> cartesianProduct = Sets.cartesianProduct(ImmutableList.of(valuesOfInnerArrays)); 

    return cartesianProduct; 
} 

在我发现所有的例子,他们总是叫笛卡儿积与已知的设置,这是我不能做:

Set<Double> first = ImmutableSet.of(1., 2.); 
Set<Double> second = ImmutableSet.of(3., 4.); 
Set<List<Double>> result = 
Sets.cartesianProduct(ImmutableList.of(first, second)); 

我最后想要的是存储在内部Double []数组中的所有数字的联合。

任何帮助表示赞赏。

回答

2

感谢the Post "Java Guava CartesianProduct"我解决了我的问题。我的最终解决方案是这样的:

private Set<List<Double>> getValueCombinations() { 
    final List<Set<Double>> valuesOfInnerArrays = new ArrayList<>(); 
    // Loop over the list of device data sets in the class and add the value vectors to a list for further 
    // processing and cartesian product generation. 
    for (Integer indexCounter = 0; indexCounter < outerArrayList.size(); indexCounter++) { 
     final List<Object> innerDataSet = (List<Object>) deviceDataSets.get(indexCounter); 
     final SortedSet<Double> >innerDoubleArray = new TreeSet<>((List<Double>) innerDataSet.get(2)); 
     valuesOfInnerArrays.add(innerDoubleArray); 
    } 

    return Sets.cartesianProduct(valuesOfInnerArrays); 
} 

此外,我改变了我的输入列表的格式:

List<Object> itemList = ArrayList(ArrayList<Object1, Object2, List<Double>>)