以下有4段,基础代码,其中有段代码中部分语句看不懂~~
第一段:
[code=Java][code=Java]public class Point {
public Point(double xVal, double yVal){
x = xVal;
y = yVal;
}

public Point(Point Point){
x = Point.x;
y = Point.y;
}

public String toString(){
return x + "," + y;
}
protected double x;
protected double y;
}第二段:
public class ListPoint {
public ListPoint(Point Point){
this.Point = Point;
next = null;
}
public void setNext(ListPoint next){
this.next = next;
}

public ListPoint getNext(){
return next;
}

public String toString(){
return"(" + Point + ")";
}
private ListPoint next;
private Point Point;
}第三段:
public class PolyLine {
public PolyLine(Point[] points){
if(points != null){
for(Point p : points){
addPoint(p);
}
}
}

public PolyLine(double[][] coords){
if(coords != null){
for(int i = 0; i<coords.length; i++){
addPoint(coords[i][0], coords[i][1]);
}
}
}

public void addPoint(Point Point){
ListPoint newEnd = new ListPoint(Point);
if(start == null){             //问题标签1~~ 
start = newEnd;
}else{
end.setNext(newEnd);
}
end = newEnd;
}

public void addPoint(double x, double y){
addPoint(new Point(x , y));
}

public String toString(){
StringBuffer str = new StringBuffer("Polyline:");
ListPoint nextPoint = start;
while(nextPoint != null){       //问题标签2~~~
str.append(" " + nextPoint);
nextPoint = nextPoint.getNext();
}
return str.toString();
}
private ListPoint start;
private ListPoint end;
}第四段:
public class TryPolyLine {
public static void main(String[] args){
double[][] coords = {{1.0 , 1.0},{1.0 , 2.0},{2.0 , 3.0},
                   {-3.0 , 5.0},{-5.0 , 1.0},{0.0 , 0.0}};
PolyLine Polygon = new PolyLine(coords);
System.out.println(Polygon);

Polygon.addPoint(10.0 , 10.0);
System.out.println(Polygon);

Point[] points = new Point[coords.length];
for(int i=0; i<points.length; i++){
points[i] = new Point(coords[i][0] , coords[i][1]);
}

PolyLine newPoly = new PolyLine(points);
System.out.println(newPoly);

}问题标签1:
  1、当生成newEnd对象时,ListPoint类里的 next 被初始化为null,那么当 end 调用 setNext 的方法时,他怎么知道我 next 这个值还是 point 的值??
  2、最后 end = newEnd; 那 end 里的 next 不又变成 null 了嘛??问题标签2:
  这代码该怎么结束循环啊??这里的 nextPoint = start 可是 start 又怎么可能会是 null 呢??因为当 if(start == null) 它才会为 start 赋值,除此以为并没有关于 start 赋值的语句

解决方案 »

  1.   

    首先,这个PolyLine就是一个链表类,而,point就是链表中的每个节点1、那么,楼主的问题1,在这个节点被定义的时候,它的next是null,但是,这个next在ListPoint类中已经定义了类型
      private ListPoint next; 
    2、end本身就是类ListPoint,它也是拥有自己的next的。所以,没有出示化过的next就是null,这点上没有任何问题,就是一个等待赋值的ListPoint3、在最开始的start没有被赋值的时候,就是空,这是链表类PolyLine等待初始化。而,toString方法是为了打印整个类而设计,会要求从链表的头读到链表的尾,尾为空就表示循环结束。
      

  2.   

    PolyLine是一个单链表类,start 表示开始节点,end表示最后一个结点,一开始链表中一个节点也没有(start==null),当加入第一个节点时,start = newEnd,end=newEnd,即开始节点和结束结点都为同一个结点(就是这个新加入的结点),当加入第二个结点的时候,start!=null,
    这时执行end.setNext(newEnd);这里的意思是将最后一个结点指向这个新加入的结点,接着end = newEnd;就是说将新加入的结点作为最后一个结点。
    联合起来end.setNext(newEnd);end = newEnd;也就是将新加入的节点插到这个链表的后面。你已经注意到了,链表的最后一个结点end的next就是为null,那是由于它不再指向其他的接点的缘故(要不也不叫最后一个)
    nextPoint = nextPoint.getNext();这样一直下去,就会到达end的next,这时循环也就结束了。
      

  3.   

    1. end 指向的 ListPoint 的 next 应该始终保持取的值是 null,否则如果链表最后一个节点还能找到下一节点(其 next 引用着的)那它就不是 end 了。
    2. 就是根据最后一个节点没有下一个节点(next == null)的这一特点来作遍历结束标志的。