刚看了Java泛型编程 以前写过C++的模板感觉还好
把代码贴出来 希望大家来指点其中 设计和细节的好坏下面的是带头节点的List模板,不足不好的大家说 class List<T extends ListNode> { /**
 *带头节点的list,头节点没有用于存放数据
 */
T head;
T current;
T prior;
T last; //指向最后的元素
public List(T headnode){//传入头节点,第二个节点才是真正的数据开始
head = headnode;
current = head;
prior = head;
last = head;
} public boolean isEmpty(){
return head == last ? true: false;
}
public boolean isStartOfList(){
return current == head ? true : false;
}
public boolean isEndOfList(){
return current == null ? true:false;
}
public boolean isPriorNullErr(){
return prior == null ? true : false;
  }
public T hasNext(){

if(isEmpty() || isEndOfList() || isPriorNullErr()){
return null;
}
else{
if(!isStartOfList() || current != prior){
prior = current;
}
current = (T) current.getNext();//后移指针再返回 ,因为list带头节点
return current;
}
} public boolean  removeCurrent(){ if(this.isEndOfList() || isPriorNullErr()){
return false;
}
else{
prior.setNext(current.getNext());
current = prior;   //指针回退,防止接着hasNext();而漏掉节点
return true;
}
}
public void rollIndextoHead(){
current = head;
prior = head;
}
public void insertFront(T newnode){
if(this.isEmpty()){
head.setNext(newnode);
last = newnode;
}else{
newnode.setNext(head.getNext());
head.setNext(newnode);
}
}
public void insetBack(T newnode){
last.setNext(newnode);
last = newnode;
}
public T removeFront(){
if(this.isEmpty()){
return null;
}else{
T temp = (T)head.getNext();//这儿有个警告,不用@SuppressWarnings("unchecked")
                                                       //这句话这么消除
head.setNext(temp.getNext());
return temp;
}

}
// public T removeBack(){}
public void printList(){
System.out.println(this.head.toString());
T temp = hasNext();
while(temp != null){
System.out.println(temp.toString());
temp = this.hasNext();
}
}
}
interface ListNode{
public boolean setNode(ListNode listnode);
public void setNext(ListNode next);
public ListNode getNode();
public ListNode getNext();
public String toString();
}

