從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

          主站蜘蛛池模板: 万安县| 仪征市| 广宁县| 湛江市| 浦江县| 武鸣县| 蒙山县| 当阳市| 聂荣县| 白玉县| 通许县| 潍坊市| 娄烦县| 玛曲县| 什邡市| 抚远县| 和顺县| 浮梁县| 青龙| 沂源县| 阿瓦提县| 偃师市| 博乐市| 东宁县| 商丘市| 澄城县| 海城市| 都昌县| 兴业县| 甘孜县| 九龙城区| 侯马市| 隆尧县| 买车| 南皮县| 玉门市| 黔东| 广州市| 保康县| 牡丹江市| 北京市|