This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

DataStructure help  RSS feed

 
Michael Alex
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class that contains two arraylists- student names and Ids
I want to put them into LinkedHashSet( so, I can sort, delete, add and edit e student profile);
I created a class called Student that contains setName getName setId getId and toString methods.

now, I try to do something like

I don't know if I am on the track or not. any suggestion? Thanks
 
jishnu dasgupta
Ranch Hand
Posts: 103
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Michael !!! Welcome to the ranch!!!

First off, if you are actually intending to use a LinkedHashSet method, then you need to override the equals() and hashcode() methods or else you cant guarrantee that unqiue elements are actually added to your Set.

Plus the sort, delete and edit can also be done if use a List for that matter.

and the coding part it would be better if you put it inside code tags!!!
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

It looks as if you have a misunderstanding about array indices. You seem to be using the student IDs to find elements in an array. That will only work if your IDs form a series or sequence starting with 0. You cannot apply a String as an array index. Only an integer number. What sort of code have you posted? I don't think it will compile.

Why do you want a LinkedHashSet in the first place? The intention of a Set is that it has no specific order, and I am not sure you can actually sort a LinkedHashSet. The Linked bit is to allow elements to be retrieved in the order they were originally inserted, and is not intended to support sorting (at least that is how I read the API documentation).
Your class with two Lists of ID and student data looks like a very iffy and error-prone bit of design, since there is no object maintaining correspondence between the ID and the other data. Once you have a student, they will always have the same ID throughout, so the ID field ought to be final and the setID method cannot be used. In fact it will have to be deleted.
I think you need to think again, and draw your data structure with a pencil and paper. If you want to link IDs to students, then a Map sounds appropriate.
 
Michael Alex
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I got everything working except sort. I want to sort by the student's name(in alphabetical order).any suggestion?
 
Stephan van Hulst
Saloon Keeper
Posts: 7808
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at Collections.sort().
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!