2016-04-02 13 views
0

これは、私が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<>(); 
        } 
     } 

助けてください。私はそれらを使用しようとする前に、ジェネリック医薬品のいくつかのチュートリアルを読んでいました、追加の注意点として

public interface MyCommonInterface { 
    public int getQuantity(); 
} 

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

+1

'T'は' getQuantity() 'メソッドを持つことをどうやって知っていますか?または 'java.lang.Object'にない他のメソッド? – bcsb1001

+0

@ bcsb1001うん、それはキャッチとエラーが私に投げられている理由です。私は作者として、このメソッドはこのメソッドを持つオブジェクトだけがこの関数を使用することを知っていますが、コンパイラはこれを実際に知っていません。 – Mlagma

+1

これらのメソッドでインターフェイスを作成する必要があるようです。 –

答えて

0

これは、あなたが入ってくるオブジェクトが関連するメソッドを持っているコンパイラに伝える方法です。彼らは当初のハングアップを取得するには少しトリッキーです。しかし、基礎を学ぶための少しの努力をしたら、実際にそれらを利用するよりはるかに良い場所にいるはずです。

+0

'getSecurity()'と 'getPrice()'メソッドもあります。しかし、これは確かに正しいアプローチです。 – bcsb1001

+0

ご返信ありがとうございます。ここで私の無知のために私を許しますが、インターフェイスは、これらのメソッドを定義する私のクラスをカプセル化する必要がありますか、それは別のエンティティですか? – Mlagma

+1

@Mlagma - それはあなたのプログラムに最適なデザインとあなた次第です。すべてのオブジェクトが共通の基底クラスを拡張するか、共通のインタフェースを実装することができます。いずれかの方法で動作します。 – jtahlborn

関連する問題