so i'm trying to setup an array that holds x y coordinates. the program seems to work but my print results are memory addresses. here's my code:
static class Point{
int x;
int y;
@Override
public String toString() {
return x + " " + y;
}
}
public static Object thePoints( int x, int y){
Point[] mypoints = new Point[10];
for (int i = 0; i < mypoints.length; i++){
mypoints[i] = new Point();
}
mypoints[0].x = 200;
mypoints[0].y = 200;
return mypoints;
}
public static void main(String args[]) {
Object thing = thePoints(0,0);
System.out.print(thing);
}
}
input is appreciated.
You are printing out an array of type Point[]
in your main()
method, contrary to what it appears. A quick way to get around this problem is to use Arrays.toString()
. Try changing the code in your main()
method to this:
public static void main(String args[]){
Object thing = thePoints(0,0);
System.out.print(Arrays.toString((Point[])thing));
}
If you also refactor Point.toString()
to the following, then you get some fairly nice looking output:
static class Point{
int x, y;
@Override
public String toString() {
return "(" + x + ", " + y + ")"; // print out a formatted ordered pair
}
}
Output:
[(200, 200), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)]