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. Using Array LIst

Using Array LIst

Scheduled Pinned Locked Moved C#
data-structureshelpquestion
4 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

    I wrote recursive function where i need to pass array of integer and output a collection of arraylist in main Below is how i did but how can i change my program to out put recive a arraylist and print out in main Please help Thankyou public class PermutationsLex { [STAThread] static void Main(String [] args) { int [] a=new int[] {3,4,2}; ArrayList sa= Permute(a,0,a.Length-1); Console.ReadLine(); } static ArrayList Permute(int[] a, int start, int finish) { System.Collections.ArrayList permutationsList= new System.Collections.ArrayList(); if (start == finish) { // for (int i = 0; i <= finish; i++) // { // Console.Write(a[i] + " " ); // } // Console.WriteLine(""); } else { for ( int i = start; i <= finish; i++) { swap(a,start,i); Permute(a, start+1, finish); swap(a,start,i); } } } public static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; }

    J 1 Reply Last reply
    0
    • A aynka2000

      I wrote recursive function where i need to pass array of integer and output a collection of arraylist in main Below is how i did but how can i change my program to out put recive a arraylist and print out in main Please help Thankyou public class PermutationsLex { [STAThread] static void Main(String [] args) { int [] a=new int[] {3,4,2}; ArrayList sa= Permute(a,0,a.Length-1); Console.ReadLine(); } static ArrayList Permute(int[] a, int start, int finish) { System.Collections.ArrayList permutationsList= new System.Collections.ArrayList(); if (start == finish) { // for (int i = 0; i <= finish; i++) // { // Console.Write(a[i] + " " ); // } // Console.WriteLine(""); } else { for ( int i = start; i <= finish; i++) { swap(a,start,i); Permute(a, start+1, finish); swap(a,start,i); } } } public static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; }

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      aynka2000 wrote:

      how can i change my program to out put recive a arraylist and print out in main

      I don't understand what you mean by "out put receive a arraylist". Could you clarify? Also, when you post code snippets in this forum, surround the code with <pre> tags. It makes it easier for people to look at your code and answer your question.

      Tech, life, family, faith: Give me a visit. I'm currently blogging about: And in this corner, the Party of Allah The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

      A 1 Reply Last reply
      0
      • J Judah Gabriel Himango

        aynka2000 wrote:

        how can i change my program to out put recive a arraylist and print out in main

        I don't understand what you mean by "out put receive a arraylist". Could you clarify? Also, when you post code snippets in this forum, surround the code with <pre> tags. It makes it easier for people to look at your code and answer your question.

        Tech, life, family, faith: Give me a visit. I'm currently blogging about: And in this corner, the Party of Allah The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

        A Offline
        A Offline
        aynka2000
        wrote on last edited by
        #3

        Currently in my function (static void Permute(int[] a, int start, int finish)) all the values in the permutation are printed recursively in a loop I want to change the program so that my function Permute returns an array list of int [] arrays to the Main function in the program static void Main(String [] args) { int [] a=new int[] {3,4,2}; ArrayList sa= Permute(a,0,a.Length-1);=Pass the Array List Console.ReadLine(); Please help Thankyou

        public class PermutationsLex
        {

        [STAThread]
        static void Main(String [] args)
        {
        int [] a=new int[] {3,4,2};

        ArrayList sa= Permute(a,0,a.Length-1);

        Console.ReadLine();

        }

        static void Permute(int[] a, int start, int finish)
        {

        if (start == finish)
        {
        for (int i = 0; i <= finish; i++)
        {
        Console.Write(a[i] + " " );
        }
        Console.WriteLine("");

        }
        else
        {

        for ( int i = start; i <= finish; i++)
        {

        swap(a,start,i);

        Permute(a, start+1, finish);

        swap(a,start,i);

        }

        }

        }

        public static void swap(int[] a, int i, int j)
        {

        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;

        }

        N 1 Reply Last reply
        0
        • A aynka2000

          Currently in my function (static void Permute(int[] a, int start, int finish)) all the values in the permutation are printed recursively in a loop I want to change the program so that my function Permute returns an array list of int [] arrays to the Main function in the program static void Main(String [] args) { int [] a=new int[] {3,4,2}; ArrayList sa= Permute(a,0,a.Length-1);=Pass the Array List Console.ReadLine(); Please help Thankyou

          public class PermutationsLex
          {

          [STAThread]
          static void Main(String [] args)
          {
          int [] a=new int[] {3,4,2};

          ArrayList sa= Permute(a,0,a.Length-1);

          Console.ReadLine();

          }

          static void Permute(int[] a, int start, int finish)
          {

          if (start == finish)
          {
          for (int i = 0; i <= finish; i++)
          {
          Console.Write(a[i] + " " );
          }
          Console.WriteLine("");

          }
          else
          {

          for ( int i = start; i <= finish; i++)
          {

          swap(a,start,i);

          Permute(a, start+1, finish);

          swap(a,start,i);

          }

          }

          }

          public static void swap(int[] a, int i, int j)
          {

          int temp = a[i];
          a[i] = a[j];
          a[j] = temp;

          }

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

          Hello I don't think I get what you are trying to do!! 1- If you want to make an ArraytList from your array simply

          ArrayList temp = new ArrayList(a);

          2- If you want to swap your values you can do the same in the ArrayList instead of the array, and you don't have to make an entire method for that!! simple make a foreach loop to loop through your ArrayList in the premute method. 3- If you ant to return the ArrayList simply

          public ArrayList Premute(int[] a)
          {
          ArrayList MyArrayList = new ArrayList(a);
          //Do something with MyArrayList
          return MyArrayList;
          }

          Yet I don't understand you logic in swapping!! I hope that was close enough.

          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