[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[jfriends-ml 10765] Prototype for JSR014



  高橋(智)です。

  さっきSunのWebサイトを見たら、新しいプロトタイプがリリース
されていました。

  [Adding Generics to the Java Programming Language v2.2 ]
    http://developer.java.sun.com/developer/earlyAccess/adding_generics/

  添付のサンプルをコンパイル&実行した結果を載せておきます。(^^)
--------- Test.java を引用 --------------------------------
import java.util.LinkedList;
import java.util.Collections;
import static java.lang.Math.*; // import static

class Test2 {

  // enum
  enum Color { red, green, blue };

  // varargs
  public static void printf(String fmt, Object... args) {
    int i = 0;
    // foreach on primitive array
    for (char c : fmt.toCharArray()) {
      if (c == '%')
        System.out.print(args[i++]);
      else if (c == '\n')
        System.out.println();
      else
        System.out.print(c);
    }
  }

  public static void main(String[] args) {

    // Integer list
    LinkedList<Integer> xs = new LinkedList<Integer>();
    xs.add(new Integer(0)); xs.add(new Integer(1));
    Integer x = xs.iterator().next();
    Integer mb = Collections.max(xs);

    // string list
    LinkedList<String> ys = new LinkedList<String>();
    ys.add("zero"); ys.add("one");
    String y = ys.iterator().next();

    // string list list
    LinkedList<LinkedList<String>> zss = new LinkedList<LinkedList<String>>();
    zss.add(ys); 
    String z = zss.iterator().next().iterator().next();

    // foreach on a collection
    for (String s : ys)
      System.out.println(s);

    // varargs and boxing
    printf("Addition: % plus % equals %\n", 1, 1, 2);

    // use static import
    printf("sin(PI/12) = %\n", sin(PI/12));

    // use enums
    printf("Colors are %\n", Color.VALUES);
    for ( Color c : Color.VALUES ) {
      // switch on enum
      switch(c) {
        case Color.red:
          System.out.println("found red.");
          break;
        case Color.green:
          System.out.println("found green.");
          break;
        case Color.blue:
          System.out.println("found blue.");
          break;
      }
    }
  }
}
--------- Test.java を引用 --------------------------------


C:\>set PATH=C:\adding_generics-2_2-ea\scripts;%PATH%
C:\>set J2SE14=C:\j2sdk1.4.2
C:\>set JSR14DISTR=C:adding_generics-2_2-ea
...
C:\adding_generics-2_2-ea\examples>javac Test.java
C:\adding_generics-2_2-ea\examples>java Test
zero
one
Addition: 1 plus 1 equals 2
sin(PI/12) = 0.25881904510252074
Colors are [red, green, blue]
found red.
found green.
found blue.


-- 
高橋智宏