I am trying to take in some integers through command line arguments and then generate all the possible ordered sets from the set of numbers. The following code does that but it repeats the ordered sets.
For example, I input "2 3 4" as the command line input, the output is as follows.
2 3 4
3 2 4
4 3 2
3 2 4
2 3 4
2 4 3
4 3 2
2 4 3
2 3 4
I don't want to repeat an ordered set. For this, I tried to convert the integer array to String and print it only if it has not been repeated by using Equals method. So, when I used Array.toString(b)
, I was expecting to return a integer converted to string but it returned nine addresses.
My questions are:
- How do I get a string containing the same values of integers instead of the addresses?
- Please help me with the method I've used or, suggest any simpler method so that an ordered set of numbers, once used is not repeated once generated, so that while printing final set of ordered sets, each one of them are unique.
My code is as follows:
class Shuffler
{
void swapWith(int[] a,int m,int n,int sizeOfArray)
{
int[] b = new int[sizeOfArray];
b = a.clone();
int temp = b[n];
b[n] = b[m];
b[m]=temp;
for(int k=0;k<sizeOfArray;k++)
{System.out.print(" "+b[k]);}
System.out.println("");
}
public static void main(String[] args)
{
int a[] = new int[10];
Shuffler SH = new Shuffler();
for(int i=0;i<args.length;i++)
{
a[i] = Integer.parseInt(args[i]);
}
System.out.println("Possibilities are..");
for(int k=0;k<args.length;k++)
{
for(int j=0;j<args.length;j++)
{
SH.swapWith(a,k,j,args.length);
}
}
}
}