2013-08-09 2 views
-2

블록을 실행 한 후 컨트롤을 외부 for 루프 (i 루프)로 전송하려고합니다. 즉, 나는 내부 루프 (J 루프)에 대한 모든 나머지 반복을 생략하고 외부 하나에 제어를 전송 싶어. 당신은 break with label을 사용할 수 있습니다 (I 루프) PLS일부 conds가 확인되고 일부 작업이 완료된 후 내부 루프에서 외부 루프로 컨트롤을 전송하는 방법은 무엇입니까?

for(int i=0;i<ana.length;i++) { 
    for(int j=i+1;j<ana.length;j++) { 

     if(a.isAnagram(ana[i],ana[j])) { 
      temp=ana[i+1]; 
      ana[i+1]=ana[j]; 
      for(int p=i+2;p<j;p++) { 
       ana[p+1]=ana[p]; 
      } 
      ana[i+2]=temp; 
     } 
    } 
} 
+0

'break'? 필요한 곳에 레이블을 사용하십시오. 방법은 다음과 같습니다. http://stackoverflow.com/questions/886955/breaking-out-of-nested-loops-in-java/886979#886979 –

+0

휴식을 사용하고 계속하십시오. –

+0

인터넷 검색의 기본적인 비트조차도이 문제를 해결하는 방법에 대한 정보를 제공합니다. -1 – StormeHawke

답변

1

도움이됩니다. 여기에 레이블 검색 : 여기

자바 튜토리얼 예이다.

class BreakWithLabelDemo { 
    public static void main(String[] args) { 

     int[][] arrayOfInts = { 
      { 32, 87, 3, 589 }, 
      { 12, 1076, 2000, 8 }, 
      { 622, 127, 77, 955 } 
     }; 
     int searchfor = 12; 

     int i; 
     int j = 0; 
     boolean foundIt = false; 

    search: 
     for (i = 0; i < arrayOfInts.length; i++) { 
      for (j = 0; j < arrayOfInts[i].length; 
       j++) { 
       if (arrayOfInts[i][j] == searchfor) { 
        foundIt = true; 
        break search; 
       } 
      } 
     } 

     if (foundIt) { 
      System.out.println("Found " + searchfor + " at " + i + ", " + j); 
     } else { 
      System.out.println(searchfor + " not in the array"); 
     } 
    } 
} 
0

if 블록의 끝에 'break'문을 사용하십시오. 이렇게하면 "j"루프의 나머지 반복을 건너 뛸 것이며 "i"루프의 다음 반복이 실행됩니다.

for(int i=0;i<ana.length;i++) 
{ 
    for(int j=i+1;j<ana.length;j++) 
    { 
     if(a.isAnagram(ana[i],ana[j])) 
     { 
     temp=ana[i+1]; 
     ana[i+1]=ana[j]; 
     for(int p=i+2;p<j;p++) 
     { 
      ana[p+1]=ana[p]; 
     } 
     ana[i+2]=temp; 
     break; 
     } 
    } 
}