2013-03-01 4 views
3

세트를 반복합니다. CarDetail의 값이 설정되어 있지 않으면 루프가 다음 CarDetail에서 다시 시작되기를 원합니다. 하지만 어떻게 든 내 계속 실용적이지는 않습니다. iterator를 계속 사용하지 않을 수 있습니까?세트를 반복하고 계속하십시오.

final Set<CarDetail> tmpDetail = new HashSet<CarDetail>(details); 
    for(Iterator<CarDetail> iter = tmpDetail.iterator(); iter.hasNext();){      
     CarDetail detail = iter.next(); 
     if(detail.getBack() == null){ 
     continue; 
    } 
    ... do something 
} 
+0

이것은 For 루프입니다. for 루프와 마찬가지로 continue를 사용할 수 있습니다. –

+0

나에게 잘 보입니다. 정확한 예기치 못한 사실과 그 증명 방법에 대한 정보를 추가하십시오. – poitroae

+1

당신이하려는 것을 다시 말해 줄 수 있습니까? 그리고 향상된 for for 루프를 사용할 것을 제안 할 수 있습니까? 'for (CarDetail detail : iter) {'는 훌륭하게 할 것입니다! 아니면 더 나은'for (CarDetail detail : details) {' – corsiKa

답변

2

계속 반복자를 사용하면 아무런 문제가 없습니다. 당신은 (각 루프) 루프 강화 이것을 사용할 수 있습니다 : 당신은 어떻게 당신이 게시 코드에서 무시되고있는 문을 계속해야

final Set<CarDetail> tmpDetail = new HashSet<CarDetail>(details); 
for(CarDetail detail : tmpDetail) {       
     if(detail.getBack() == null) { 
     System.out.println("Skipping over " + detail.toString()); 
     continue; 
     } 
     System.out.println("Processing car detail: " + detail.toString()); 
    //... do something 
} 

입니까? println 문을 약간만 사용하면 예상대로 계속 작업이 작동하는지 확인할 수 있습니다.

0

Iterator없이 직접 Set을 반복하고 원하는 경우 루프에 continue을 사용할 수 있습니다. 다음 예는 다음과 continue이 있기 때문에 Set에 주문의 보장이 없기 때문에이 2 인 경우에만

Current set element is 1 
Current set element is 3 

Set<Integer> intSet = new HashSet<Integer>(); 
intSet.add(1);intSet.add(2);intSet.add(3); 

for(Integer setElem : intSet) 
{ 
    if(setElem.intValue() == 2) continue; 

    System.out.println("Current set element is " + setElem); 
} 

인쇄, 출력은 또한 잘 될 수있다

Current set element is 3 
Current set element is 1 
관련 문제