2014-01-15 24 views
0

我正在嘗試使用堆棧的想法來構建科赫雪花。我有一個鏈接列表,但出於某種原因,我無法讓它與數組一起工作。我一直得到一個ArrayIndexOutOfBoundsException 1,然後在0.我似乎無法弄清楚我的錯誤在哪裏。任何輸入將不勝感激。謝謝。使用數組實現配置堆棧以構建科赫雪花

class SnowflakeStack extends Frame { 

private Stack<Line> line = new ArrayStack(); 
private LinkedList<Line> complete = new LinkedList<Line>(); 

public Snowflake() { 
    setTitle("Stack-based Snowflake"); 
    setSize(400, 400); 
    addWindowListener(new CloseQuit()); 
} 

public void run() { 
    // insert first lines into snowflake 
    Point a = new Point(50, 140); 
    Point b = new Point(350, 140); 
    Point c = new Point(200, 360); 

    this.line.push(new Line(a, b)); 
    this.line.push(new Line(b, c)); 
    this.line.push(new Line(c, a)); 

    // now make the snowflake 
    while (!this.line.isEmpty()) { 
     Line lne = this.line.pop(); 
     processLine(lne); 
     try { 
      Thread.sleep(100); 
     } catch (Exception exception) { 
      System.out.println(exception.getMessage()); 
     } 
     repaint(); 
    } 
} 

private void processLine(Line lne) { 
    // first compute line lengths 
    int dx = (lne.stop.x - lne.start.x)/3; 
    int dy = (lne.stop.y - lne.start.y)/3; 
    if ((dx * dx + dy * dy) < 10) { 
     this.complete.addFirst(lne); // line is too small 
    } else { 
     Point a = new Point(lne.start.x + dx, lne.start.y + dy); 
     Point b = new Point(lne.start.x + 3 * dx/2 + dy, lne.start.y + 3 
       * dy/2 - dx); 
     Point c = new Point(lne.start.x + 2 * dx, lne.start.y + 2 * dy); 
     this.lines.push(new Line(lne.start, a)); 
     this.lines.push(new Line(a, b)); 
     this.lines.push(new Line(b, c)); 
     this.lines.push(new Line(c, lne.stop)); 
    } 
} 

public void paint(Graphics g) { 
    Iterator<Line> iter = this.lines.iterator(); 
    while (iter.hasNext()) { 
     Line lne = (Line) iter.next(); 
     lne.draw(g); 
    } 
    iter = this.complete.iterator(); 
    while (iter.hasNext()) { 
     Line lne = (Line) iter.next(); 
     lne.draw(g); 
    } 
} 

public static void main(String[] args) { 
    Snowflake snowflake = new Snowflake(); 
    snowflake.setVisible(true); 
    snowflake.run(); 
} 

} 

是從堆棧中製作雪花的類。下面是有數組的類。

public class ArrayStack implements Stack<Line> { 

private Line[] lines; 
private int top = -1; 

/** 
* Creates array of Line objects 
*/ 

public ArrayStack() { 
    this.lines = new Line[0]; 
    top = lines.length; 

} 

@Override 
public boolean isEmpty() { 

    return this.lines.length == 0; 
} 

/** 
* Sees if array is full since not dynamically allocated like a linked list 
* 
* @return true if array is full 
*/ 

public boolean isFull() { 
    return this.size() == this.lines.length; 
} 

@Override 
public int size() { 

    return this.lines.length; 
} 

@Override 
public Iterator<Line> iterator() { 

    return new ListIterator(this.lines); 

} 

@Override 
public void push(Line newValue) { 
    if (newValue == null) { 
     throw new IllegalArgumentException("Item is null"); 
    } 

    Line[] holder = new Line[this.size() + 1]; 
    for (int i = 0; i < this.size(); i++) { 
     holder[i] = this.lines[i]; 
    } 
    holder[this.size() + 1] = newValue; 
    this.lines = new Line[this.size() + 1]; 
    this.lines = holder; 

} 

@Override 
public Line peek() { 

    return this.lines[this.lines.length]; 

} 

@Override 
public Line pop() { 
    Line poppedVal = null; 
    poppedVal = this.lines[this.size()]; 

    Line[] holder = new Line[this.size() - 1]; 

    for (int i = 0; i <= (this.size() - 1); i++) { 
     holder[i] = this.lines[i]; 
    } 
    this.lines = new Line[this.size() - 1]; 
    this.lines = holder; 
    return poppedVal; 

} 

protected class ListIterator implements Iterator<Line> { 
    private Line[] array; 
    private int index = 0; 
    private int size; 

    public ListIterator(Line[] anArray) { 
     array = anArray; 
     size = array.length; 
    } 

    public boolean hasNext() { 
     return (index + 1) >= size; 
    } 

    public Line next() { 
     Line nextVal = this.array[size]; 
     return nextVal; 

    } 

    @Override 
    public void remove() { 
     throw new UnsupportedOperationException(); 

    } 

} 

} 

我得到的錯誤是在線路: holder[this.size() + 1] = newValue; this.lines.push(new Line(a, b));Line nextVal = this.array[size]; Line lne = (Line) iter.next();

再次感謝。

回答

1

這裏支架的尺寸是this.size() + 1

Line[] holder = new Line[this.size() + 1]; 

但在這裏,你想添加值的大小指數

holder[this.size() + 1] = newValue; 

例如,如果大小()返回5,然後大小所以最大指數是4,但是你正在給持有者增加價值[5];

我希望你瞭解的情況

所以加樣

holder[this.size()] = newValue; 
+0

謝謝你的價值,我只是有一些錯誤,而不是一整列。我在'poppedVal = this.lines [this.size()];'和Line lne = this.lines.pop();上得到了一個錯誤;'這是一個類似於push方法的問題嗎? – Ashton

+0

但是這次我的outofboundsexception是3 – Ashton

+0

我只是以5號爲例..你的數組大小是多少?總共有 – stinepike