| Author |
Why does SortedSet class has no .get(location) method?
|
Aditya Sirohi
Ranch Hand
Joined: Jan 05, 2010
Posts: 91
|
|
Hi,
I have put some data in the SortedSet, but i can only iterate over it using an iterator. If i have to get a element on a certain index i need to first put elements from the SortedSet to ArrayList and then do a .get(location). Is there any other better less expensive way to get a element on a certain index in SortedSet?
-Adi
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9948
|
|
|
The subset method wouldn't work?
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2780
|
|
|
Fundamentally, a Set doesn't have an index for each position. If that's what you want, you should probably use a List instead.
|
 |
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2780
|
|
|
I don't see how a subset method would work - how would you use an index in that?
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Aditya Sirohi wrote:I have put some data in the SortedSet, but i can only iterate over it using an iterator. If i have to get a element on a certain index i need to first put elements from the SortedSet to ArrayList and then do a .get(location). Is there any other better less expensive way to get a element on a certain index in SortedSet?
Iterate until you've reached the index:
This code is flawed as it doesn't check if index is valid, but you can use this basic principle.
This is slightly better than using an ArrayList, because with that you have to iterate over all the elements, even those past the index you want. After all, adding the SortedSet elements into the ArrayList requires all of the elements to be iterated over.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2780
|
|
Rob Spoor wrote:
I think this needs to be something like:
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9948
|
|
Mike Simmons wrote:I don't see how a subset method would work - how would you use an index in that?
I didn't read it closely enough. I thought you could say "from element 2 to element 3"
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Mike Simmons wrote:
Rob Spoor wrote:
I think this needs to be something like:
Yes, of course... I forgot to decrement index, and index can of course be 0 as well
|
 |
 |
|
|
subject: Why does SortedSet class has no .get(location) method?
|
|
|