Saving Data in a recursive function to a listWhat is tail recursion?How do I check if a list is empty?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow do you split a list into evenly sized chunks?Getting the last element of a list in PythonHow to make a flat list out of list of lists?How do I get the number of elements in a list in Python?How do I concatenate two lists in Python?How to clone or copy a list?

Is there a RAID 0 Equivalent for RAM?

C++ copy constructor called at return

Microchip documentation does not label CAN buss pins on micro controller pinout diagram

What is the highest possible scrabble score for placing a single tile

Can you use Vicious Mockery to win an argument or gain favours?

Why does AES have exactly 10 rounds for a 128-bit key, 12 for 192 bits and 14 for a 256-bit key size?

What is the difference between lands and mana?

Do we have to expect a queue for the shuttle from Watford Junction to Harry Potter Studio?

How to explain what's wrong with this application of the chain rule?

Stack Interview Code methods made from class Node and Smart Pointers

Why does Carol not get rid of the Kree symbol on her suit when she changes its colours?

Multiplicative persistence

Can I say "fingers" when referring to toes?

Does an advisor owe his/her student anything? Will an advisor keep a PhD student only out of pity?

What does Apple's new App Store requirement mean

Has any country ever had 2 former presidents in jail simultaneously?

What kind of floor tile is this?

How do you make your own symbol when Detexify fails?

Why does this expression simplify as such?

C++ check if statement can be evaluated constexpr

What is the English pronunciation of "pain au chocolat"?

Is there a nicer/politer/more positive alternative for "negates"?

Giving feedback to someone without sounding prejudiced

Biological Blimps: Propulsion



Saving Data in a recursive function to a list


What is tail recursion?How do I check if a list is empty?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow do you split a list into evenly sized chunks?Getting the last element of a list in PythonHow to make a flat list out of list of lists?How do I get the number of elements in a list in Python?How do I concatenate two lists in Python?How to clone or copy a list?













1















I'm working on a function which breaks down the given input into denominations using a recursive call.



At each step it recurses into two variants:



  1. Continue with the current coin: Add it to the list and recurse.

  2. Switch to the next coin: Increment coin pos and recurse.

In addition to printing out the combination of denominations captured in the list when remaining == 0, I intend to capture the value of that list and return it from the function.



Here's the code:



 static final int[] DENOMINATIONS = 9,5,3;

private static void change(int remaining, List<Integer> coins, int pos)

if (remaining == 0)

// This correctly prints the desired output.
// I want to return that exact value from the function.
System.out.println(coins);

else
if (remaining >= DENOMINATIONS[pos])
coins.add(DENOMINATIONS[pos]);
another.addAll(coins);
change(remaining - DENOMINATIONS[pos], coins, pos);
coins.remove(coins.size() - 1);

if (pos + 1 < DENOMINATIONS.length)
change(remaining, coins, pos + 1);


}


public static List<Integer> denominations(int amount)
List<Integer> result = new ArrayList<Integer>();
List<Integer> another = new ArrayList<Integer>();
change(amount, result, another ,0);
System.out.println(another.size());
return another;


public static void main(String[] args)
List<Integer> list = denominations(13);
System.out.println(list);



Output : [5, 5, 3]










share|improve this question
























  • Is this based on the SICP counting-change example?

    – Jodast
    Mar 7 at 5:55






  • 1





    I suppose it is.

    – USQ91
    Mar 7 at 6:23















1















I'm working on a function which breaks down the given input into denominations using a recursive call.



At each step it recurses into two variants:



  1. Continue with the current coin: Add it to the list and recurse.

  2. Switch to the next coin: Increment coin pos and recurse.

In addition to printing out the combination of denominations captured in the list when remaining == 0, I intend to capture the value of that list and return it from the function.



Here's the code:



 static final int[] DENOMINATIONS = 9,5,3;

private static void change(int remaining, List<Integer> coins, int pos)

if (remaining == 0)

// This correctly prints the desired output.
// I want to return that exact value from the function.
System.out.println(coins);

