從codes學java tiger之范型

          Posted on 2006-06-20 18:18 BlueO2 閱讀(392) 評論(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

          主站蜘蛛池模板: 略阳县| 万州区| 绥滨县| 东乡| 盱眙县| 九江县| 绿春县| 小金县| 宁乡县| 绥江县| 进贤县| 古交市| 曲松县| 灌云县| 楚雄市| 金坛市| 犍为县| 扬州市| 广汉市| 新闻| 永康市| 北流市| 青铜峡市| 瑞昌市| 灵山县| 西宁市| 松江区| 桂阳县| 五华县| 西华县| 揭西县| 鹤山市| 田东县| 灵川县| 汤阴县| 乾安县| 翁牛特旗| 桃源县| 绥滨县| 阿合奇县| 绵阳市|