Using Array LIst
-
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; }
-
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; }
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
-
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
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;}
-
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;}
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: