• Post Reply Bookmark Topic Watch Topic
  • New Topic

Removing Elements in a list  RSS feed

 
T Walsh
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys i am relatively new to java and i don't know how to solve this problem.
I have a list of Athletes and Coaches and if i want to remove any from the list it works,
but the problem is if a Coach has Athletes registered to him and i want to remove that Coach i also want
the Athletes registered with that Coach to be removed also.
I think the problem could be fixed with some nested if statements but i just need to be helped in the right direction.

This is my Remove Person Method, Any help will be highly appreciated.

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim, and welcome to the Ranch!

There is not nearly enough information to help you. We have no idea what coach, athlete, 'athlete registered with coach' means. So it is impossible for us to help you. What i would suggest is that you provide a small SSCCE that shows what you have and where you are having problems so we can understand the context of your question.

Please make the code you post complete (so it can be compiled, run, and shows the problem) but only include the absolutely necessary parts related to the problem (drop the GUI, for example, provide hard-coded data instead of requiring input, etc...)
 
Janardan Kelkar
Ranch Hand
Posts: 72
Eclipse IDE Firefox Browser Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Store your Coaches in a Map, and your Atheletes in a Set.
Something like this:



The space complexity of this is worse than using two lists, as effecively, you would be storing the athletes twice. However, given that this is an operation that would be executed in realtime, by a user, through a UI , the time savings over nested lists iterations would be good. I had faced a similar issue a few years ago, and i had implemented the scenario as above. I would use TreeMaps for coaches and HashSet for athletes.

 
T Walsh
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great advice Janardan, i know what to do with it now.

Regards
Tim
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janardan Kelkar wrote:The space complexity of this is worse than using two lists, as effecively, you would be storing the athletes twice.


I could be wrong here, but wouldn't you just create new references to the same athlete objects, thus only having twice as much references instead of twice as much athletes? That would mean that you'd still need more space, but since references are relatively small, it wouldn't matter that much.
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Hoppmann wrote: . . . I could be wrong here, . . .
I don’t think you are wrong.
Anyway, I can buy 8GB of memory for less than it costs to go and see a film and have dinner with my wife, so why are people worried about memory footprints?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim tim and Janardan:

Please DontWriteLongLines (←click). I've broken them up for you this time, but please read the link carefully.

Thanks.

Winston
 
T Walsh
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok won't happen again, sorry.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!