的拷贝填充我想制作一个棋盘游戏的移动版本,并且由于某种原因,我有一个ArrayList,其中所有元素都符合最后一个元素,只要添加一个新元素,我就可以不知道为什么。 (我非常新到Android工作室)Arraylist由最后一个元素Android Studio
下面是关心增加了列表中的所有类:
public class ScoreBoard {
private static final ScoreBoard ourInstance = new ScoreBoard();
public static ScoreBoard getInstance() {
return ourInstance;
}
List<Player> Players;
int nrOfPlayers;
int activePlayer;
private ScoreBoard() {
Players = new ArrayList<Player>();
}
public void addPlayer(CharSequence name) {
Player p = new Player(name);
Players.add(p);
}
public void nrOfPlayers(int number)
{
nrOfPlayers = number;
}
public boolean stop()
{
boolean stop = false;
if (nrOfPlayers <= Players.size())
{
stop = true;
}
return stop;
}
public void StartGame()
{
Random random = new Random();
random.nextInt(nrOfPlayers);
}
public CharSequence GetActivePlayerName()
{
return Players.get(activePlayer).name;
}
}
public class Player {
public CharSequence name;
public int points;
public Player(CharSequence name)
{
this.name = name;
}
}
public class PlayerName extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player_name);
}
public void addPlayer(View view)
{
ScoreBoard SB = ScoreBoard.getInstance();
TextView nameText = (TextView)findViewById(R.id.NameText);
TextView playerText = (TextView)findViewById(R.id.PlayerText);
CharSequence name = nameText.getText();
SB.addPlayer(name);
playerText.setText("Player " + (SB.Players.size() + 1) + " enter your name");
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, name + " Added", duration);
toast.show();
if(SB.stop())
{
SB.StartGame();
Intent intent = new Intent(this, TakeTurn.class);
startActivity(intent);
}
else
{
}
}
}
你是什么意思符合最后一个元素? –
所有元素都成为最后添加的元素的副本(应该有更好的措辞) – Calmaty
“嗨,我看到你是新来的,如果你觉得答案可以解决问题,请点击”接受“绿色的复选标记,这有助于将注意力集中在仍然没有答案的旧版SO上 –