2017-05-31 20 views
-1

我必須編寫一個方法兩次。一旦使用數組並且一次使用鏈接列表。 任務是繪製一個多邊形。點被保存在數組或鏈接列表中。使用數組重寫方法使用有序列表

我已經做的第一部分(陣列和它完美的作品)

public void draw() { 
     Drawing.graphics.setColor(this.color); 
     int[]xPoints = new int[this.points.size()]; 
     int[]yPoints = new int[this.points.size()]; 
     for(int i = 0; i < this.points.size();i++){ 
      Position p = this.points.get(i); 
      xPoints[i] = p.getX()+this.origin.getX(); 
      yPoints[i] = p.getY()+this.origin.getY(); 
     } 
     Drawing.graphics.fillPolygon(xPoints,yPoints,this.points.size()); 

    } 

現在,我只是不知道如何寫只能用鏈表的方法相同。 這就是我已經走了多遠。這不是很多.....

public void draw() { 
     Drawing.graphics.setColor(this.color); 
     PolygonList list = new PolygonList(this.points.size(),this.points.size(), this.color); 
      Drawing.graphics.fillPolygon(list.origin.x,list.origin.y,this.points.size());  
    } 

感謝您的幫助!

+0

'points.size()'和'points.get(我)'聽起來更像列表不是像數組 –

+0

請始終顯示在你的第二次嘗試解決方案,不管它可能有多糟糕。這樣做會使問題的質量提高100倍。 –

+0

爲什麼'LinkedList'和爲什麼不'ArrayList'? – SomeDude

回答

1

請勿複製代碼。將你現有的代碼解壓到一個參數爲drawPolygon(List<Position> positions)的方法。所以,你可以調用這個梅索德既數組和列表:

drawPolygon(Arrays.asList(points)); 
drawPolygon(pointsAsList);