Generics is one of the most important features of JDK and is mainly used to process collections. The following code passes JDK1.5 debugging. Code Example 1: Demo. javapackagemaoxiang. examples. jdk15.generics; importjava. util. ArrayList; importjava. util. Collection; importjava. util. HashMap; importjava JDK
Generics is one of the most important features of JDK 1.5 and is mainly used to process collections.
The following code is successfully debugged in JDK 1.5.
Code Example 1: Demo. java
Package maoxiang. examples. jdk15.generics;
Import java. util. ArrayList;
Import java. util. Collection;
Import java. util. HashMap;
Import java. util. collections list;
Import java. util. List;
Import java. util. Map;
/**
* @ Author Mao Xiang
*
* Demonstrate how to use the Generics feature. The code is from the Generics tutorial:
* Http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
*
* Generics is similar to a template in C ++.
* Differences:
* 1.
* 2.
*/
Public class Demo {
Public static void main (String [] args ){}
/**
* Simplest usage
*/
Public void Test1 (){
// Previous usage
// List myIntList = new writable List (); // 1
// MyIntList. add (new Integer (0); // 2
// Integer x = (Integer) myIntList. iterator (). next (); // 3 needs to be forcibly converted
// Usage 1.5
List MyIntList = new parameter list (); // 1'
MyIntList. add (new Integer (0); // 2'
Integer x = myIntList. iterator (). next (); // 3'
}
/**
* Anonymous character usage
*/
Public void Test2 (){
List List = new ArrayList ();
// Print a set with anonymous characters
Wildcards (list );
Wildcards1 ();
/*
* If Wildcards2 is defined as Wildcards2 (List Shapes)
* The following call errors
*/
Wildcards2 (list );
}
Public void Wildcards (Collection <? > C ){
// Previous usage
// Iterator I = c. iterator ();
// For (int k = 0; k <c. size (); k ++ ){
//
Log (I. next ());
/