사용

2017-10-05 4 views
2
나는 다음과 같은 문제에 붙어있어

: 있습니다사용

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

Object1Object2 :이 같이 정의 항목의 동적 목록을

여기서는 관심 대상이 아니지만 임의의 수의 항목을 포함하는 배열 Double[]이 있습니다.

내 코드에서 나는 ArrayList 바깥 쪽을 반복하고 구아바의 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)); 
:

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; 
} 

내가 찾은 모든 예에서를, 그들은 항상 내가 할 수없는 알려진 세트, 함께 곱 집합 전화

마지막으로 가지고 싶은 것은 내부 Double [] 배열에 저장된 숫자의 모든 일치입니다.

도움을 주시면 감사하겠습니다.

답변

2

the Post "Java Guava CartesianProduct" 덕분에 문제가 해결되었습니다. 내 최종 솔루션은 다음과 같습니다

List<Object> itemList = ArrayList(ArrayList<Object1, Object2, List<Double>>) 
:
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); 
} 

이 또한 내 입력 목록의 형식을 변경