我有一个看起来像这样的方法...public static ArrayList<Integer> bettingCycle( ArrayList<Integer> playersRemaining, String[][] playerHands, ArrayList<Integer> bets, int totalPot) { //irrelevant code return playersRemaining;}我的方法成功返回调整后的 ArrayList 玩家恢复,但不会更改任何传入的变量(玩家手牌、投注、总池)。为了使我的程序正常工作,我需要以某种方式调整玩家手牌,赌注和变量中传递的总池。无论我尝试了什么,我仍然无法得到任何东西,除了返回的改变。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 回答

犯罪嫌疑人X
TA贡献2080条经验 获得超4个赞
我建议使用DTO类,其中包含可以作为属性修改的参数
public class CycleDTO {
private String[][] playerHands,
private List<Integer> bets,
private int totalPot
List<Integer> playersRemaining,
// getters and setters
}
修改投注周期方法以接收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);
...
添加回答
举报
0/150
提交
取消