This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collecction/Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collecction/Generics" Watch "Collecction/Generics" New topic
Author

Collecction/Generics

Anu Bhagat
Ranch Hand

Joined: Jun 20, 2008
Posts: 64
Hope this is the right place to ask java certification question.

If not please let me know where I i should ask.

Any How here is my question.

I have this code which compiles fine.

Code -----------------------------

import java.util.*;

public class Test {

public static void main() {

LinkedList<String> x = new LinkedList<String>(); #1

x.add("one");
x.add("two");
x.add("Two");

System.out.println(x.poll());

}

}

Why does it compile? ArrayList does not have poll method. it doesn't inherit it either. I tried to read API several times. The poll method is in Queue,
so I think the line #1 can be only the line below.
Queue<String> x = new PriorityQueue<String>();

What am I missing here? Please explain.
This is my first time asking question here, so forgive me if this is not the right place to post a question.

Thanks in advance.


Anu Bhagat

SCJP5.0, SCJA
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Hi Anu, In the above example where have you used ArrayList?

You have used LinkedList which implements Queue Interface, so the poll() method works.

Hope this helps.
Anu Bhagat
Ranch Hand

Joined: Jun 20, 2008
Posts: 64
Thank you very much. I see what I missed. I was typing LinkedList and thinking ArrayList, so iw wasn't working for me. Thanks for clarifying.

Regards,

Anu Bhagat
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Welcome to JavaRanch.

Please use code tags when you post source code.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
wood burning stoves
 
subject: Collecction/Generics
 
Similar Threads
Whats the output???
Question about PriorityQueue
Priority queue
question regarding PriorityQueue
question regarding PriorityQueue