public class TryPolyLine
{
public static void main(String[] args)
{
TryPolyLine tpl = new TryPolyLine();
Point[] coords={tpl.new Point(1.0,1.0), tpl.new Point(2.0,2.0), tpl.new Point(3.0,3.0), tpl.new Point(4.0,4.0), tpl.new Point(5.0,5.0), tpl.new Point(6.0,6.0)};
TryPolyLine.PolyLine polygon=tpl.new PolyLine(coords);
System.out.println(polygon);
polygon.addPoint(tpl.new Point(7.0,7.0));
System.out.println(polygon);
Point[] points=new Point[coords.length];
for(int i=0;i<points.length;i++)
{
points[i]=tpl.new Point(coords[i].x,coords[i].y);
TryPolyLine.PolyLine newPoly=tpl.new PolyLine(points);
System.out.println(newPoly);
}
}

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;
} 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;
} class PolyLine
{
public ListPoint start;
public ListPoint end;

public PolyLine(Point[] points)
{
if(points!=null)
{
start=new ListPoint(points[0]);
end=start;
for(int i=1;i<points.length;i++)
{
addPoint(points[i]);
}
}
} public void addPoint(Point point)
{
ListPoint newEnd=new ListPoint(point);
if(start==null)
start=newEnd;
else
end.setNext(newEnd);
end=newEnd;
} public String tostring()
{
StringBuffer str=new StringBuffer("PolyLine:");
ListPoint nextPoint=start;
while(nextPoint!=null)
{
str.append(" "+nextPoint);
nextPoint=nextPoint.getNext();
}
return str.toString();
}
}}//这是我修改过后的,自己对照一下吧

解决方案 »

  1.   

    public class TryPolyLine
    {
        public TryPolyLine()
        {
            Point[] coords={new Point(1.0,1.0),new Point(2.0,2.0),new Point(3.0,3.0),new Point(4.0,4.0),new Point(5.0,5.0),new Point(6.0,6.0)};
            PolyLine polygon=new PolyLine(coords);
            System.out.println(polygon);
            polygon.addPoint(new Point(7.0,7.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].x,coords[i].y);
                PolyLine newPoly=new PolyLine(points);
                System.out.println(newPoly);
            }
        }
        public static void main(String[] args)
        {
            TryPolyLine tpl = new TryPolyLine();
        }    private 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 ListPoint start;
            public ListPoint end;
            public PolyLine(Point[] points)
            {
                if(points!=null)
                {
                    start=new ListPoint(points[0]);
                    end=start;
                    for(int i=1;i<points.length;i++)
                    {
                        addPoint(points[i]);
                    }
                }
            }
                public void addPoint(Point point)
                {
                    ListPoint newEnd=new ListPoint(point);
                    if(start==null)
                        start=newEnd;
                    else
                        end.setNext(newEnd);
                    end=newEnd;
                }
                public String tostring()
                {
                    StringBuffer set=new StringBuffer("PolyLine:");
                    ListPoint nextPoint=start;
                    while(nextPoint!=null)
                    {
                        set.append(" "+nextPoint);
                        nextPoint=nextPoint.getNext();
                    }
                    return set.toString();
                }
        }
    }
      

  2.   

    GJA106(中文字符) 哥哥,你改了只后,我就理解了,怪不得原来的代码我在传递参数的时候就??????????????????不明白。可是我是照着书打的啊《java编程指南jdk1.3版》哦!