It's not an error, it's a piece of advice -- and in this case, it's bad advice, since you're actually using the richer interface of LinkedList. I haven't used Sonar personally, but hopefully it has some way to turn off this warning for this line -- with a setting somewhere, or possibly using an annotation in the source (ugh).
Anyway, feel free to ignore it. This is a case where automatic style checking just makes more work (in fact, most of the time, automatic style checking just makes more work, but that's another discussion.)
I'm not sure what this has to do with performance -which is the title and topic of this forum- but "add(0, ...)" -which is part of the List interface- does the same as "addFirst(...)".
Ernest Friedman-Hill
author and iconoclast
Marshal
It does indeed, except that by calling addFirst(), you're guaranteed to be calling an efficient operation. If you generalize to add(0,...) then you're implying that any List is Ok, when the truth is that anything but LinkedList will be inefficient. Using the more specific type is really more appropriate here, IMO.
Mahendra Athneria
Greenhorn
Joined: Jan 18, 2009
Posts: 16
posted
0
Hi Ernest Friedman-Hill,
Thanks for your reply. but my doubt is that by using addfirst() i am using more specific method and that is my requirement. if we can do the same thin with add(0,..) then what is the use of addFirst() and linkedlist?
Regards,
Mahendra Athneria
Mumbai
India
Lester Burnham
Rancher
Joined: Oct 14, 2008
Posts: 1337
posted
0
Ernest already mentioned it: efficiency reasons. There are implementations of List (notably ArrayList) for which inserting an element at position 0 is an O(n) operation instead of O(1) (which it is for LinkedList).
Mahendra Athneria
Greenhorn
Joined: Jan 18, 2009
Posts: 16
posted
0
Hi Lester,
thanks, i got it.
but it means List add(0,..) and linkedList addFirst() work in the same fashion? only difference is the Big O complexity of both.
Lester Burnham
Rancher
Joined: Oct 14, 2008
Posts: 1337
posted
0
it means List add(0,..) and linkedList addFirst() work in the same fashion?