public class Point {La classe main:
private int x;
private int y;
public Point(int x, int y){
this.x=x;
this.y=y;
}
@Override
public String toString() {
return "[x= "+this.x+", y= "+this.y+"]";
}
}
import java.util.ArrayList;Exécution:
public class Main {
public static void main(String[] args) {
ArrayList<Point> list = new ArrayList<Point>()
{
private static final long serialVersionUID = 1L;
@Override
public String toString()
{
return super.toString();
}
};
list.add(new Point(2, 4));
list.add(new Point(1, 6));
list.add(new Point(5, 2));
list.add(new Point(3, 7));
list.add(new Point(8, 9));
System.out.println(list.toString());
}
}
[[x= 2, y= 4], [x= 1, y= 6], [x= 5, y= 2], [x= 3, y= 7], [x= 8, y= 9]]Dans cet exemple, ArrayList fait appel à la méthode toString() déclarée dans la classe Point. Si on enlève la méthode toString() dans la classe Point, on aura le format suivant:
[Point@4a5f634c, Point@3c7038b9, Point@6b9c18ae, Point@55187eb3, Point@3b26456a]
Please disable your ad blocker and refresh the window to use this website.