0

I have a code that finds permutations of numbers. Here is the C++ code


void premutate(std::vector<int> nums, int index)
{
    if(index == nums.size())
    {
        ans.push_back(nums);
        return;
    }

    for(int i = index; i<nums.size(); i++)
    {
        std::swap(nums[i], nums[index]);
        premutate(nums, index+1);
        std::swap(nums[i], nums[index]);
    }
}

int main()
{

    std::vector<int> v = {1,2,3};
    premutate(v, 0);

    for(std::vector<int> v: ans)
    {
        for(int x: v)
        {

            std::cout << x << " ";
        }
        std::cout << "\n";
    }
}

Above code gives the following output -

1 2 3 
1 3 2
2 1 3
2 3 1
3 2 1
3 1 2

And this is Okay. But when I translate this to a Java Code, which goes like this -

 public ArrayList<ArrayList<Integer>> li = new ArrayList<ArrayList<Integer>>();

    public void helper(ArrayList<Integer> al, int index) {

        if(index == al.size()) {
            li.add(al);
            return;
        }

        for(int i = index; i<al.size(); i++) {
            Collections.swap(al, i ,index);
            helper(al, index+1);
            Collections.swap(al, i, index);
        }

    }

    public ArrayList<ArrayList<Integer>> permute(ArrayList<Integer> A) {

        helper(A, 0);
        return li;

    }

My output is like this -

[[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3]]

Please ignore the formatting of output, but see how the list of numbers is same in in Java, but not in C++ output

I have been coding in C++ for past 3 years, and I am new to Java. What am I doing wrong ???

  • `li` is an ArrayList and you are printing `li` directly I assume. To get the output like in c++ you need to loop through the elements and print them. Can you show how you are outputting them? – Saatvik Ramani Jul 17 '21 at 04:21
  • 1
    Java passes objects by references, `std::vector nums` in C++ is a copied object. You do not modify the original `std::vector v` as you are expecting. The C++ codes does not work. What is `ans`? – 273K Jul 17 '21 at 04:24
  • See duplicate question "[Why does my ArrayList contain N copies of the last item added to the list?](https://stackoverflow.com/questions/19843506/why-does-my-arraylist-contain-n-copies-of-the-last-item-added-to-the-list)" for why it's not working in Java ("Adding the Same Object"). *Solution:* Replace `li.add(al);` with `li.add(new ArrayList<>(al));` – Andreas Jul 17 '21 at 04:37
  • @S.M. In C++ code, I suspected the same thing and tried passing by reference, the output did not change. – Chitransh Saxena Jul 17 '21 at 04:57
  • @Andreas Whoa, this worked. I went through the duplicate question link, but can you please explain once again what did I miss? – Chitransh Saxena Jul 17 '21 at 04:58
  • *But when I translate this to a Java Code* -- This is what happens when you try and mimic Java code using C++ techniques and doing a line-by-line translation. This: `void premutate(std::vector nums, int index)` as mentioned earlier, works on a copy of `nums`, and not the original vector that was passed, thus changes to `nums` disappear as soon as the function returns. This is different than Java, where a reference to the original "vector" is used within the function, thus the changes stick when the function returns. – PaulMcKenzie Jul 17 '21 at 06:12
  • @ChitranshSaxena -- *what did I miss?* -- The fundamental of C++ makes copies, Java doesn't. Issuing a `vector::push_back(al)` in C++ generates a new copy in the vector. Doing this in Java: `li.add(al);` creates another *reference* to the same `al`, not a brand new copy of `al`. In Java, if you want to add a brand new, detached, copy of `al` to store in the container, you have to actually create it yourself, copy the data to it, and add the new item (or add the item, get the reference to the newly added item, and adjust it). – PaulMcKenzie Jul 17 '21 at 06:16

0 Answers0