Results 1 to 6 of 6
- 04-11-2009, 09:50 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 40
- Rep Power
- 0
Searching for a String within an linked list
Hi All. I'm trying to implement sorting through an linked list just using an string. I'm not too sure how to do this. But one thought is converting this into an array then sorting it then converting the nodes back into the link list. Does anybody know how i can implement this?
Thanks people
- 04-12-2009, 12:34 AM #2
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
Actually, that's usually the best way to sort a linked list, unless you know in advance that the list is more-or-less sorted.
My advice would be use Collections.sort() -- sorting a list is precisely what it's there for. If you're interested in how it actually does its job, you could look at the source code.Neil Coffey
Javamex - Java tutorials and performance info
- 04-12-2009, 12:40 AM #3
Member
- Join Date
- Apr 2009
- Location
- Pretoria, Gauteng, South Africa
- Posts
- 43
- Rep Power
- 0
Hi
I'm not sure what you mean by "just using a string" but I think what you want to do is:
Declare a List
Do whatever with the list
Declare a new array and store the list in this array by using the method list.toArray() or toArray(Object[] a) . The later method returns an array of the type specified.
sort the array
clear your list
loop through your array and add each element to the list using the method add(Object o)
Good luckTshegofatso Manakana
a.k.a Untouchable ™
- 04-12-2009, 12:18 PM #4
Why do that when you can just sort the list with Collections.sort()?
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 04-12-2009, 03:42 PM #5
Member
- Join Date
- Apr 2009
- Location
- Pretoria, Gauteng, South Africa
- Posts
- 43
- Rep Power
- 0
Because he/she wants to convert it into an array?
Tshegofatso Manakana
a.k.a Untouchable ™
- 04-13-2009, 02:18 AM #6
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
The OP said they wanted to convert the linked list to an array purely to sort then convert back to a linked list. That's precisely what Collections.sort() does. There's a reason the JDK contains megabytes of library classes: so you can use them...
Neil Coffey
Javamex - Java tutorials and performance info
Similar Threads
-
Searching through a list.
By wethekings in forum New To JavaReplies: 3Last Post: 02-24-2009, 12:43 PM -
Convert Linked List Object element to String
By CirKuT in forum New To JavaReplies: 2Last Post: 12-13-2008, 05:22 AM -
Linked List integer list
By igniteflow in forum Advanced JavaReplies: 1Last Post: 12-10-2008, 08:53 PM -
Linked List
By rnavarro9 in forum New To JavaReplies: 0Last Post: 11-29-2007, 03:42 AM -
Help with linked list
By trill in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:29 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks