【问题标题】:How would I return the last generic object in a list? Java我将如何返回列表中的最后一个通用对象?爪哇
【发布时间】:2013-11-04 18:08:08
【问题描述】:

这是我将对象添加到列表的地方:

   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 T last() {...}

标签: java generics methods linked-list nodes


【解决方案1】:

大概LinearNode&lt;T&gt; 有一个方法getValue(),它返回存储在该节点中的T 实例。你的LinkedList 类中已经有一个last 引用,所以它应该很简单

public T getLast()
{
    return last.getValue();
}

这只是一个骨架,需要检查空列表等

【讨论】:

    猜你喜欢
    • 2015-04-24
    • 1970-01-01
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 1970-01-01
    • 2014-08-11
    • 2018-05-20
    相关资源
    最近更新 更多