【发布时间】:2018-03-17 09:13:03
【问题描述】:
我遇到了以下问题:
我有一个动态的项目列表,定义如下:
List<Object> itemList = ArrayList(ArrayList<Object1, Object2, Double[]>)
Object1 和 Object2 在这里不感兴趣,但数组 Double[] 包含任意数量的条目。
在我的代码中,我遍历外部ArrayList 并尝试计算番石榴的笛卡尔积。到目前为止,我有类似的东西(部分代码不起作用,对不起......):
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[] 数组中的数字的所有组合。
任何帮助表示赞赏。
【问题讨论】:
标签: java guava cartesian-product