Java Arrays.deepToString()

String[][] ticTacToe = { {"X", "O", "O"},

                            {"O", "X", "X"},

                            {"X", "O", "X"}};

   System.out.println(Arrays.deepToString(ticTacToe));

Java 泛型 - 使用类型通配符

public void printList(List<?> list, PrintStream out) throws IOException {

      for (Iterator<?> i = list.iterator( ); i.hasNext( ); ) {

        out.println(i.next( ).toString( ));

      }

 }

Java 泛型 - 未知清单

public interface List<E> {



      public E get( );

      

      public void add(E value);

    }

Java 泛型 - 未知列表 - 添加类型限制。

public class NumberBox<N extends Number> extends Box<N> {



  public NumberBox( ) {

    super( );

  }

  

  // Sum everything in the box

  public double sum( ) {

    double total = 0;

    for (Iterator<N> i = contents.iterator( ); i.hasNext( ); ) {

      total = total + i.next( ).doubleValue( );

    }

    return total;

  }

}

You can use this same syntax in method definitions:

    public static double sum(Box<? extends Number> box1,

                             Box<? extends Number> box2) {

      double total = 0;

      for (Iterator<? extends Number> i = box1.contents.iterator( );

           i.hasNext( ); ) {

        total = total + i.next( ).doubleValue( );

      }

      for (Iterator<? extends Number> i = box2.contents.iterator( );

           i.hasNext( ); ) {

        total = total + i.next( ).doubleValue( );

      }

      return total;

    }

Java 打开枚举

public void testSwitchStatement(PrintStream out) throws IOException {

     StringBuffer outputText = new StringBuffer(student1.getFullName( ));

      

     switch (student1.getGrade( )) {

       case A:

         outputText.append(" excelled with a grade of A");

         break;

       case B: // fall through to C

       case C:

         outputText.append(" passed with a grade of ")

                    .append(student1.getGrade( ).toString( ));

         break;

       case D: // fall through to F

       case F:

         outputText.append(" failed with a grade of ")

                    .append(student1.getGrade( ).toString( ));

         break;

       case INCOMPLETE:

         outputText.append(" did not complete the class.");

         break;

     }

         

     out.println(outputText.toString( ));

   }

Java 枚举地图

public enum AntStatus {

  INITIALIZING,

  COMPILING,

  COPYING,

  JARRING,

  ZIPPING,

  DONE,

  ERROR

}


   public void testEnumMap(PrintStream out) throws IOException {

       // Create a map with the key and a String message

       EnumMap<AntStatus, String> antMessages =

         new EnumMap<AntStatus, String>(AntStatus.class);

         

       // Initialize the map

       antMessages.put(AntStatus.INITIALIZING, "Initializing Ant...");

       antMessages.put(AntStatus.COMPILING,    "Compiling Java classes...");

       antMessages.put(AntStatus.COPYING,      "Copying files...");

       antMessages.put(AntStatus.JARRING,      "JARring up files...");

       antMessages.put(AntStatus.ZIPPING,      "ZIPping up files...");

       antMessages.put(AntStatus.DONE,         "Build complete.");

       antMessages.put(AntStatus.ERROR,        "Error occurred.");

       

       // Iterate and print messages

       for (AntStatus status : AntStatus.values( ) ) {

         out.println("For status " + status + ", message is: " +

                     antMessages.get(status));

       }

     }

Java 枚举中特定于值的类主体

// These are the the opcodes that our stack machine can execute.

   abstract static enum Opcode {

     PUSH(1),

     ADD(0),

     BEZ(1); // Remember the required semicolon after last enum value



     int numOperands;



     Opcode(int numOperands) { this.numOperands = numOperands; }



     public void perform(StackMachine machine, int[] operands) {

       switch(this) {

         case PUSH: machine.push(operands[0]); break;

         case ADD: machine.push(machine.pop( ) + machine.pop( )); break;

         case BEZ: if (machine.pop( ) == 0) machine.setPC(operands[0]); break;

         default: throw new AssertionError( );

       }

     }

   }

Java Thread.java

/**
     * Causes the currently executing thread to sleep (cease execution) 
     * for the specified number of milliseconds plus the specified number 
     * of nanoseconds. The thread does not lose ownership of any monitors.
     *
     * @param      millis   the length of time to sleep in milliseconds.
     * @param      nanos    0-999999 additional nanoseconds to sleep.
     * @exception  IllegalArgumentException  if the value of millis is 
     *             negative or the value of nanos is not in the range 
     *             0-999999.
     * @exception  InterruptedException if another thread has interrupted
     *             the current thread.  The <i>interrupted status</i> of the
     *             current thread is cleared when this exception is thrown.
     * @see        java.lang.Object#notify()
     */
    public static void sleep(long millis, int nanos) 
    throws InterruptedException {
	if (millis < 0) {
            throw new IllegalArgumentException("timeout value is negative");
	}

	if (nanos < 0 || nanos > 999999) {
            throw new IllegalArgumentException(
				"nanosecond timeout value out of range");
	}

        // Et là, c'est la grosse arnaque!!!
	if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
	    millis++;
	}

	sleep(millis);
    }

Java Java素数生成器

package com.destiney.Prime;
class Prime
{
  public static void main( String args[] )
  {
    int x, y, c = 0;
    for( x = 2; x < 1000; x++ )
    {
      if( x % 2 != 0 || x == 2 )
      {
        for( y = 2; y <= x / 2; y++ )
        {
          if( x % y == 0 )
          {
            break;
          }   
        }
        
        if( y > x / 2 )
        {
          System.out.println( x );
          c++;
        }
      }
    }
    System.out.println( "\nTotal: " + c );
  }    
}

Java StringTokenizer Java

String cadenaPreguntas = request.getParameter(Keys.PREGUNTA_CADENA);
		//System.out.println("cadena de Preguntas" + cadenaPreguntas);
		StringTokenizer tokenizerPreguntas = new StringTokenizer(cadenaPreguntas, "||");
		String stringPregunta = "";
		while (tokenizerPreguntas.hasMoreTokens()) { 
		    stringPregunta = tokenizerPreguntas.nextToken();
		    
		    Pregunta pregunta = new Pregunta();
		    
		    StringTokenizer tokenizerAtributosPregunta = new StringTokenizer( stringPregunta, "##");
		    
		    // primero el texto de la pregunta
		    String textoPreg = tokenizerAtributosPregunta.nextToken();
		    if (!textoPreg.equals("")) {
			    pregunta.setTextoPregunta( Utilities.esNulo(textoPreg) );
		    }