else
if (remaining >= DENOMINATIONS[pos])
coins.add(DENOMINATIONS[pos]);
another.addAll(coins);
change(remaining - DENOMINATIONS[pos], coins, pos);
coins.remove(coins.size() - 1);

if (pos + 1 < DENOMINATIONS.length)
change(remaining, coins, pos + 1);


}


public static List<Integer> denominations(int amount)
List<Integer> result = new ArrayList<Integer>();
List<Integer> another = new ArrayList<Integer>();
change(amount, result, another ,0);
System.out.println(another.size());
return another;


public static void main(String[] args)
List<Integer> list = denominations(13);
System.out.println(list);



Output : [5, 5, 3]










share|improve this question
























  • Is this based on the SICP counting-change example?

    – Jodast
    Mar 7 at 5:55






  • 1





    I suppose it is.

    – USQ91
    Mar 7 at 6:23













1












1








1








I'm working on a function which breaks down the given input into denominations using a recursive call.



At each step it recurses into two variants:



  1. Continue with the current coin: Add it to the list and recurse.

  2. Switch to the next coin: Increment coin pos and recurse.

In addition to printing out the combination of denominations captured in the list when remaining == 0, I intend to capture the value of that list and return it from the function.



Here's the code:



 static final int[] DENOMINATIONS = 9,5,3;

private static void change(int remaining, List<Integer> coins, int pos)

if (remaining == 0)

// This correctly prints the desired output.
// I want to return that exact value from the function.
System.out.println(coins);

else
if (remaining >= DENOMINATIONS[pos])
coins.add(DENOMINATIONS[pos]);
another.addAll(coins);
change(remaining - DENOMINATIONS[pos], coins, pos);
coins.remove(coins.size() - 1);

if (pos + 1 < DENOMINATIONS.length)
change(remaining, coins, pos + 1);


}


public static List<Integer> denominations(int amount)
List<Integer> result = new ArrayList<Integer>();
List<Integer> another = new ArrayList<Integer>();
change(amount, result, another ,0);
System.out.println(another.size());
return another;


public static void main(String[] args)
List<Integer> list = denominations(13);
System.out.println(list);



Output : [5, 5, 3]










share|improve this question
















I'm working on a function which breaks down the given input into denominations using a recursive call.



At each step it recurses into two variants:



  1. Continue with the current coin: Add it to the list and recurse.

  2. Switch to the next coin: Increment coin pos and recurse.

In addition to printing out the combination of denominations captured in the list when remaining == 0, I intend to capture the value of that list and return it from the function.



Here's the code:



 static final int[] DENOMINATIONS = 9,5,3;

private static void change(int remaining, List<Integer> coins, int pos)

if (remaining == 0)

// This correctly prints the desired output.
// I want to return that exact value from the function.
System.out.println(coins);

else
if (remaining >= DENOMINATIONS[pos])
coins.add(DENOMINATIONS[pos]);
another.addAll(coins);
change(remaining - DENOMINATIONS[pos], coins, pos);
coins.remove(coins.size() - 1);

if (pos + 1 < DENOMINATIONS.length)
change(remaining, coins, pos + 1);


}


public static List<Integer> denominations(int amount)
List<Integer> result = new ArrayList<Integer>();
List<Integer> another = new ArrayList<Integer>();
change(amount, result, another ,0);
System.out.println(another.size());
return another;


public static void main(String[] args)
List<Integer> list = denominations(13);
System.out.println(list);



Output : [5, 5, 3]







java algorithm list recursion coin-change






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 6:04







USQ91

















asked Mar 7 at 5:06









USQ91USQ91

7010




7010












  • Is this based on the SICP counting-change example?

    – Jodast
    Mar 7 at 5:55






  • 1





    I suppose it is.

    – USQ91
    Mar 7 at 6:23

















  • Is this based on the SICP counting-change example?

    – Jodast
    Mar 7 at 5:55






  • 1





    I suppose it is.

    – USQ91
    Mar 7 at 6:23
















Is this based on the SICP counting-change example?

– Jodast
Mar 7 at 5:55





Is this based on the SICP counting-change example?

