• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Collecction/Generics

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Please use code tags when you post source code.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic