How can I write a method that deletes the last value of 开发者_JAVA技巧a list and returns the deleted value? If the list is empty, it should throw an exception. This is for a linkedlist class.
Thanks!
edit: nevermind! i'm allowed to use the java built in method. thanks!
java.util.LinkedList has the removeLast() method out of the box...
You can use the removeLast() method from LinkedList. If your variable is of type List then this method will not be available, you may have to cast it back to LinkedList again/change the type to LinkedList
Is this homework? LinkedList in Java has a removeLast method with returns the element removed.
I hear a distant pop coming from a Stack ;)
Unfortunatly, Stack
is a subclass of Vector
and not of LinkedList
(Hint hidden behind the last link ;) )
LinkedList has a method called remove
http://download.oracle.com/javase/1.4.2/docs/api/java/util/LinkedList.html#remove%28int%29
It will return the element removed.
精彩评论