– Jodast
Mar 7 at 5:55




1




1





I suppose it is.

– USQ91
Mar 7 at 6:23





I suppose it is.

– USQ91
Mar 7 at 6:23












3 Answers
3






active

oldest

votes


















1














You seems to assume that java is passed by reference which is not true. Java methods are passed by value.



I have updated your code:



change method:



private static List<Integer> change(int remaining, List<Integer> coins, int pos) pos >= DENOMINATIONS.length) // check if position is invalid
return new ArrayList<>(); // return an empty list


if (remaining == DENOMINATIONS[pos]) // check if remaining is equal to denominations[pos]
coins.add(DENOMINATIONS[pos]); // add the denominations to the coins result
return coins; // return the result
else if (remaining > DENOMINATIONS[pos]) // check if remaining is greater than denominations[pos]
coins.add(DENOMINATIONS[pos]);// add the possible denominations to the coins result
remaining = remaining - DENOMINATIONS[pos]; // calculate the new remaining
if (remaining >= DENOMINATIONS[pos]) // check if remaining is greater than or equal to denominations[pos]
return change(remaining, coins, pos); // stick to this position
else
return change(remaining, coins, pos + 1); // increment pos to go to the lower denominations

else if (remaining < DENOMINATIONS[pos]) // check if remaining is lesser than denominations[pos]
if (coins.isEmpty()) // if coins is empty then go to the next denomination
return change(remaining, coins, pos + 1);
else
coins.remove(coins.size() - 1); // remove the previous denomination
return change(remaining + DENOMINATIONS[pos - 1], coins, pos); // go back to the previous remaining and // try this DENOMINATIONS[pos]


return coins;



denominations method:



public static List<Integer> denominations(int amount) 
return change(amount, new ArrayList<Integer>(), 0);



INPUT : 13
OUTPUT : [5, 5, 3]






share|improve this answer

























  • Thank you Mark. It works well. I would highly appreciate if you could briefly explain the need for denominations method here. I'm trying to understand the underlying concepts.

    – USQ91
    Mar 12 at 1:30











  • I just tried to fit my solutions to your solution. The denominations method is not needed. You can simply call the change method into your main method.

    – Mark
    Mar 12 at 1:33











  • The functions returns an empty list for some input values like 21 , 30 . Any idea why?

    – USQ91
    Mar 12 at 12:09


















1














You would have to add return coins; at the and of change method, but you can keep it the way you have it. Returning and changing an array is a code smell as the method both operates on object (modifies it) and returns a result.



To make it work, you can define your denomination method as follows:



public static List<Integer> denominations(int amount) 
List<Integer> result = new ArrayList<Integer>();
change(amount, result, 0);
return result;



Edit:



The list is empty because the only place it's changed is here:



coins.add(DENOMINATIONS[pos]);
change(remaining - DENOMINATIONS[pos], coins, pos);
coins.remove(coins.size() - 1);


Where an element is added and removed. It's what you have written that makes it empty:)



Edit2:



I would suggest passing around a second object, that would be a copy of the list you would like to return and is not modified.






share|improve this answer

























  • I've tried that but it still returns an empty list. I modified my code according to your suggestions

    – USQ91
    Mar 7 at 5:26











  • Yes you're right . And that because the items are being removed from the list. It's the value of list i want to return exactly when if(remaining == 0). Where modifications do you suggest?

    – USQ91
    Mar 7 at 5:30











  • I have added a suggestion;)

    – Andronicus
    Mar 7 at 5:33


















0














change should return boolean that mean whether has been found an answer.



So change body looks like this:



if (remaining == 0) 
return true;

