我正在尝试使用增强的 for 循环从数组中打印出我的数据,但结果是垃圾。请帮忙,提前谢谢你。
ArrayList 数组 = new ArrayList();
//For loop to input names
for(int i=0; i < 3; i++)
{
System.out.print("Enter name of Player " + i +":");
String startName = keyboard.nextLine();
System.out.print("Enter jersey number of Player " + i +":");
int playNum = keyboard.nextInt();
startName = keyboard.nextLine();
//Make objects and add to array
HockeyPlayer p = new HockeyPlayer(startName, playNum);
array.add(p);
}
keyboard.close();
循环的代码是:
System.out.println("The players are...");
for(HockeyPlayer val : array)
{
System.out.println(val);
}
它最终打印出这样的东西......我该如何解决它?
Please enter the names and jersey numbers of 3 hockey players.
Enter name of Player 0:Joeasdas
Enter jersey number of Player 0:213
Enter name of Player 1:Baskdja
Enter jersey number of Player 1:121
Enter name of Player 2:djasklaj
Enter jersey number of Player 2:455
The players are...
HockeyPlayer@3f84246a
HockeyPlayer@7c72cbb9
HockeyPlayer@549d1e83