【发布时间】:2021-05-03 19:29:29
【问题描述】:
我有这个 java 类:
public class Coordinate {
//Instansvariabler (klassvariabler, objektvariabler)
int xPos, yPos;
//Konstruktor
public Coordinate(int xPos, int yPos){
this.xPos = xPos;
this.yPos = yPos;
}
}
我正在使用 ArrayList 来通过这个 for 循环获取尽可能多的值
for(int i = 0; i < snakeParts; i++){
if(snake.size() <= snakeParts)
snake.add(new Coordinate(0, 0));
}
如何从 snake.get(i) 获取 xPos 和 yPos?有什么简单的方法可以做到这一点吗?当我打印 snake.get(i) 这就是我得到的: Coordinate@634b392;
【问题讨论】:
-
覆盖
toString类中的Coordinate方法,以您希望的任何格式返回其实例。 -
@AlexRudenko 我对编程很陌生,所以我不能 100% 确定你的意思,我该怎么做?
-
在Java中,当打印一个对象时,它的方法
toString被调用,Coordinate@634b392默认实现toString返回
标签: java arrays arraylist integer