-1

I have a simple recursive program to find all the distinct permutation of a given string. I am using a recursive function go() to calculate the permutations and using a List<char[]> to store the permuations. After a calculation, the function checks whether the permutation already exists in the List<char[]> or not. If it doesn't, it gets added to the List<>.

Now here is the problem, the permutations are getting calculated perfectly but when i try to add them to the list, it't not adding the current value. Here is the code that does the adding.

List<char[]> l;
public void go(char[] list, int k, int m)
{
if (k == m)
{
     Console.WriteLine(list); //print to check
     l.Add(list); //add it to the list
}
// more code
}

Now for input=ABC, i get the following output..

ABC
ACB
BAC
BCA
CBA
CAB

which is correct because i am printing it directly. but when i print the list, i get.

ABC
ABC
ABC
ABC
ABC
ABC

Now i know i am making just a simple mistake somewhere but i can't figure out what. I have tried this in a bunch of different ways but nothing seems to work and i can't find any answer on the internet. Here is the complete code.

Akshay Gupta
  • 361
  • 1
  • 6
  • 17
  • @EugenePodskal can you elaborate a little, i am a newbie in c# and i don't understand how does that relate to my question? – Akshay Gupta Feb 23 '16 at 12:06
  • 1
    Though you haven't shown the code that [reproduces the issue](http://stackoverflow.com/help/mcve), it is obvious that your `char[] list` is always the same reference, thus all those `l.Add(list);` actually add the same value that will be changed by the following call. Each item in the `l` is **the same** reference, that references the same array, array that can obviously can contain only one value. – Eugene Podskal Feb 23 '16 at 12:09
  • so why does it prints the correct value in the statement `Console.WriteLine(list)` and not adding the correct value in the list in the very next statement `l.add(list);` and sorry for the badly formatted question.. although i have added a link to the complete code in the question and [here it is again](https://ideone.com/x4664R) – Akshay Gupta Feb 23 '16 at 12:20
  • Because it is correct at the exact moment - you have just calculated it and then printed it. Nothing could have changed it in between. But on the next iteration (call) it will be overwritten. – Eugene Podskal Feb 23 '16 at 12:22

1 Answers1

1

The answer is already given in the comments on your question, the problem you're experiencing is that on this row:

l.Add(list); //add it to the list

you are adding a reference to the "list" array in the "l" list, you are not making a copy of the array. So basicly you are left with a list full of references to the same object.

Try changing this

List<char[]> l;

to this

List<string> l;

and switching this code

l.Add(list); //add it to the list

for this

l.Add(new string(list)); //add it to the list

And I Think you will receive the expected result. The difference is that we have converted the array (that is a reference type) into a string (that behaves like a value type)

Shazi
  • 1,490
  • 1
  • 10
  • 22