2017-05-23 11 views
0

그래서 해시 테이블 내에서 충돌을 기록하고 보여주는 프로그램이 있습니다. 나는 충돌을 기록했다. 충돌 한 데이터, 예상되는 위치, 테이블의 위치를 ​​기록했다.그림판 구성 요소의 JAVA 무한 루프?

문제는 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; 
       } 
      } 
     } 
    } 

여러분의 도움과 입력이 크게 감사합니다 :

여기 내 코드입니다!

+0

'해시 테이블'을 null 값으로 설정하는 이유는 무엇입니까? –

+0

어떤 클래스 유형이'hashtable' 변수이고이 조건이'while (hashtable! = null)'에 부합 할 때? 표시된 코드에서 실제적으로 오류의 위치를 ​​말할 수는 없습니다. – MaxZoom

+0

'while '대신'if (hashtable! = null)'입니다. 분명히 해시 테이블은 나중에 만들어집니다. –

답변

0

HashMapnull으로 설정된 경우 이스케이프가 발생하지 않습니다. 루프를 해제하려면 if(start == end) hashtable = null;과 같은 것이 필요합니다.

0

답변을 찾았습니다. 어쩌면 최고는 아니지만 ...

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. 
    } 
} 
+0

"break가 필요하고 after for 루프가 필요합니다." 그런 다음 루프가 필요 없습니다. 'if (hashtable! = null)'. –