• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

LinkedList and remove

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!

I have a theoretical question here. I have this class OrderedList that represents element in ascending order.


Now I'm supposed to implement a method remove into the class OrderedList.

I thought that I could implement this method in the same way as get, size and iterator are implemented ie



I mean ; there is a method boolean remove(E obj) in java.util.LinkedList<E>Class. But I can't use that one here?

The answer looks like this:


Why do I have to do it this way?

Anders
 
Saloon Keeper
Posts: 7350
170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But I can't use that one here?


Why not?
 
Anders Kviback
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a "paper and pen" issue. I don't do any programming. 1. But you mean that it is OK to just write:

2. Well, put it this way, what's the difference?

Anders
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The difference basically is that if you program the logic yourself, you write more code, have more code to maintain in the future and have higher probability of bugs in that code, since core JDK classes have been already proved bug-free by millions of users.

In any case, I'd consider two possible different approaches:

1) Use an ArrayList instead of LinkedList to keep the data in. With ArrayList, you can efficiently perform binary searches (there are JDK methods for that) and random access.

2) Use a TreeSet to keep the data in. It keeps data sorted without the need to write a single line of code. Granted, it does not allow duplicates and you'll have to program index access yourself, but since your requirements are quite vague, it might be worth considering.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Martin Vajsar wrote:The difference basically is that if you program the logic yourself, you write more code, have more code to maintain in the future and have higher probability of bugs in that code, since core JDK classes have been already proved bug-free by millions of users.



On the other hand, by doing it yourself a time or two you learn more about the building blocks of the tools you'll be using day-in and day-out later. If the OP is at the "learning about data structures" stage of his education, then it's worth slogging through it.
 
Anders Kviback
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for answering,

this is, as you propably understand, a java datastructure course. A very interesting course but sometimes a bit tricky.

Thanks again, Anders
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anders Kviback wrote:Thanks for answering,

this is, as you propably understand, a java datastructure course. A very interesting course but sometimes a bit tricky.

Thanks again, Anders



So, where are you now? Do you still have a question?
 
Anders Kviback
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry about that, no I don't have a question. I got the answer.

Anders
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic