Results 1 to 6 of 6
  1. #1
    phil128 is offline Member
    Join Date
    Sep 2008
    Posts
    40
    Rep Power
    0

    Default 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

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    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.

  3. #3
    Tshegofatsom is offline Member
    Join Date
    Apr 2009
    Location
    Pretoria, Gauteng, South Africa
    Posts
    43
    Rep Power
    0

    Default

    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 luck
    Tshegofatso Manakana
    a.k.a Untouchable

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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

  5. #5
    Tshegofatsom is offline Member
    Join Date
    Apr 2009
    Location
    Pretoria, Gauteng, South Africa
    Posts
    43
    Rep Power
    0

    Default

    Because he/she wants to convert it into an array?
    Tshegofatso Manakana
    a.k.a Untouchable

  6. #6
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    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...

Similar Threads

  1. Searching through a list.
    By wethekings in forum New To Java
    Replies: 3
    Last Post: 02-24-2009, 01:43 PM
  2. Convert Linked List Object element to String
    By CirKuT in forum New To Java
    Replies: 2
    Last Post: 12-13-2008, 06:22 AM
  3. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 09:53 PM
  4. Linked List
    By rnavarro9 in forum New To Java
    Replies: 0
    Last Post: 11-29-2007, 04:42 AM
  5. Help with linked list
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 08:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •