最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java編程思想里的泛型實現(xiàn)一個堆棧類 分享

 更新時間:2013年07月01日 14:46:47   作者:  
這篇文章介紹了Java編程思想里的泛型實現(xiàn)一個堆棧類,有需要的朋友可以參考一下

覺得作者寫得太好了,不得不收藏一下。

對這個例子的理解:

//類型參數(shù)不能用基本類型,T和U其實是同一類型。

//每次放新數(shù)據(jù)都成為新的top,把原來的top往下壓一級,通過指針建立鏈接。

//末端哨兵既是默認構(gòu)造器創(chuàng)建出的符合end()返回true的節(jié)點。

復(fù)制代碼 代碼如下:

//: generics/LinkedStack.java
// A stack implemented with an internal linked structure.
package generics;

public class LinkedStack<T> {
  private static class Node<U> {
    U item;
    Node<U> next;
    Node() { item = null; next = null; }
    Node(U item, Node<U> next) {
      this.item = item;
      this.next = next;
    }
    boolean end() { return item == null && next == null; }
  }
  private Node<T> top = new Node<T>(); // End sentinel
  public void push(T item) {
    top = new Node<T>(item, top);
  }   
  public T pop() {
    T result = top.item;
    if(!top.end())
      top = top.next;
    return result;
  }
  public static void main(String[] args) {
    LinkedStack<String> lss = new LinkedStack<String>();
    for(String s : "Phasers on stun!".split(" "))
      lss.push(s);
    String ss;
    while((ss = lss.pop()) != null)
      System.out.println(ss);
      //----- if put integer into the LinkedList
      LinkedStack<Integer> lii = new LinkedStack<Integer>();
      for(Integer i = 0; i < 10; i++){
          lii.push(i);
      }
      Integer end;
      while((end = lii.pop()) != null)
          System.out.println(end);
      //----- integer test end!
  }

 
}
/* Output:
stun!
on
Phasers
*/

相關(guān)文章

最新評論

合川市| 彭水| 肃南| 永年县| 天柱县| 滁州市| 涿鹿县| 通城县| 泸水县| 西藏| 邵阳市| 黄平县| 山丹县| 乃东县| 峨山| 会理县| 高要市| 贡嘎县| 兴义市| 共和县| 浙江省| 泸溪县| 达孜县| 大丰市| 阳江市| 秀山| 武宁县| 鄂托克前旗| 银川市| 衡阳市| 苗栗市| 孟津县| 金门县| 邓州市| 津市市| 大渡口区| 栾城县| 寻甸| 汶川县| 南川市| 宁安市|