issues with clone and for loop

两盒软妹~` 提交于 2019-12-14 00:07:10

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!