aspose file tools*
The moose likes Java in General and the fly likes LinkedList problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "LinkedList problem" Watch "LinkedList problem" New topic
Author

LinkedList problem

saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
hi List is the interface and LinkedList implements the interface and here in the code i am taking l as List reference type then why it is giving error at line error?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I don't find the method addFirst in the API docs for that class.
saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
hey, addFirst method is there in LinkedList class

void addFirst(E o)
Inserts the given element at the beginning of this list.

you just refer to it once again
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11506
    
  95

Hi Saikrishna,

Keith is correct - there is no addFirst method in the List interface, which is why you are getting the error.

You have specified that l is a List. The fact that it happens to be backed by a LinkedList is irrelevant to the Test class - the Test class will only treat it as a List.

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
ok thanx , i am more confused now

every one follows the above way for creating an instance

i.e alwasy we must use parent type reference to the child class object

example
List l=new ArrayList();
List l1=new vecotr();
map m=new Hashtable();

but no onfe prefers to do this
ArrayList a =new ArrayList();
Vector v =new Vector();
HashTable h=new Hashtable();

am i right??
:roll:
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14345
    
  22

Right. Normally, it's good practice to program to an interface (List in this case) and not to a particular implementation of the interface (ArrayList or LinkedList).

The reason for that is that almost always, the rest of the program only needs to know that it is dealing with a List. It doesn't need to know which particular implementation it's using.

By programming to the interface List, it's very easy to change the implementation later - you only need to change one line of code.

But sometimes you need to do something that is specific to the particular implementation of the interface you're using, so just using the common interface unfortunately doesn't work.

If all you want to do is add an element to the beginning of the List and you don't want to break the "program to an interface" practice, you can use the add(...) method in interface List with two parameters to insert an element at the 0th position:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
Originally posted by Jesper Young:
Right. Normally, it's good practice to program to an interface (List in this case) and not to a particular implementation of the interface (ArrayList or LinkedList).


why not in this case? javasoft must inform about this case in the API

Originally posted by Jesper Young:

If all you want to do is add an element to the beginning of the List and you don't want to break the "program to an interface" practice, you can use the add(...) method in interface List with two parameters to insert an element at the 0th position:


for adding an element at specific location is alright for linkedlist but if i want to use some other methods like peek(),poll()


now please tell me the solution for this problem
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14345
    
  22

I think you misunderstood that sentence of me. You should always program to an interface, so use List and not ArrayList or LinkedList. You already know this:

Note that het methods peek() and poll() are part of interface Queue and Deque. Maybe you should write your code like this if you need to treat the list as a queue or a double-ended queue:
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791

// This is good
List<Integer> lst1 = new LinkedList<Integer>();

// This is not good
LinkedList<Integer> lst2 = new LinkedList<Integer>();

That's a bit strong.

I always recommend to use the most abstract thing that will work. That is, prefer an interface over an abstract class, an abstract class over a concrete class. But if you really need to use addFirst() then LinkedList is the only "thing that will work." What you gain is access to methods that are found only on LinkedList. Using the LinkedList type is the only way to use that method, so it absolutely has to be done sometimes. What you lose is the flexibility to use this chunk of code with an ArrayList. It happens.

[ July 22, 2006: Message edited by: Stan James ]
[ July 23, 2006: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
Originally posted by Jesper Young:
I think you misunderstood that sentence of me. You should always program to an interface, so use List and not ArrayList or LinkedList. You already know this:

Note that het methods peek() and poll() are part of interface Queue and Deque. Maybe you should write your code like this if you need to treat the list as a queue or a double-ended queue:


ok, but why shoudl i go for Queue interface
and how can i remeber all those interfaces ?
:roll:

i think it is better to write in this way if i am not confident about it.
LinkedList l=new LinkedList();
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: LinkedList problem