Java 泛型 Generic

  1. package  v13;
  2. import  java.util.*;
  3. public   class  Test {
  4. public   static   void  main( String []  args ){
  5. List<</SPAN>Stringc new ArrayList<</SPAN>String>();//采用泛型 使传入的对象类型只能是String
  6. c .add( "aaa" );
  7. c .add( "bbb" );
  8. c .add( "ccc" );
  9. for  ( int   i  ; i c .size(); i ++){
  10. String   s   c .get( i );
  11. System. out .println( s );
  12. System. out .println( c .get( i ));
  13. }
  14.  
  15. Collection<</SPAN>Stringc2 new HashSet<</SPAN>String>();//泛型使用
  16. c2 .add( "aaa" );
  17. c2 .add( "bbb" );
  18. c2 .add( "ccc" );
  19. for  (Iterator<</SPAN>Stringit c2.iterator();it.hasNext(); ){
  20. String   s   it .next();
  21. System. out .println( s );
  22. }
  23. Map<</SPAN>String,Integer> m new HashMap<</SPAN>String,Integer>();//泛型使用,注意参数
  24.  
  25.  
  26. }
  27.  
  28. }
  29.  
  30. class  MyName  implements  Comparable{
  31. int   age ;
  32. public   int  compareTo(MyName  my ){
  33. if  ( this . age   my . age return  1;
  34. else   if ( this . age   my . age return  -1;
  35. else   return  0;
  36. }
  37. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值