解析来自增强for循环中的泛型对象的方法调用

时间:2016-04-02 21:21:33

标签: java generics

对于比我更熟悉Java的人来说,这可能是一个简单的问题。这是我的问题的要点:

我有一个函数,它基本上生成ArrayList中包含的对象的可能组合。由于我有多个需要使用此功能的对象,因此该功能正在尖叫我使其成为通用的。但是,我遇到的问题是增强的for循环无法解析来自泛型迭代器的方法调用。我理解为什么会发生这种情况,但我不熟悉Java知道如何解决这个问题。无论如何,这是我的代码:

private <T> ArrayList<T> determineIdealOrderCombination(ArrayList<T> orders, int position){
            // Local Variable Declarations
                List<ArrayList<T>> subsets = new ArrayList<>();
                int k = orders.size()+1;         // Add one due to the do-while loop
                int theoreticalQuantity;
                int indexOfMaxProfit;
                double maxProfit;
                int[] s;             // Here we'll keep indices pointing to elements in input array
                double[] profits;    // Here we'll keep track of the profit of each combination

            // Begin searching for valid combinations
                do {
                    // Setup
                        k--;
                        s = new int[k];
                        profits = new double[k];

                    // Generate combinations
                        if ( (k <= orders.size()) && (k > 0) ) {
                            // Set the first index sequence: 0, 1, 2,...
                            for (int i = 0; (s[i] = i) < k - 1; i++) ;
                            subsets.add(getSubset(orders, s));
                            for (; ; ) {
                                int i;
                                // Find position of item that can be incremented
                                for (i = k - 1; i >= 0 && s[i] == orders.size() - k + i; i--) ;
                                if (i < 0) {
                                    break;
                                } else {
                                    s[i]++;                    // increment this item
                                    for (++i; i < k; i++) {    // fill up remaining items
                                        s[i] = s[i - 1] + 1;
                                    }
                                    subsets.add(getSubset(orders, s));
                                }
                            }

                            // All combinations have been evaluated, now throw away invalid combinations that violate the upper limit
                            // and calculate the valid combinations profits.
                                for (int i = 0; i < subsets.size(); i++) {
                                    // Calculate the final position
                                        theoreticalQuantity = position;
                                        profits[i] = 0;
                                        for (T t : subsets.get(i)) {
                                            theoreticalQuantity += t.getQuantity(); // <-- THE PROBLEM
                                            profits[i] += calculateProjectedProfit(t.getSecurity(), t.getQuantity(), t.getPrice()); // <-- THE PROBLEM
                                        }

                                        if(theoreticalQuantity > _MAX_POSITION_PER_ASSET){
                                            // Negate profits if final position violates the position limit on an asset
                                                profits[i] = Double.MIN_VALUE;
                                        }
                                }
                        }
                        else{
                            break;
                        }
                }
                while( (subsets.size() == 0)  );

                // Verify that the subset array is not zero - it should never be zero
                    if(subsets.size() == 0){
                        return new ArrayList<>();
                    }

                // Return the most profitable combination, if any.
                    indexOfMaxProfit = -1;
                    maxProfit = Double.MIN_VALUE;
                    for(int i = 0; i < profits.length; i++){
                        if(profits[i] != Double.MIN_VALUE){
                            if(profits[i] > maxProfit){
                                maxProfit = profits[i];
                                indexOfMaxProfit = i;
                            }
                        }
                    }

                    if( (maxProfit > 0) && (indexOfMaxProfit != -1) ){
                        return subsets.get(indexOfMaxProfit);
                    }
                    else{
                        return new ArrayList<>();
                    }
        }

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

这是告诉编译器传入对象具有相关方法的方法:

public interface MyCommonInterface {
  public int getQuantity();
}

private <T extends MyCommonInterface> ArrayList<T> determineIdealOrderCombination(ArrayList<T> orders, int position) {

作为补充说明,我会在尝试使用它们之前阅读一些关于泛型的教程。最初得到它们有点棘手。然而,一旦你花了一点力气去学习基础知识,你应该在一个更好的地方实际使用它们。