问题
private void buildDeck(ArrayList<Card> Monsters, ArrayList<Card> Spells) {
int monstersQouta = 15;
int spellsQouta = 5;
Random r = new Random();
for (; monstersQouta > 0; monstersQouta--) {
int randomIndex = r.nextInt(monsters.size());
MonsterCard monster = (MonsterCard) monsters.get(randomIndex);
MonsterCard clone = new MonsterCard(monster.getName(),
monster.getDescription(), monster.getLevel(),
monster.getAttackPoints(), monster.getDefensePoints());
clone.setMode(monster.getMode());
clone.setHidden(monster.isHidden());
clone.setLocation(Location.DECK);
deck.add(clone);
}
I need to know why we used here clone()
and how this for loop in this code works
回答1:
As Andy Turner said for your first question :
clone() is not being used. It just so happens that the variable is called clone, but that has no semantic importance to the code.
Concerning the for
statement he's composed of 3 parts ,each separated by one ;
, and none of them is obligated to be there :
- The first part is where you can, or not, declare your incremental variable :
int i = 0;
- The second part is where you must put an evaluation resulting on a boolean value :
kappa.lengh >= i;
- The thrid part is where you will modify the variable(s) values :
i--;
Since none of these parts are obligated, you could write a correct for loop like this one : for(;;)
.
Here's a link to the documentation : For statement.
来源:https://stackoverflow.com/questions/29414954/issues-with-clone-and-for-loop