Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何返回列表中的最后一个泛型对象?JAVA_Java_Generics_Methods_Linked List_Nodes - Fatal编程技术网

如何返回列表中的最后一个泛型对象?JAVA

如何返回列表中的最后一个泛型对象?JAVA,java,generics,methods,linked-list,nodes,Java,Generics,Methods,Linked List,Nodes,这是我将对象添加到列表中的位置: public void add (T element) { LinearNode<T> node = new LinearNode<T> (element); if (size() == 0) { this.last = node; // last node this.list = node; // first node

这是我将对象添加到列表中的位置:

   public void add (T element)
   {      
       LinearNode<T> node = new LinearNode<T> (element); 

       if (size() == 0) {  
            this.last = node; // last node 
            this.list = node; // first node
            this.count++;
       }//end if
       else
          if (!(contains(element)))
          { 
              last.setNext(node); 
              last = node; 
              count++;   
          } //end if
   }
public void add(T元素)
{      
LinearNode节点=新的LinearNode(元素);
如果(size()=0){
this.last=node;//最后一个节点
this.list=node;//第一个节点
这个.count++;
}//如果结束
其他的
如果(!(包含(元素)))
{ 
last.setNext(节点);
last=节点;
计数++;
}//如果结束,则结束
}

我必须创建一个返回列表中最后一个对象的方法。有人能帮忙吗?

大概
LinearNode
有一个方法
getValue()
,它返回存储在该节点中的
T
实例。您的
LinkedList
类中已经有一个
last
引用,因此它应该尽可能简单

public T getLast()
{
    return last.getValue();
}
这只是一个框架,需要检查空列表等。

public T last(){…}