当前位置: 代码迷 >> J2SE >> 有段程序里的部分语句看不大懂,请进来看看~该如何解决
  详细解决方案

有段程序里的部分语句看不大懂,请进来看看~该如何解决

热度:62   发布时间:2016-04-24 13:01:01.0
有段程序里的部分语句看不大懂,请进来看看~~
以下有4段,基础代码,其中有段代码中部分语句看不懂~~
第一段:
Java code
[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;}


第二段:
Java code
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;}


第三段:
Java code
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){             [color=#FF0000]//问题标签1~~ [/color]            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){       [color=#FF0000]//问题标签2~~~[/color]            str.append(" " + nextPoint);            nextPoint = nextPoint.getNext();        }        return str.toString();    }    private ListPoint start;    private ListPoint end;}


第四段:
Java code
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 赋值的语句

------解决方案--------------------
首先,这个PolyLine就是一个链表类,而,point就是链表中的每个节点

1、那么,楼主的问题1,在这个节点被定义的时候,它的next是null,但是,这个next在ListPoint类中已经定义了类型
[code=java]  private ListPoint next; [/code]
2、end本身就是类ListPoint,它也是拥有自己的next的。所以,没有出示化过的next就是null,这点上没有任何问题,就是一个等待赋值的ListPoint

3、在最开始的start没有被赋值的时候,就是空,这是链表类PolyLine等待初始化。而,toString方法是为了打印整个类而设计,会要求从链表的头读到链表的尾,尾为空就表示循环结束。
  相关解决方案