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
Post by:jishnu dasgupta
, Ranch Hand
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!!!
Post by:Campbell Ritchie
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.
Post by:Michael Alex
Thanks. I got everything working except sort. I want to sort by the student's name(in alphabetical order).any suggestion?
Post by:Stephan van Hulst
, Saloon Keeper
Take a look at Collections.sort().
Bras cause cancer. And tiny ads:
Thread Boost - a very different sort of advertising