Cannot invoke add on the array type

后端 未结 2 768

I have been given some code to tinker with, which is a prisoners dilema game are initially the players were set with this piece of code into an array.

IPDPlayer[         


        
2条回答
  •  我在风中等你
    2021-01-21 14:06

    Why do you want to have an array right at the very beginning; Use a list and once the input is over, you can always convert it to an array .

    List ipdPlayerLst = new ArrayList();
    ipdPlayerLst .add(.....);
    

    later

    IPDPlayer[] currentPlayers = new IPDPlayer[ipdPlayerLst .size()];
    currentPlayers =ipdPlayerLst.toArray(currentPlayers ); 
    

提交回复
热议问题