Results 1 to 2 of 2
  1. #1
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Recursion list replace elements method Help

    Hi. For an assignment, I need to create a replace method which replaces all old objects in a list with a new object. I can not use loops. Can someone help me? This must be done recursively.

    The parameter contains a list, old element, and new element.

    Here's my algorithm:

    while the first element is not null
    if the list.get(first) equals old element
    list.set(that indext, new element)
    replace(list, old element, new element)

    I don't know how I can do this without loops.r

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Recursion list replace elements method Help

    The thing about Lists is that they are made up of subLists. eg
    List {5,4,3,2,1}
    is equal to
    5 + List {4,3,2,1}
    is equal to
    4 + List {3,2,1}
    etc

Similar Threads

  1. Replies: 5
    Last Post: 01-22-2013, 07:13 PM
  2. Replies: 2
    Last Post: 03-23-2012, 04:53 AM
  3. Replies: 8
    Last Post: 11-23-2010, 11:03 AM
  4. Turning Recursion Method into Iterative method
    By mattakuevan in forum New To Java
    Replies: 9
    Last Post: 06-15-2010, 06:46 AM

Tags for this Thread

Posting Permissions

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