Jackson Jackson - 1 year ago 66
Java Question

Initialize a Point2D in List<>

If I have a simple List< Point2D > declared.


List<Point2D> listOfPoints;

/* What I tried */
Point2D point1;

But, how does one initialize
so that I can have a coordinate of let's say

Answer Source

You have to create an instance of Point2D. Right now, you are adding null to your listOfPoints. Plus, listOfPoints is not initialized, so your code would generate a NullPointerException. Try this instead:

List<Point2D> listOfPoints = new ArrayList<>(); // or another List implementation class

Point2D point1 = new Point2D.Float(3, 2); // or perhaps Point2D.Double

Also, once you have a Point2D.Float or Point2D.Double object, you can set the coordinates explicitly, either by assigning directly to the x and y fields or by calling setLocation() and passing the coordinates.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download