如何更改方法的多个传入变量,然后在Java中返回所说的变量?

时间:2019-03-11 21:01:45

标签: java

我有一个看起来像这样的方法...

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

    //irrelevant code
    return playersRemaining;
}

我的方法成功返回了调整后的ArrayListplayersRemaining,但未更改任何传入的变量(playerHands,赌注,totalPot)。

为了使我的程序正常运行,我需要以某种方式调整传入变量中的 playerHands,下注 totalPot 。不管我尝试了什么,除了返回的playersRemaining之外,我什么都没得到。

为了帮助解释我想要实现的目标,下面是一个示例,其中附带了一些注释,可以帮助解释我的问题。

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;
}

关于如何更改传入变量的任何想法,以便可以对其进行适当调整?

1 个答案:

答案 0 :(得分:0)

我建议将DTO类与可以修改为属性的参数一起使用

public class CycleDTO {

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

    // getters and setters
}

修改bettingCycle方法以接收dto

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

修改调用以传递dto

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

    bettingCycle(dto); 

...