绘制组件上的JAVA无限循环?

绘制组件上的JAVA无限循环?,java,while-loop,infinite-loop,paintcomponent,Java,While Loop,Infinite Loop,Paintcomponent,所以,我有一个程序,记录并显示哈希表中的冲突。我已经让它记录了所有的碰撞-什么数据发生了碰撞,它应该在哪里,以及它在表中的位置 问题在于paintcomponent似乎陷入了无限循环。我不知道while循环的哪一部分 我尝试删除while循环,但这会导致编译时错误。 我还尝试在if语句中添加一个return,但这只会在x个崩溃量中给出1个值 这是我的密码: public void paintComponent (Graphics g) { int xpos = 20,

所以,我有一个程序,记录并显示哈希表中的冲突。我已经让它记录了所有的碰撞-什么数据发生了碰撞,它应该在哪里,以及它在表中的位置

问题在于paintcomponent似乎陷入了无限循环。我不知道while循环的哪一部分

我尝试删除while循环,但这会导致编译时错误。 我还尝试在if语句中添加一个return,但这只会在x个崩溃量中给出1个值

这是我的密码:

     public void paintComponent (Graphics g) {
        int xpos = 20, ypos = 30;
        crash = 0;

        g.setFont(plainfont);

        g.drawString("Hash Crash count is: " + crash, xpos, ypos);
        while(hashtable != null){
            for (String name : names) {
                int start = hashtable.hashFunc3(name);      //locates where data must be
                int end = hashtable.locateCrash(name);      //locates where data is found
                if (start != end) {
                    ypos += 20;
                    crash++;
                    g.drawString("Hash Crash:", xpos, ypos);
                    g.drawString(name, 100, ypos);
                    g.drawString("should be at", 200, ypos);
                    g.drawString(Integer.toString(start), 300, ypos);
                    g.drawString("found at", 350, ypos);
                    g.drawString(Integer.toString(end), 400, ypos);
                    //return;
                }
            }
        }  
    }

非常感谢您的帮助和投入

没有将
HashMap
设置为
null
的转义情况。您需要类似于
if(start==end)hashtable=null以打破循环。

找到了答案。也许不是最好的,但是

public void paintComponent (Graphics g) {
    int xpos = 20, ypos = 30;
    crash = 0;

    g.setFont(plainfont);

    g.drawString("Hash Crash count is: " + crash, xpos, ypos);
    while(hashtable != null){
        for (String name : names) {
            int start = hashtable.hashFunc3(name);      //locates where data must be
            int end = hashtable.locateCrash(name);      //locates where data is found
            if (start != end) {
                ypos += 20;
                crash++;
                g.drawString("Hash Crash:", xpos, ypos);
                g.drawString(name, 100, ypos);
                g.drawString("should be at", 200, ypos);
                g.drawString(Integer.toString(start), 300, ypos);
                g.drawString("found at", 350, ypos);
                g.drawString(Integer.toString(end), 400, ypos);
            }
        }
        break; //<-- needed a break; after for loop.
    }  
}
公共组件(图形g){
int xpos=20,ypos=30;
崩溃=0;
g、 setFont(普通字体);
g、 drawString(“哈希崩溃计数为:”+Crash,xpos,ypos);
while(哈希表!=null){
for(字符串名称:名称){
int start=hashtable.hashFunc3(名称);//查找必须保存数据的位置
int end=hashtable.locateCrash(name);//查找数据的位置
如果(开始!=结束){
ypos+=20;
crash++;
g、 抽绳(“散列崩溃:”,xpos,YPO);
g、 抽绳(名称,100,ypos);
g、 抽绳(“应在”,200,ypos);
g、 抽绳(整数。toString(开始),300,ypos);
g、 抽绳(“发现于”,350,ypos);
g、 抽绳(整型toString(end),400,ypos);
}
}

break;//什么将
哈希表设置为空值?什么类类型是
哈希表
变量,何时满足此条件
while(hashtable!=null)
?从显示的代码中几乎不可能说出错误在哪里。
if(hashtable!=null)
而不是
while
。显然哈希表是在以后创建的。@AndyTurner哈希表是在按下按钮时创建的。表格大小取自文本字段。感谢大家的快速响应。但是我找到了一个解决我愚蠢的小问题的方法。“需要中断;之后为循环。”然后你就不需要循环了。
如果(哈希表!=null)