...
if (change(...)) return true;
...
return false; // It's last line of body





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55036453%2fsaving-data-in-a-recursive-function-to-a-list%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You seems to assume that java is passed by reference which is not true. Java methods are passed by value.



    I have updated your code:



    change method:



    private static List<Integer> change(int remaining, List<Integer> coins, int pos) pos >= DENOMINATIONS.length) // check if position is invalid
    return new ArrayList<>(); // return an empty list


    if (remaining == DENOMINATIONS[pos]) // check if remaining is equal to denominations[pos]
    coins.add(DENOMINATIONS[pos]); // add the denominations to the coins result
    return coins; // return the result
    else if (remaining > DENOMINATIONS[pos]) // check if remaining is greater than denominations[pos]
    coins.add(DENOMINATIONS[pos]);// add the possible denominations to the coins result
    remaining = remaining - DENOMINATIONS[pos]; // calculate the new remaining
    if (remaining >= DENOMINATIONS[pos]) // check if remaining is greater than or equal to denominations[pos]
    return change(remaining, coins, pos); // stick to this position
    else
    return change(remaining, coins, pos + 1); // increment pos to go to the lower denominations

    else if (remaining < DENOMINATIONS[pos]) // check if remaining is lesser than denominations[pos]
    if (coins.isEmpty()) // if coins is empty then go to the next denomination
    return change(remaining, coins, pos + 1);
    else
    coins.remove(coins.size() - 1); // remove the previous denomination
    return change(remaining + DENOMINATIONS[pos - 1], coins, pos); // go back to the previous remaining and // try this DENOMINATIONS[pos]


    return coins;



    denominations method:



    public static List<Integer> denominations(int amount) 
    return change(amount, new ArrayList<Integer>(), 0);



    INPUT : 13
    OUTPUT : [5, 5, 3]






    share|improve this answer

























    • Thank you Mark. It works well. I would highly appreciate if you could briefly explain the need for denominations method here. I'm trying to understand the underlying concepts.

      – USQ91
      Mar 12 at 1:30











    • I just tried to fit my solutions to your solution. The denominations method is not needed. You can simply call the change method into your main method.

      – Mark
      Mar 12 at 1:33











    • The functions returns an empty list for some input values like 21 , 30 . Any idea why?

      – USQ91
      Mar 12 at 12:09















    1














    You seems to assume that java is passed by reference which is not true. Java methods are passed by value.



    I have updated your code:



    change method:



    private static List<Integer> change(int remaining, List<Integer> coins, int pos) pos >= DENOMINATIONS.length) // check if position is invalid
    return new ArrayList<>(); // return an empty list


    if (remaining == DENOMINATIONS[pos]) // check if remaining is equal to denominations[pos]
    coins.add(DENOMINATIONS[pos]); // add the denominations to the coins result
    return coins; // return the result
    else if (remaining > DENOMINATIONS[pos]) // check if remaining is greater than denominations[pos]
    coins.add(DENOMINATIONS[pos]);// add the possible denominations to the coins result
    remaining = remaining - DENOMINATIONS[pos]; // calculate the new remaining
    if (remaining >= DENOMINATIONS[pos]) // check if remaining is greater than or equal to denominations[pos]
    return change(remaining, coins, pos); // stick to this position
    else
    return change(remaining, coins, pos + 1); // increment pos to go to the lower denominations

    else if (remaining < DENOMINATIONS[pos]) // check if remaining is lesser than denominations[pos]
    if (coins.isEmpty()) // if coins is empty then go to the next denomination
    return change(remaining, coins, pos + 1);
    else
    coins.remove(coins.size() - 1); // remove the previous denomination
    return change(remaining + DENOMINATIONS[pos - 1], coins, pos); // go back to the previous remaining and // try this DENOMINATIONS[pos]


    return coins;



    denominations method:



    public static List<Integer> denominations(int amount) 
    return change(amount, new ArrayList<Integer>(), 0);



    INPUT : 13
    OUTPUT : [5, 5, 3]






    share|improve this answer

























    • Thank you Mark. It works well. I would highly appreciate if you could briefly explain the need for denominations method here. I'm trying to understand the underlying concepts.

      – USQ91
      Mar 12 at 1:30











    • I just tried to fit my solutions to your solution. The denominations method is not needed. You can simply call the change method into your main method.

      – Mark
      Mar 12 at 1:33











    • The functions returns an empty list for some input values like 21 , 30 . Any idea why?

      – USQ91
      Mar 12 at 12:09













    1












    1








    1







    You seems to assume that java is passed by reference which is not true. Java methods are passed by value.



    I have updated your code:



    change method:



    private static List<Integer> change(int remaining, List<Integer> coins, int pos) pos >= DENOMINATIONS.length) // check if position is invalid
    return new ArrayList<>(); // return an empty list


    if (remaining == DENOMINATIONS[pos]) // check if remaining is equal to denominations[pos]
    coins.add(DENOMINATIONS[pos]); // add the denominations to the coins result
    return coins; // return the result
    else if (remaining > DENOMINATIONS[pos]) // check if remaining is greater than denominations[pos]
    coins.add(DENOMINATIONS[pos]);// add the possible denominations to the coins result
    remaining = remaining - DENOMINATIONS[pos]; // calculate the new remaining
    if (remaining >= DENOMINATIONS[pos]) // check if remaining is greater than or equal to denominations[pos]
    return change(remaining, coins, pos); // stick to this position
    else
    return change(remaining, coins, pos + 1); // increment pos to go to the lower denominations

    else if (remaining < DENOMINATIONS[pos]) // check if remaining is lesser than denominations[pos]
    if (coins.isEmpty()) // if coins is empty then go to the next denomination
    return change(remaining, coins, pos + 1);
    else
    coins.remove(coins.size() - 1); // remove the previous denomination
    return change(remaining + DENOMINATIONS[pos - 1], coins, pos); // go back to the previous remaining and // try this DENOMINATIONS[pos]


    return coins;



    denominations method:



    public static List<Integer> denominations(int amount) 
    return change(amount, new ArrayList<Integer>(), 0);



    INPUT : 13
    OUTPUT : [5, 5, 3]






    share|improve this answer















    You seems to assume that java is passed by reference which is not true. Java methods are passed by value.



    I have updated your code:



    change method:



    private static List<Integer> change(int remaining, List<Integer> coins, int pos) pos >= DENOMINATIONS.length) // check if position is invalid
    return new ArrayList<>(); // return an empty list


    if (remaining == DENOMINATIONS[pos]) // check if remaining is equal to denominations[pos]
    coins.add(DENOMINATIONS[pos]); // add the denominations to the coins result
    return coins; // return the result
    else if (remaining > DENOMINATIONS[pos]) // check if remaining is greater than denominations[pos]
    coins.add(DENOMINATIONS[pos]);// add the possible denominations to the coins result
    remaining = remaining - DENOMINATIONS[pos]; // calculate the new remaining
    if (remaining >= DENOMINATIONS[pos]) // check if remaining is greater than or equal to denominations[pos]
    return change(remaining, coins, pos); // stick to this position
    else
    return change(remaining, coins, pos + 1); // increment pos to go to the lower denominations

    else if (remaining < DENOMINATIONS[pos]) // check if remaining is lesser than denominations[pos]
    if (coins.isEmpty()) // if coins is empty then go to the next denomination
    return change(remaining, coins, pos + 1);
    else
    coins.remove(coins.size() - 1); // remove the previous denomination
    return change(remaining + DENOMINATIONS[pos - 1], coins, pos); // go back to the previous remaining and // try this DENOMINATIONS[pos]


    return coins;



    denominations method:



    public static List<Integer> denominations(int amount) 
    return change(amount, new ArrayList<Integer>(), 0);



    INPUT : 13
    OUTPUT : [5, 5, 3]







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 7 at 6:09

























    answered Mar 7 at 5:32









    MarkMark

    869218




    869218












    • Thank you Mark. It works well. I would highly appreciate if you could briefly explain the need for denominations method here. I'm trying to understand the underlying concepts.

      – USQ91
      Mar 12 at 1:30











    • I just tried to fit my solutions to your solution. The denominations method is not needed. You can simply call the change method into your main method.

      – Mark
      Mar 12 at 1:33











    • The functions returns an empty list for some input values like 21 , 30 . Any idea why?

      – USQ91
      Mar 12 at 12:09

















    • Thank you Mark. It works well. I would highly appreciate if you could briefly explain the need for denominations method here. I'm trying to understand the underlying concepts.

      – USQ91
      Mar 12 at 1:30











    • I just tried to fit my solutions to your solution. The denominations method is not needed. You can simply call the change method into your main method.

      – Mark
      Mar 12 at 1:33











    • The functions returns an empty list for some input values like 21 , 30 . Any idea why?

      – USQ91
      Mar 12 at 12:09
















    Thank you Mark. It works well. I would highly appreciate if you could briefly explain the need for denominations method here. I'm trying to understand the underlying concepts.

    – USQ91
    Mar 12 at 1:30





    Thank you Mark. It works well. I would highly appreciate if you could briefly explain the need for denominations method here. I'm trying to understand the underlying concepts.

    – USQ91
    Mar 12 at 1:30













    I just tried to fit my solutions to your solution. The denominations method is not needed. You can simply call the change method into your main method.

    – Mark
    Mar 12 at 1:33





    I just tried to fit my solutions to your solution. The denominations method is not needed. You can simply call the change method into your main method.

    – Mark
    Mar 12 at 1:33













    The functions returns an empty list for some input values like 21 , 30 . Any idea why?

    – USQ91
    Mar 12 at 12:09





    The functions returns an empty list for some input values like 21 , 30 . Any idea why?

    – USQ91
    Mar 12 at 12:09













    1














    You would have to add return coins; at the and of change method, but you can keep it the way you have it. Returning and changing an array is a code smell as the method both operates on object (modifies it) and returns a result.



    To make it work, you can define your denomination method as follows:



    public static List<Integer> denominations(int amount) 
    List<Integer> result = new ArrayList<Integer>();
    change(amount, result, 0);
    return result;



    Edit:



    The list is empty because the only place it's changed is here:



    coins.add(DENOMINATIONS[pos]);
    change(remaining - DENOMINATIONS[pos], coins, pos);
    coins.remove(coins.size() - 1);


    Where an element is added and removed. It's what you have written that makes it empty:)



    Edit2:



    I would suggest passing around a second object, that would be a copy of the list you would like to return and is not modified.






    share|improve this answer

























    • I've tried that but it still returns an empty list. I modified my code according to your suggestions

      – USQ91
      Mar 7 at 5:26











    • Yes you're right . And that because the items are being removed from the list. It's the value of list i want to return exactly when if(remaining == 0). Where modifications do you suggest?

      – USQ91
      Mar 7 at 5:30











    • I have added a suggestion;)

      – Andronicus
      Mar 7 at 5:33















    1














    You would have to add return coins; at the and of change method, but you can keep it the way you have it. Returning and changing an array is a code smell as the method both operates on object (modifies it) and returns a result.



    To make it work, you can define your denomination method as follows:



    public static List<Integer> denominations(int amount) 
    List<Integer> result = new ArrayList<Integer>();
    change(amount, result, 0);
    return result;



    Edit:



    The list is empty because the only place it's changed is here:



    coins.add(DENOMINATIONS[pos]);
    change(remaining - DENOMINATIONS[pos], coins, pos);
    coins.remove(coins.size() - 1);


    Where an element is added and removed. It's what you have written that makes it empty:)



    Edit2:



    I would suggest passing around a second object, that would be a copy of the list you would like to return and is not modified.






    share|improve this answer

























    • I've tried that but it still returns an empty list. I modified my code according to your suggestions

      – USQ91
      Mar 7 at 5:26











    • Yes you're right . And that because the items are being removed from the list. It's the value of list i want to return exactly when if(remaining == 0). Where modifications do you suggest?

      – USQ91
      Mar 7 at 5:30











    • I have added a suggestion;)

      – Andronicus
      Mar 7 at 5:33













    1












    1








    1







    You would have to add return coins; at the and of change method, but you can keep it the way you have it. Returning and changing an array is a code smell as the method both operates on object (modifies it) and returns a result.



    To make it work, you can define your denomination method as follows:



    public static List<Integer> denominations(int amount) 
    List<Integer> result = new ArrayList<Integer>();
    change(amount, result, 0);
    return result;



    Edit:



    The list is empty because the only place it's changed is here:



    coins.add(DENOMINATIONS[pos]);
    change(remaining - DENOMINATIONS[pos], coins, pos);
    coins.remove(coins.size() - 1);


    Where an element is added and removed. It's what you have written that makes it empty:)



    Edit2:



    I would suggest passing around a second object, that would be a copy of the list you would like to return and is not modified.






    share|improve this answer















    You would have to add return coins; at the and of change method, but you can keep it the way you have it. Returning and changing an array is a code smell as the method both operates on object (modifies it) and returns a result.



    To make it work, you can define your denomination method as follows:



    public static List<Integer> denominations(int amount) 
    List<Integer> result = new ArrayList<Integer>();
    change(amount, result, 0);
    return result;



    Edit:



    The list is empty because the only place it's changed is here:



    coins.add(DENOMINATIONS[pos]);
    change(remaining - DENOMINATIONS[pos], coins, pos);
    coins.remove(coins.size() - 1);


    Where an element is added and removed. It's what you have written that makes it empty:)



    Edit2:



    I would suggest passing around a second object, that would be a copy of the list you would like to return and is not modified.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 7 at 5:32

























    answered Mar 7 at 5:16









    AndronicusAndronicus

    5,33221733




    5,33221733












    • I've tried that but it still returns an empty list. I modified my code according to your suggestions

      – USQ91
      Mar 7 at 5:26











    • Yes you're right . And that because the items are being removed from the list. It's the value of list i want to return exactly when if(remaining == 0). Where modifications do you suggest?

      – USQ91
      Mar 7 at 5:30











    • I have added a suggestion;)

      – Andronicus
      Mar 7 at 5:33

















    • I've tried that but it still returns an empty list. I modified my code according to your suggestions

      – USQ91
      Mar 7 at 5:26











    • Yes you're right . And that because the items are being removed from the list. It's the value of list i want to return exactly when if(remaining == 0). Where modifications do you suggest?

      – USQ91
      Mar 7 at 5:30











    • I have added a suggestion;)

      – Andronicus
      Mar 7 at 5:33
















    I've tried that but it still returns an empty list. I modified my code according to your suggestions

    – USQ91
    Mar 7 at 5:26





    I've tried that but it still returns an empty list. I modified my code according to your suggestions

    – USQ91
    Mar 7 at 5:26













    Yes you're right . And that because the items are being removed from the list. It's the value of list i want to return exactly when if(remaining == 0). Where modifications do you suggest?

    – USQ91
    Mar 7 at 5:30





    Yes you're right . And that because the items are being removed from the list. It's the value of list i want to return exactly when if(remaining == 0). Where modifications do you suggest?

    – USQ91
    Mar 7 at 5:30













    I have added a suggestion;)

    – Andronicus
    Mar 7 at 5:33





    I have added a suggestion;)

    – Andronicus
    Mar 7 at 5:33











    0














    change should return boolean that mean whether has been found an answer.



    So change body looks like this:



    if (remaining == 0) 
    return true;

    ...
    if (change(...)) return true;
    ...
    return false; // It's last line of body





    share|improve this answer



























      0














      change should return boolean that mean whether has been found an answer.



      So change body looks like this:



      if (remaining == 0) 
      return true;

      ...
      if (change(...)) return true;
      ...
      return false; // It's last line of body





      share|improve this answer

























        0












        0








        0







        change should return boolean that mean whether has been found an answer.



        So change body looks like this:



        if (remaining == 0) 
        return true;

        ...
        if (change(...)) return true;
        ...
        return false; // It's last line of body





        share|improve this answer













        change should return boolean that mean whether has been found an answer.



        So change body looks like this:



        if (remaining == 0) 
        return true;

        ...
        if (change(...)) return true;
        ...
        return false; // It's last line of body






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 7 at 5:47









        MoreFreezeMoreFreeze

        1,21331630




        1,21331630



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55036453%2fsaving-data-in-a-recursive-function-to-a-list%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            1928 у кіно

            Захаров Федір Захарович

            Ель Греко