如何在每个循环中跳过a的第一次迭代? [英] How to skip the first iteration in a for each loop?

查看:166
本文介绍了如何在每个循环中跳过a的第一次迭代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编程对我来说是新的,我试图了解一些概念。我正在尝试创建一个简单的小程序,在地图上显示欧洲的首都城市,使用drawLine方法将它们连接起来。我遇到了一个问题,我无法成功加入两个省会城市。我想我理解为什么,但我想不出办法绕过它。 draw方法中的后两个参数与前两个参数相同,但我无法跳过第一次迭代。这对我来说是全新的,我现在正试图从书本和网上学习。

Programming is new to me and i am trying to get my head around some of the concepts. I am trying to make a simple applet that displays the capital cities of Europe on a map on join them using the drawLine method. I have encountered a problem where i cannot successfully join the 2 capital cities together with a line. I think i understand why but i cannot think of a way to around it. The second two parameters in the draw method are the same as the first two, but i cannot make it skip the first iteration. This is all new to me and i am trying to learn from a book and the web at the moment.

public void paint(Graphics g)
{
    super.paint(g);
    g.drawImage(image, 0, 0, this);

    for (Entry<String, co> entry : map.entrySet()) 
    {
        g.setColor(Color.BLUE);
        g.fillOval(entry.getValue().a, entry.getValue().b, 5, 5);

        g.setColor(Color.BLUE);
        g.drawString(entry.getKey(), entry.getValue().a+7, entry.getValue().b+7);     

        g.setColor(Color.RED);
        g.drawLine(entry.getValue().a, entry.getValue().b, 0, 0);//Problem
    }
}

有人可以推动我朝着正确的方向前进吗?我正在考虑使用迭代器而不是每个循环,这是我目前唯一的想法。

Can some one push me in the right direction? I was thinking of using an iterator instead of a for each loop, that is the only idea i have in my head at the moment.

推荐答案

可能不优雅,但它会起作用并且不会对性能产生任何明显的影响:

Probably not elegant, but it will work and won't have any noticeable performance impact:

boolean isFirst = true;
for(...) {
    if(!isFirst) {
        // your code
    } else {
        isFirst = false;
    }
}

这篇关于如何在每个循环中跳过a的第一次迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