1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
   |  
import java.util.LinkedList;
import java.util.Collections;
import static java.lang.Math.*; // import static
 
class Test {
 
    // 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 red:
		System.out.println("found red.");
		break;
	    case green:
		System.out.println("found green.");
		break;
	    case blue:
		System.out.println("found blue.");
		break;
	    }
	}
    }
} | 
Partager