在我的程序中调用record()方法。所以如果用户输入 1,2,3,4,5,6,7。按照该顺序,它打印出7,6,5,4,3,2,1。并且,我尝试了增强的for-loops,forloops增量等请帮助,提前致谢。如何按照添加的顺序打印ArrayList
ArrayList<Integer> playerMoves = new ArrayList<Integer>(20);
public void record(int value) {
playerMoves.add(0, value);
if(playerMoves.size() > 20) {
playerMoves.remove(playerMoves.size() - 1);
}
}
public void displayPlayerMoves() {
int fullstopCount = 20;
System.out.print(playerMoves.size() + " moves: ");
for(int i = playerMoves.size(); i > 0; i--) {
fullstopCount--;
if(playerMoves.size() == fullstopCount) {
System.out.print(playerMoves.get(i) + ".");
} else {
System.out.print(playerMoves.get(i) + ", ");
}
}
}
'fullstopCount'为'0',那么你正在做'fullstopCount'它被初始化为什么??还需要将'add'方法更改为'playerMoves.add(value);' – Prashant
@Prashant哎呀 – BriannaXD