从codes学java tiger之范型

Posted on 2006-06-20 18:18 BlueO2 阅读(371) 评论(0)  编辑  收藏 所属分类: JAVA foundation
public   class  Generic  {
    
    
/**  Creates a new instance of Generic  */
    
public  Generic()  {
    }

    
    
public   void  collectionGeneric() {
        
// generic list
        List < String >  onlyStrings  =   new  LinkedList < String > ();
        onlyStrings.add(
" Legal addition " );
        
// generic map
        Map < Integer, Integer >  squares  =   new  HashMap < Integer, Integer > ();
        
for  ( int  i = 0 ; i < 100 ; i ++ {
            squares.put(i, i
* i);
        }

        
for  ( int  i = 0 ; i < 10 ; i ++ {
            
int  n  =  i * 3 ;
            System.out.println(
" The square of  "   +  n  +   "  is  "   +  squares.get(n));
        }

        
// generic iterator
        List < String >  listOfStrings  =   new  LinkedList < String > ();
        listOfStrings.add(
" Happy " );
        listOfStrings.add(
" Birthday " );
        
for  (Iterator < String >  i  =  listOfStrings.iterator(); i.hasNext( ); )  {
            String s 
=  i.next( );
            System.out.println(s);
        }

    }

    
// parameter generic
     public   void  paramGeneric(List < String >  list) {
        
for (Iterator < String >  i  =  list.iterator();i.hasNext();) {
            String s 
=  i.next();
            System.out.println(s);
        }

    }

    
// return generic
     public  List < String >  returnGeneric() {
        List
< String >  list  =   new  LinkedList < String > ();
        list.add(
" string1 " );
        
return  list;
    }

    
// conversion
     public   void  conversionGeneric() {
        LinkedList
< Float >  floatList  =   new  LinkedList < Float > ();
        List
< Float >  moreFloats  =  floatList;
        
// illegal conversion LinkedList<Number> numberList = floatList;
        
// the way to resolve for backward capbility
        List < Integer >  ints  =   new  LinkedList < Integer > ();
        List oldList 
=  ints;
        List
< Number >  numList  =  oldList;
    }

    
// generic wildcard : this old plain way will generate unchecked warning
     public   void  wildcardGeneric(List list) {
        
for  (Iterator i  =  list.iterator(); i.hasNext(); )  {
            System.out.println(i.next().toString());
        }

    }

    
// generic wildcard
     public   void  wildcardGeneric2(List <?>  list) {
        
// you can't solve the problem by List<Object>
         for  (Iterator <?>  i  =  list.iterator( ); i.hasNext(); )  {
            System.out.println(i.next().toString( ));
        }

    }

    
}

/**
 * public class Box<T extends Number>
 * 
@author  david.duan
 
*/

public   class  Box < T >   {
    
    
// you can't have a static variable such as protected static List<T> list
     protected  List < T >  contents;
    
    
public  Box( )  {
        contents 
=   new  ArrayList < T > ( );
    }

    
public   int  getSize( )  {
        
return  contents.size( );
    }

    
public   boolean  isEmpty( )  {
        
return  (contents.size( )  ==   0 );
    }

    
public   void  add(T o)  {
        contents.add(o);
    }

    
public  T grab( )  {
        
if  ( ! isEmpty( ))  {
            
return  contents.remove( 0 );
        }
  else
            
return   null ;
    }

    
public   static   void  main(String[] args) {
        Box
< String >  box  =   new  Box < String > ();
        
//
    }

}

只有注册用户登录后才能发表评论。


网站导航:
 

posts - 29, comments - 3, trackbacks - 0, articles - 0

Copyright © BlueO2