10 Replies Latest reply: Jul 16, 2009 9:20 AM by 807588 RSS

    Recursive program

    807588
      I have class called Item and now i want search item by name.
      Assume,How can we achieve this? using recursive program?
      If so,some one Please send me the code.

      class Item
      {

      String name;
      List<Item> items;


      }

      Thanks in advance
        • 1. Re: Recursive program
          807588
          kaleems wrote:
          If so,some one Please send me the code.
          That is not how this forum generally works... The following will work: you think about your problem and you give it a go. If at that time you run into a problem, you ask a very specific question to solve that particular problem.

          btw asking for someone else to do your work is like putting a "kick-me"-sign on your own back.
          • 2. Re: Recursive program
            699554
            kaleems wrote:
            I have class called Item and now i want search item by name.
            Assume,How can we achieve this? using recursive program?
            If so,some one Please send me the code.
            Ahh that's easy since you have not specified the collection Item belongs to, I conclude that only one Item exists therefore the following recursive "program" must be sufficient.
            //Item a = new Item(...)
            public Item findItem(String name, int totalItems){
              if(totalItems > 0)
                return a.name.equals(name) ? item : findItem(name, --totalItems);
              return null;
            }
            Mel
            • 3. Re: Recursive program
              807588
              Mel,

              Thanks for the code.

              Assume Item are structured following way:
               <item name="ARCHIVI DI BASE">
                          <item name="MANUTENZIONE ANAGRAFICHE">
                              <item  name="BA015"/>
                              <item  name="COS0F" />
                        
                          </item>
                       
                          <item name="BA014"/>
                      </item>
              I provided code not works in this case.

              Can you please help me in this regard?

              Thanks

              Edited by: kaleems on Jul 16, 2009 3:49 AM
              • 4. Re: Recursive program
                796262
                kaleems wrote:
                I have class called Item and now i want search item by name.
                So what's stopping you? What have you tried that didn't work?
                Assume
                Assume what? You know what they say about assuming..
                How can we achieve this?
                How do you think you can achieve this?
                using recursive program?
                I'm really curious about this one. What exactly do you think a "recursive program" is , exactly?
                If so,some one Please send me the code.
                You're going to have to work a little harder than that!
                • 5. Re: Recursive program
                  807588
                  Kevin,

                  I'm newbie.Please don't beak the thread.Please answer my Mele post reply,if you can.

                  Thanks
                  • 6. Re: Recursive program
                    807588
                    kaleems wrote:
                    I'm newbie.
                    Then learn. Trial and error is a good way to learn. Copying other people's code is not.
                    • 7. Re: Recursive program
                      JosAH
                      kaleems wrote:
                      I'm newbie.Please don't beak the thread.Please answer my Mele post reply,if you can.
                      Spoonfeeding is down the hall, second door on the left; this is the Java programming forum; thank you.

                      kind regards,

                      Jos
                      • 8. Re: Recursive program
                        796262
                        kaleems wrote:
                        Kevin,

                        I'm newbie.Please don't beak the thread.Please answer my Mele post reply,if you can.

                        Thanks
                        My questions were legitimate. What have you tried? How do you think you should go about approaching the problem? And why did you specify "recursive program" in regards to the solution?

                        Please answer my post reply, if you can.
                        • 9. Re: Recursive program
                          807588
                          I have created following class to store the given XML structure
                          class Item
                          {
                          
                          String name;
                          List<Item> items;
                          
                          public List<Item> getItems()
                          {
                            return items;
                          }
                          
                          }
                          XML Structure:
                          <item name="ITEM1>
                                      <item name="ITEM-A">
                                          <item  name="ITEM-A1"/>
                                          <item  name="ITEM-A2" />
                                    
                                      </item>
                                   
                                      <item name="ITEM-B"/>
                          </item>
                          {code}
                          
                          Now,I am trying to search  "ITEM-A2" using the following code
                          
                          {code}public Item getItemByName(String name)
                          Item root=<getting root node here>;
                          List<Item> lst=root.getItems();
                          for(Item obj:lst)
                          {
                             if(name.equals(obj.getName()))
                            {
                              return obj;
                            }
                            else
                          {
                             return findItem(name,item);
                          }
                          
                          }
                          
                          
                           public Item findItem(String name, Item item) {
                                  if (item.getItem().size() > 0) {
                                      List<Item> lst = item.getItem();
                                      for (Item itemObj : lst) {
                          
                                          return name.equals(itemObj.getName()) ? itemObj : findItem(name, itemObj);
                                      }
                                  }
                                  return null;
                              }{code}
                          
                          But this code works only for second level item search not for third level item search.
                          Please correct me where i am doing mistake.
                          
                          Thanks                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                          • 10. Re: Recursive program
                            807588
                            You need to navigate to the child elements, getting the root elements will only display the upper items.