解决方案 »

  1.   

    程序拿到eclipse中 有2个警告 怎么解决 大家帮忙
      

  2.   

    除了你不准的那种方法,还可以用1. 在eclipse的Window->Preferences->Java->Compiler->Errors/Warning->Generic types中将Unchecked generic type operation设置为Ignore。  2. 在eclipse的Window->Preferences->Java->Compiler将Compiler compliance level 设置为小于5.0的级别。你这样把ListNode和T强制类型转换难免出现类型擦除的警告吧
      

  3.   


    写泛型的警告是有的,因为有关于类型的很多,转化时有问题
    可以用@SuppressWarnings("unckecked”)
      

  4.   

    head.getNext()返回的是ListNode,而T是不为人知的一具体类型,是不能进行转换的
    lz用接口表示结点,不大合理,结点包含数据域和指针域,应该用类表示吧而头结点是list本身就有的,干嘛还要传递给构造函数呢.
    还有lz的list是这样定义的,跟一般的list不一样呢,一般的list就是插,删,改,索引,查找几个操作.
    下面是thingink in java 中链式栈的实现,非常简洁,贴出来下:
    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 s;
        while((s = lss.pop()) != null)
          System.out.println(s);
      }
    } /* Output:
      

  5.   

    lz是在用c/c++的思想写java代码,建议楼主看看java编程思想的书,Java的泛型是假的,在编译时编译器都会替换掉泛型,用类型转换来实现,当然编译器会优化你的代码,
      

  6.   


    哦 头节点的设计的确不好 那我要想很好的删去当前current节点 这么实现才比较好呢
      

  7.   


    以前学C++的思想的确有很大影响 
    用接口写节点是为了 实现这一接口的类 都能作为 List的节点
      

  8.   

    看不懂.T是一个ListNode的子类,可是ListNode是一个interface.不使用泛型不是也可以吗?为什么用泛型?ListNode不是结点的类型吗?结点的值域怎么处理?比如我想得到结点的值域以便对链表排序,怎么办啊?
      

  9.   

    可以有多种类去实现 ListNode 这种接口 用hasNext得到当前节点的应用 就可以访问值域 
    链表排序嘛 可以在List 里加 值域都可以得到 排序就简单了
      

  10.   


    hasNext得到的是ListNode类型,父类能访问到子类的定义的值域吗?
      

  11.   

    还不错,给人的感觉像C++代码。。LZ头像和我一样都是柯南
      

  12.   

    我觉得把结点类型应该用泛型。
    类似下面的:
    class LinkNode <T>{ 
    private T data; 
    private Node next; 
    private Node prior;

    public Node(T data) { 
    this.data = data; 
    this.next = null; 
    }

    public void setData(T data) { 
    this.data = data; 
    }

    public T getData() { 
    return data; 
    }

    public Node getNext() { 
    return next; 
    }

    public void setNext(Node next) { 
    this.next = next; 
    }
    public Node getPrior(){
    return prior;
    }
    public void setPrior(Node prior){
    this.prior=prior;
    }
            //下面还可以加一些其它的方法:
             //
      

  13.   

    应该是LinkNode对吧   那List就是这样写了 List<LinkNode<T>>是吗
    这样写貌似可以把功能写很强大啊
      

  14.   

      这才像JAVA的泛型( ⊙ o ⊙ )!
      

  15.   

    return current == head ? true : false;
    好像有点多余啊?
    看样子是学C++学多了
    return current == head 
    而且,用==比较,有点晕,建议用equals
      

  16.   


     java 编程思想里可以查阅相关资料
      

  17.   

    没细看算法,主要是考虑了一下泛型问题,代码不会有警告。public class Test { public static void main(String args[]){

    List<Integer,IntNode<Integer>> list = new List<Integer,IntNode<Integer>>();
    System.out.println(list.toString());
    }


    }/**
     * 该类中有两个参数类型未知.
     * 1: ListNode中包含的数据类型参数
     * 2:实现ListNode接口的具体类
     * 
     * ListNode中包含的数据类型未知,用T表示。
     * List中包含的元素类型未知用E表示。
     * 
     * 如:
     *  class IntNode<T> implements ListNode<T>{
     *  ...
     *  }
     * 使用:(好比你将Map放到List中,或者将List放到Map中。)
     *  List<Integer,IntNode<Integer>> list = new List<Integer,IntNode<Integer>>();
     *  System.out.println(list.toString());
    */
    class List<T,E extends ListNode<T>> {    /**
         *带头节点的list,头节点没有用于存放数据
         */
        ListNode<T> head;
        ListNode<T> current;
        ListNode<T> prior;
        ListNode<T> last; //指向最后的元素
        
        public List(){}
        
        public List(ListNode<T> headnode){//传入头节点,第二个节点才是真正的数据开始
            head = headnode;
            current = head;
            prior = head;
            last = head;
        }    public boolean isEmpty(){
            return head == last ? true: false;
        }
        public boolean isStartOfList(){
            return current == head ? true : false;
        }
        public boolean isEndOfList(){
            return current == null ? true:false;
        }
        public boolean isPriorNullErr(){
            return prior == null ? true : false;
         }
        public ListNode<T> hasNext(){
            
            if(isEmpty() || isEndOfList() || isPriorNullErr()){
                return null;
            }
            else{
                if(!isStartOfList() || current != prior){
                    prior = current;
                }
                current = current.getNext();//后移指针再返回 ,因为list带头节点
                return current;
            }
        }    public boolean  removeCurrent(){        if(this.isEndOfList() || isPriorNullErr()){
                return false;
            }
            else{
                prior.setNext(current.getNext());
                current = prior;   //指针回退,防止接着hasNext();而漏掉节点
                return true;
            }
        }
        public void rollIndextoHead(){
            current = head;
            prior = head;
        }
        public void insertFront(E newnode){
            if(this.isEmpty()){
                head.setNext(newnode);
                last = newnode;
            }else{
                newnode.setNext(head.getNext());
                head.setNext(newnode);
            }
        }
        public void insetBack(E newnode){
            last.setNext(newnode);
            last = newnode;
        }
        public ListNode<T> removeFront(){
            if(this.isEmpty()){
                return null;    
            }else{
             ListNode<T> temp = head.getNext();//这儿有个警告,不用@SuppressWarnings("unchecked")
                                                           //这句话这么消除
                head.setNext(temp.getNext());
                return temp;
            }
            
        }
    //    public T removeBack(){}
        public void printList(){
            System.out.println(this.head.toString());
            ListNode<T> temp = hasNext();
            while(temp != null){
                System.out.println(temp.toString());
                temp = this.hasNext();
            }
        }
    }//ListNode中包含的数据类型未知,用T表示。
    interface ListNode<T>{
        public boolean setData(T data);
        public void setNext(ListNode<T> next);
        public T getData();
        public ListNode<T> getNext();
        public String toString();    
    }class IntNode<T> implements ListNode<T>{ public T getData() {
    // TODO Auto-generated method stub
    return null;
    } public ListNode<T> getNext() {
    // TODO Auto-generated method stub
    return null;
    } public boolean setData(T data) {
    // TODO Auto-generated method stub
    return false;
    } public void setNext(ListNode<T> next) {
    // TODO Auto-generated method stub

    }

    }
      

  18.   

    “好比你将Map放到List中,或者将List放到Map中。”
    这个比喻不好,很糟糕,等待大家更贴切的比喻。