Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to use array list in a recursive function:(pls help)

How to use array list in a recursive function:(pls help)

Scheduled Pinned Locked Moved C#
questiondata-structureshelptutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aynka2000
    wrote on last edited by
    #1

    static ArrayList Permute(int[] a,int start,int finish)) { if(start!=finsh) Permute(a,start+1,finish) } In this recursive function I need to add the int [] a(array) collection to array list and return the array list how can I do that? (simply how to add values to a array list in a recursive function and the return the array list with all values) like ( arraylist alist =new arraylist(); alist .add(1); alist .add(2); then return alist but this wont work in recursive function like above due each time alist get reinitialized

    G N 2 Replies Last reply
    0
    • A aynka2000

      static ArrayList Permute(int[] a,int start,int finish)) { if(start!=finsh) Permute(a,start+1,finish) } In this recursive function I need to add the int [] a(array) collection to array list and return the array list how can I do that? (simply how to add values to a array list in a recursive function and the return the array list with all values) like ( arraylist alist =new arraylist(); alist .add(1); alist .add(2); then return alist but this wont work in recursive function like above due each time alist get reinitialized

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      I think that you don't want to do what you are asking for at all. I think that you want to create the ArrayList once, and use it in the recursive function. Create the ArrayList before you call the recursive function, and pass the reference to the ArrayList as a parameter:

      static void Permute(int[] a, int start, int finish, ArrayList results)) {
      results.Add(a);
      if (start != finish) {
      Permute(a, start + 1, finish, results);
      }
      }

      --- b { font-weight: normal; }

      1 Reply Last reply
      0
      • A aynka2000

        static ArrayList Permute(int[] a,int start,int finish)) { if(start!=finsh) Permute(a,start+1,finish) } In this recursive function I need to add the int [] a(array) collection to array list and return the array list how can I do that? (simply how to add values to a array list in a recursive function and the return the array list with all values) like ( arraylist alist =new arraylist(); alist .add(1); alist .add(2); then return alist but this wont work in recursive function like above due each time alist get reinitialized

        N Offline
        N Offline
        Nader Elshehabi
        wrote on last edited by
        #3

        Hello Again?!! Why do you insist on making it in a recursive call?? One line of code is enough:

        ArrayList MyArrayList = new ArrayList(a);

        If you insist, you can check if the ArrayList is already initialized before reinitializing it:

        ArrayList alist;
        static ArrayList Permute(int[] a, int start, int finish)
        {
        if(alist.Count == 0)
        alist = new ArrayList()
        alist.Add(1);
        alist.Add(2);
        if(start != finish)
        Permute(a, start+1, finish)
        return alist;
        }

        but I'm sure there is a much better way of doing whatever you are trying to do!! Please post more details about what you are trying to do, perhaps we code suggest a better approach.

        Regards:rose:

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups