2011-01-16 7 views
1

私はこのコードをできるだけジェネリックなものにしようとしていますが、最後の部分に正しく貼り付けられています。私のコードが呼び出されるところである:この汎用的なJavaについて若干の助けが必要

List<Integer> NewList = map(OriginalList, new IFunction<Integer>(){   
     public <T extends Number> int execute(T anInt){ 
      return anInt.intValue() + 1;     
     }  
}); 

、私はメソッドのマップを持っている:

public static <T> List<Integer> map(List<T> c, IFunction<T> f) {  
    List<Integer> TempList = new ArrayList<Integer>(); 
    for (T o : c){   
     TempList.add(f.execute(o)); 
    } 
    return TempList; 
} 

とインタフェースIFUNCTION:

public interface IFunction<T> {  
    public <T extends Number> int execute(T o); 
} 

私のエラーは地図(である)どこにTempList.add(f.execute(o));と述べています。 TempListをT型に宣言しようとしていて、T型でインクリメントした数値を返すexecuteメソッドを呼び出そうとしています。

コードの一部を修正するたびに、別の部分が破損したようです。そうでなければf.execute()がタイプと文句を言うだろう

public static <T extends Number> List<Integer> map(List<T> c, IFunction<T> f) {  
    ... 

:理想的にはすべてのパラメータは、ジェネリックだろうと私はあなたがmap()方法であなたのパラメータを制約する必要が

+0

スタックトレースを追加できますか? – adrianboimvaser

+0

はこのリストではありません。 TempList = new ArrayList (); Tにいる? –

答えて

1

私のコードを呼び出す場合を除きノー「整数」どこにでもあるだろう引数のどれもが可能であり、それはNumberを期待しています。

+0

ありがとう、私はちょうどジェネリックであると思った私は整数を持っていないはずですか?私はジェネリック薬をあまりにも遠くに持っていましたか – Ben

+0

いいえ、ただし、クラスに期待する最小値を指定する必要があります。 'map()'メソッドは、その引数がNumberの子孫であることを期待するメソッド( 'execute()')を呼び出すので、 'map()'はより一般的なものを受け入れることもできません。 – biziclop

0

これを試してみてください:

IFunction.java

public interface IFunction <T extends Number> { 
    T execute(T obj); 
} 

Main.java

public class Main { 

    public static void main(String[] args) { 
     List<Integer> originalList = new ArrayList<Integer>(); 
     List<Integer> newList = map(originalList, new IFunction<Integer>(){   
      public Integer execute(Integer anInt){ 
       return anInt.intValue() + 1;     
      }  
     }); 
    } 

    public static <T extends Number> List<T> map(List<T> c, IFunction<T> f) {  
     List<T> tempList = new ArrayList<T>(); 
     for (T o : c){   
      tempList.add(f.execute(o)); 
     } 
     return tempList; 
    } 
} 
0

あなたは別の一般的なセットアップ試してみてください:

public interface IFunction<T extends Number> { 
    public int execute(T o); 
} 

List<Integer> NewList = map(OriginalList, new IFunction<Integer>(){   
    public int execute(Integer anInt){ 
     return anInt.intValue() + 1;     
    }  
}); 

public static <T extends Number> List<Integer> map(List<? extends T> c, IFunction<T> f) {  
    List<Integer> tempList = new ArrayList<Integer>(); 
    for (T o : c){   
     tempList.add(f.execute(o)); 
    } 
    return tempList; 
} 
0

これは私が整数(小文字を開始するには、変数名の変更)を除去し得ることができる限り近い:

public class Main 
{ 
    public static void main(String[] args) 
    { 
     List<Integer> originalList = new ArrayList<Integer>(); 
     originalList.add(1); 
     originalList.add(2); 
     originalList.add(3); 
     originalList.add(4); 

     List<Integer> newList = map(originalList, new IFunction<Integer>() 
     { 
      public <T extends Number> T execute(T aNumber) 
      { 
       Integer result = aNumber.intValue() + 1; 
       return (T) result; 
      } 
     }); 
     System.out.println(newList); 
    } 

    public static <T extends Number> List<T> map(List<T> c, IFunction<T> f) 
    { 
     List<T> tempList = new ArrayList<T>(); 
     for (T number : c) 
     { 
      tempList.add(f.execute(number)); 
     } 
     return tempList; 
    } 

} 

public interface IFunction<T> { 
    public <T extends Number> T execute(T o); 
} 

はまだ実行()の実装内部1つを得ました。

関連する問題