首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用数组实现配置堆栈以构建Koch Snowflake

使用数组实现配置堆栈以构建Koch Snowflake
EN

Stack Overflow用户
提问于 2014-01-15 12:16:05
回答 1查看 84关注 0票数 0

我正在尝试使用堆栈的想法来构建一个Koch雪花。我让它与链表一起工作,但由于某些原因,我不能让它与数组一起工作。我一直得到一个ArrayIndexOutOfBoundsException 1,然后是0。我似乎找不出我的错误在哪里。任何意见都将不胜感激。谢谢。

代码语言:javascript
复制
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();
}

}

是从堆栈中生成雪花的类。下面是包含数组的类。

代码语言:javascript
复制
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();

再次感谢。

EN

回答 1

Stack Overflow用户

发布于 2014-01-15 12:21:14

这里,容器的大小是this.size() + 1

代码语言:javascript
复制
Line[] holder = new Line[this.size() + 1];

但是在这里,您尝试将值添加到size索引

代码语言:javascript
复制
holder[this.size() + 1] = newValue;

例如,如果size()返回5,那么holder的大小就是5。因此,最大索引将是4。但是您是在向holder5添加值;

我希望你能理解这种情况

因此,添加如下值

代码语言:javascript
复制
holder[this.size()] = newValue;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21129023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档