0

I have a method which looks like so...

public static ArrayList<Integer> bettingCycle(
    ArrayList<Integer> playersRemaining, 
    String[][] playerHands,
    ArrayList<Integer> bets, 
    int totalPot) { 

    //irrelevant code
    return playersRemaining;
}

My method successfully returns the adjusted ArrayList playersRemaining, but does not alter any of the passed in variables (playerHands, bets, totalPot).

In order for my program to work, I need to somehow adjust the playerHands, bets and totalPot passed in variables. No matter what I've tried, I still can't get anything, other than the returned playersRemaining to change.

To help explain what I desire to be accomplished, here's an example with comments attached to help explain my problem.

public static ArrayList<Integer> playHand(
   ArrayList<Integer> initialPlayers, 
   String[][] initialPlayerHands, 
   ArrayList<Integer> preliminaryBets //assumed to be initially 0's
   int initialPot //also assumed to be initially 0
) 
  {

   //fancy code that allows for initial betting cycle (so that the values 
   //passed into the bettingCycle method are not null, or invalid
   //It is here that the values are initially changed before passing to the 
   //bettingCycle 

   if (!allPlayersCalled) {
       bettingCycle(updatedPlayersRemaining, updatedPlayerHands, 
       updatedBets, updatedTotalPot); 

       //when I call this, only the 
       //updatedPlayersRemaining changes, 
       //but none of the other variables. 
       //I'd like for bettingCycle() to also change the other passed in 
       //variable, so I can use the changed even further along in the method
  }
  return updatedPlayersRemaining;
}

Any thoughts as to how to change the passed in variables, so they can be adjusted appropriately?

dbc
  • 104,963
  • 20
  • 228
  • 340
  • You can't change the passed-in variables, since Java is strictly a [pass-by-value](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) language. You'll need to redesign your bettingCycle method rather substantially I think, either to break it up into discrete functional bits, or perhaps create some sort of master state object that it can act on. – azurefrog Mar 11 '19 at 21:07
  • 2
    Java passes all its arguments by value. All but one of your arguments are references. You could modify the arrays and objects to which they point. Otherwise, another option is to group the arguments into a class -- e.g., GameState -- pass an instance of that class as an argument, and return an instance of that class as the method return value. – Andy Thomas Mar 11 '19 at 21:07
  • Actually Java is not like pass-by-value when the parameters are not primitives or immutables. İf you send mutable object parameters, you can modify them inside of your method. You have to change you totalPot parameter with a mutable object. – GaripTipici Mar 11 '19 at 21:13
  • @GaripTipici - Java is always pass-by-value. The values can be *references* or primitives, but are never objects. More detail is in https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value?rq=1 . – Andy Thomas Mar 11 '19 at 21:35
  • @AndyThomas Yeah I know. I only wanted to notice that if arguments are mutable objects, they can be modified. By this view, it likes pass-by-reference. – GaripTipici Mar 11 '19 at 21:44
  • It really looks like all these arguments should be fields of some class (`Game`), and `bettingCycle` should be its non-static method. – Reputation Farmer Mar 11 '19 at 23:05
  • @GaripTipici - Arguments are never objects in Java. They are references passed by value. In practice, teaching new Java programmers to think of arguments as objects passed by reference turns out to be not only incorrect, but harmful to understanding the language. – Andy Thomas Mar 11 '19 at 23:16

1 Answers1

0

I suggest that use a DTO class with the parameters that can be modified as attributes

public class CycleDTO {

    private String[][] playerHands,
    private List<Integer> bets, 
    private int totalPot
    List<Integer> playersRemaining, 

    // getters and setters
}

modify bettingCycle method to receive dto

public static void bettingCycle( CycleDTO  dto) {
  // "irrelevant code"
}

modify the invocation to pass the dto

...
if (!allPlayersCalled) {
    CycleDTO dto = new CycleDTO();
    dtp.setPlayersRemaininf(updatedPlayersRemaining)
    dto.setPlayerHands(updatedPlayerHands);
    dto.setBets(updatedBets);
    dto.setTotalPot(updatedTotalPot);

    bettingCycle(dto); 

...
imperezivan
  • 761
  • 5
  • 13