简体   繁体   English

如何使用 Java 中的迭代器正确访问队列中的值?

[英]How to properly access values from Queue using Iterator in Java?

I used queue in adding values for my x and y which are the coordinates of where i clicked my mouse.我使用队列为我的xy添加值,这是我单击鼠标的坐标。 However, I got an error when I try to access the values using Iterator .但是,当我尝试使用Iterator访问值时出现错误。 Can somebody help me?有人可以帮助我吗? How do I do it the right way?我如何以正确的方式做到这一点? Also, I don't know what Timer and Actionlistener does to my code (are they needed or do i have to remove this?) so a little explanation will be very useful to me.另外,我不知道TimerActionlistener对我的代码做了什么(需要它们还是我必须删除它?)所以一些解释对我来说非常有用。 I hope you could help me fix and improve these codes.我希望你能帮助我修复和改进这些代码。 Thanks!谢谢! :) :)

public class Canvas extends JPanel implements MouseListener, ActionListener{

Timer time;
int numberOfPoints;
Queue<Integer> x = new LinkedList<Integer>();
Queue<Integer> y = new LinkedList<Integer>();
Iterator itX = x.iterator();
Iterator itY = y.iterator();
public Canvas(){
    addMouseListener(this);
    time = new Timer(1, this);
    time.start();       
    numberOfPoints = 5;

}

public void paint (Graphics g){
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setBackground(Color.black);
    g2d.drawString("Left: "+ numberOfPoints, 10, 20);
    g2d.fillOval(220, 170, 30, 30);
    g2d.setColor(Color.red);    
    while(itX.hasNext()){
        Integer xx = (Integer) itX.next();
        Integer yy = (Integer) itY.next();
        g2d.fillOval(xx, yy, 30, 30);
    }
}

public void mousePressed(MouseEvent e) {        
    if(numberOfPoints>0){
        numberOfPoints--;
        x.add(e.getX()-15);
        y.add(e.getY()-15);
        repaint();
    }

}

public void mouseReleased(MouseEvent e) {}

public void mouseClicked(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

@Override
public void actionPerformed(ActionEvent arg0) {
    repaint();

}

} }

Anyway, here's my main class in case you want to run it.无论如何,这是我的主要课程,以防您想运行它。

public class ClikkPanikk extends JFrame{

public ClikkPanikk(){
    short width = 500;
    short height = 400;
    add(new Canvas());
    setTitle("ClikkPanikk");
    setVisible(true);
    setSize(width, height);
    setResizable(false);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String[] args) {
    new ClikkPanikk();
}

} }

You are creating the iterator as a field rather than just before its use.您正在将迭代器创建为一个字段,而不是在使用之前。 Because of this, the iterator notices that the values have changed since it was created and throws a concurrent modification exception.因此,迭代器会注意到自创建以来这些值已更改并抛出并发修改异常。

Initialize your iterator just before your while loop like:在 while 循环之前初始化您的迭代器,例如:

itX = x.iterator();
itY = y.iterator();
while(itX.hasNext()){
    Integer xx = (Integer) itX.next();
    Integer yy = (Integer) itY.next();
    g2d.fillOval(xx, yy, 30, 30);
}

Also, out of context I may be wrong (I haven't gone through all of your code), but you shouldn't need to cast for an Integer object as you can just set an Integer object to the primitive int type:另外,在上下文之外我可能是错的(我还没有完成您的所有代码),但是您不需要为 Integer 对象进行强制转换,因为您可以将 Integer 对象设置为原始 int 类型:

Integer varName = 12;

will create an Integer object of value 12, so the unnecessary casting just leaves room for the compiler to miss fatal errors.将创建一个值为 12 的 Integer 对象,因此不必要的强制转换只会为编译器遗漏致命错误留下空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM