Update: The defects have been solved in the latest release of 1.5更新日: 2007の欠陥が解決され、最新のリリース1.5

I wanted to create a simple max function, something I wanted to do for quite sometime, which takes a variable number of Number & Comparable arguments, essetially Integer, Double etc., and returns the max of the same type.したかった単純なマックス関数を作成するには、何かしたかったこれを行うにはかなりのいつか、これでは、変数の数の数と同等の引数は、整数essetially 、ダブルルーム等、および戻り値の上限と同じタイプです。 Also I would like to use auto-boxing to enable me to pass int & floats.またしたいボクシングを有効にするオートを使用する私に渡すint &フロートが登場します。 With generics, varargs, auto-boxing available, I thought I was all set until I tried.にノー、のVarargs 、オートボクシングの利用可能な、と思ってみました私はすべての設定までです。 I realized the dream is there but JDK 1.5 is still far from realizing the vision.私の夢があるしかし、実現はまだ程遠いのJDK 1.5のビジョンを実現します。

The code shows an implementation which should, but doesn’t compile with jdk1.5.ショーのコードを実装するには、コンパイルしてjdk1.5をほしいわけではない。

To compile use javac -source 1.5 MathUtil.java使用するソースをコンパイルするのjavac 1.5 mathutil.java

It also shows a commented implementation (dumbed down version) which compiles.また、コメントの実装を示します( dumbedダウン版)をコンパイルします。 However it doesn’t allow me to realize the benefits of varargs in this case.しかし私を実現することは許可されませんのVarargsこの場合の利点をします。

Also note that the errors messages are ambiguous (what an ambiguous spelling!).また、エラーメッセージがあいまいに注意して(どのようなあいまいなスペルチェック! )です。 For example the first errors says: generic array creationたとえば、最初のエラーは言う:汎用的な配列を作成
Sure I can create a generic array if I use: T [] args, as shown in the commented code.もちろん汎用的な配列を作ることもできますを使用すればよい拡張: T [ ] argsに示すように、コメントのコードです。
The other two errors messages are ambiguous too.他の2つのエラーメッセージがあまりにもあいまいです。

Here is the sample code with comments:ここでは、サンプルコードをコメント:

 /** This class demonstrate few limitations in jdk implementation. / **このクラスを示すいくつかの制限がJDKの実装です。 The objective is  *  to implement a generic max function which takes any number of Number values and returns  *  the max.の目標は*を実装するのは、汎用的な機能を任意の数の最大値と戻り値*の最大数です。 It allows variable arguments so you can say max(1, 2, 3).これにより、変数の引数を言ってやるが上限することができます( 1 、 2 、 3 ) 。 It supports  *  autoboxing so you can use 1 instead of Integer.valueOf(1). autoboxingのようにサポート* 1の代わりに使用することができますinteger.valueof ( 1 ) 。 And it returns the  *  data of the same type as passes parameter, so type casting is not required,  *  using generics capability. *戻り値と同じタイプのデータをパラメータとして渡すため、鋳造のタイプは必須ではありません、 *を使用してジェネリックが可能です。 *  *  This class doesn't compile. * *このクラスはありませんコンパイルします。 Errors are shown below.エラーは次のとおりです。 *  * * * 
 *  MathUtil.java:16: generic array creation  *  public static * mathutil.java : 16 :汎用的な配列を作成*公共の静的な  T max(T … args) {  *                                                   ^  *  MathUtil.java:34: T最大(トン… args ) ( * ^ * mathutil.java : 34 :  max(T[]) in MathUtil cannot be applied to (int,int,int,int)  *  ; no instance(s) of type variable(s) T exist so that argument type int conforms  *  to formal parameter type T[]  *          assert max(10, 2, 3, 5).compareTo(10) == 0;;  *                 ^  *  MathUtil.java:35:マックス(トン[ ] )でmathutilに適用されることはできません( int 、 int 、 int 、 int ) * ;ないのインスタンス(劇団)型の変数(秒) int型の引数が存在するように適合*を正式なパラメータタイプT [ ] *主張上限( 10 、 2 、 3 、 5 ) 。 compareto ( 10 ) == 0 ; ; * ^ * mathutil.java : 35 :  max(T[]) in MathUtil cannot be applied to (double,double,do  *  uble,double); no instance(s) of type variable(s) T exist so that argument type d  *  ouble conforms to formal parameter type T[]  *          assert max(10.0, 2.0, 3.2, 5.0).compareTo(10.0) == 0;;  *                 ^  *  3 errors  *マックス(トン[ ] )でmathutilに適用されることはできません(ダブル、ダブル、行う* uble 、ダブル) ;ないのインスタンス(劇団)型の変数(秒)の引数が存在するようにタイプD * oubleに適合して正式パラメータタイプT [ ] *主張上限( 10.0 、 2.0 、 3.2 、 5.0 ) 。 compareto ( 10.0 ) == 0 ; ; * ^ * 3エラー* 

*/ * /
public class MathUtil {パブリッククラスmathutil (
/* This alternative dumbed down implementation works / *この代替dumbedダウンの実装の作品
public static公共の静的な T max(T args[]) { T最大(トンargs [ ] ) (
assert args.length > 1;主張args.length > 1 ;
T max = args[0]; T最大= args [ 0 ] ;
for(T arg:args) {を(トンarg高級: args ) (
if(max.compareTo(arg) < 0) {もし( max.compareto ( arg高級) < 0 ) (
max = arg;マックス= arg高級;
}
}
return max;戻りマックス;
}
*/ * /

public static公共の静的な T max(T … args) { T最大(トン… args ) (
assert args.length > 1;主張args.length > 1 ;
T max = args[0]; T最大= args [ 0 ] ;
for(T arg:args) {を(トンarg高級: args ) (
if(max.compareTo(arg) < 0) {もし( max.compareto ( arg高級) < 0 ) (
max = arg;マックス= arg高級;
}
}
return max;戻りマックス;
}

public static void main(String … args) {公共の静的な無効メイン(文字列… args ) (
/* This alternative dumbed down implementation works / *この代替dumbedダウンの実装の作品
assert max(new Integer[] {10, 2, 3, 5}).intValue() == 10;主張マックス(新整数[ ] ( 10 、 2 、 3 、 5 ) ) 。 intvalue ( ) == 10 ;
assert max(new Double[] {10.0, 2.0, 3.2, 5.0}).doubleValue() == 10.0;主張マックス(新ダブル[ ] ( 10.0 、 2.0 、 3.2 、 5.0 ) ) 。 doublevalue ( ) == 10.0 ;
*/ * /

assert max(10, 2, 3, 5).compareTo(10) == 0;主張上限( 10 、 2 、 3 、 5 ) 。 compareto ( 10 ) == 0 ;
assert max(10.0, 2.0, 3.2, 5.0).compareTo(10.0) == 0;主張上限( 10.0 、 2.0 、 3.2 、 5.0 ) 。 compareto ( 10.0 ) == 0 ;
}
}

See more onもっと見る上 bugs in jdk1.5のバグをJDK1.5は in my next post.私の次のポストを作成。