File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collecction/Generics

 
Anu Bhagat
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Abhi vijay
Ranch Hand
Posts: 509
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Please use code tags when you post source code.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic