Java Reference
In-Depth Information
ListPoint
object in its
next
member, which in turn contains a reference to the next, and so on through to
the last
ListPoint
object. You know it is the last one because its
next
member, which usually points to the
next
ListPoint
object, is
null
. Let's try it.
TRY IT OUT: The ListPoint Class
You can define the
ListPoint
class using the
Point
class with the following code:
public class ListPoint {
// Constructor
public ListPoint(Point point) {
this.point = point;
// Store point reference
next = null;
// Set next ListPoint as null
}
// Set the pointer to the next ListPoint
public void setNext(ListPoint next) {
this.next = next;
// Store the next ListPoint
}
// Get the next point in the list
public ListPoint getNext() {
return next;
// Return the next ListPoint
}
// Return String representation
@Override
public String toString() {
return "(" + point + ")";
}
private ListPoint next;
// Refers to next ListPoint in
the list
private Point point;
// The point for this list
point
}
Directory "TryPolyLine"
Save this file in the same directory as the
Point
class,
TryPolyLine
.
How It Works
A
ListPoint
object is a means of creating a list of
Point
objects that originate elsewhere so you don't
need to worry about duplicating
Point
objects stored in the list. You can just store the reference to the
Point
object passed to the constructor in the data member,
point
. The data member,
next
, should con-
tain a reference to the next
ListPoint
in the list, and because that is not defined here, you set
next
to
null
.