Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

LinkedList problem

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't find the method addFirst in the API docs for that class.
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11878
195
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

// 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 ]
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic