Java?for循環(huán)標(biāo)簽跳轉(zhuǎn)到指定位置的示例詳解
Java for循環(huán)標(biāo)簽跳轉(zhuǎn)到指定位置
大家是否見過這種for循環(huán),在for循環(huán)前加了個(gè)標(biāo)記的:
outerLoop: for (; ; ) { for (; ; ) { break outerLoop; } }
我之前有一次在公司業(yè)務(wù)代碼中見過有這種寫法的,沒在意,今天在看JDK線程池的代碼時(shí),又看到ThreadPoolExecutor
的addWorker
方法中有這種寫法。于是就查了相關(guān)資料,也比較簡單。
總結(jié)下它的用法吧:
- 上面代碼中的
outerLoop
是一個(gè)標(biāo)記外層for
循環(huán)的標(biāo)簽,它可以隨便命名。 - 該標(biāo)簽主要用于for循環(huán)嵌套的情況,結(jié)合
break
和continue
跳轉(zhuǎn)到外層for循環(huán);
我們知道,break
的作用是跳出當(dāng)前循環(huán),continue
的作用是結(jié)束本次循環(huán),繼續(xù)下次循環(huán)。如果有雙層for循環(huán),在內(nèi)層的for循環(huán)中,想直接跳出所有循環(huán),使用break outerLoop
就可以實(shí)現(xiàn);而continue outerLoop
的作用是結(jié)束外層的本次循環(huán),繼續(xù)外層的下一次循環(huán)。
舉個(gè)例子:
public static void main(String[] args) { String[] strings = {"1", "2", "3"}; outerLoop: for (String str : strings) { for (; ; ) { if (str.equals("1")) { break; } if (str.equals("2")) { continue outerLoop; } if (str.equals("3")) { break outerLoop; } } System.out.println("str.equals(1)"); } System.out.println("str.equals(3)"); }
上面代碼中雙重for循環(huán),執(zhí)行邏輯為:
- 第一個(gè)
if
跳出當(dāng)前內(nèi)層循環(huán),會打印str.equals(1)
; - 第二個(gè)
if
執(zhí)行外層for循環(huán)的下一次循環(huán); - 最后一次循環(huán),
str
的值為3,跳出外層循環(huán),結(jié)束整個(gè)循環(huán),然后打印str.equals(3)
。
這種for加標(biāo)簽的寫法確實(shí)很少見,學(xué)Java的時(shí)候都沒學(xué)這個(gè)東西,實(shí)際寫業(yè)務(wù)代碼的時(shí)候能避免就避免,內(nèi)層循環(huán)能抽就抽個(gè)方法。如果業(yè)務(wù)太復(fù)雜抽不了,這種寫法也不失為一種策略。
補(bǔ)充:java for 循環(huán)continue 跳轉(zhuǎn)到外層
for (int i = 0; i < cardRecordsList.size(); i++) { BomCardRecords bomCardRecords = cardRecordsList.get(i); String recordsContent = bomCardRecords.getRecordsContent(); if (i == 0){ recordsContent += "$$$狀態(tài)"; } String[] contentArr = recordsContent.split("\\$\\$\\$", -1); List<String> needData = new ArrayList<>(); for (int j = 0; j < contentArr.length; j++) { String contentColumn = contentArr[contentArr.length - 1]; if (StringUtils.isBlank(state)) { clearUpData(columns, partList, contentArr, columnArr, needData); continue; } else { String[] stateArr = state.split(" "); List<String> stateList = Arrays.asList(stateArr); contentColumn = contentColumn.split(",")[0]; if (contentColumn.equals("狀態(tài)") || stateList.contains(contentColumn)) { clearUpData(columns, partList, contentArr, columnArr, needData); continue; } } } }
continue 跳出循環(huán)
如上代碼我們是嵌套循環(huán) , 當(dāng)我們循環(huán)完畢時(shí)需要跳出最外層循環(huán) , 我們只需要在跳轉(zhuǎn)的的地方這么來寫
my: for (int i = 0; i < cardRecordsList.size(); i++) { BomCardRecords bomCardRecords = cardRecordsList.get(i); String recordsContent = bomCardRecords.getRecordsContent(); if (i == 0){ recordsContent += "$$$狀態(tài)"; } String[] contentArr = recordsContent.split("\\$\\$\\$", -1); List<String> needData = new ArrayList<>(); for (int j = 0; j < contentArr.length; j++) { String contentColumn = contentArr[contentArr.length - 1]; if (StringUtils.isBlank(state)) { clearUpData(columns, partList, contentArr, columnArr, needData); continue my; } else { String[] stateArr = state.split(" "); List<String> stateList = Arrays.asList(stateArr); contentColumn = contentColumn.split(",")[0]; if (contentColumn.equals("狀態(tài)") || stateList.contains(contentColumn)) { clearUpData(columns, partList, contentArr, columnArr, needData); continue my; } } } }
這樣我們就可continue到最外層循環(huán)了
到此這篇關(guān)于Java for循環(huán)標(biāo)簽跳轉(zhuǎn)到指定位置的文章就介紹到這了,更多相關(guān)java跳轉(zhuǎn)到指定位置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
zookeeper+Springboot實(shí)現(xiàn)服務(wù)器動(dòng)態(tài)上下線監(jiān)聽教程詳解
這篇文章主要介紹了zookeeper+Springboot實(shí)現(xiàn)服務(wù)器動(dòng)態(tài)上下線監(jiān)聽,主要介紹了什么是服務(wù)器動(dòng)態(tài)上下線監(jiān)聽及為什么要實(shí)現(xiàn)對服務(wù)器上下線的監(jiān)聽,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06Java后端WebSocket的Tomcat實(shí)現(xiàn)
這篇文章主要介紹了Java后端WebSocket的Tomcat實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06Java實(shí)現(xiàn)文件壓縮為zip和解壓zip壓縮包
這篇文章主要為大家介紹了Java如何實(shí)現(xiàn)將文件壓縮為zip以及解壓zip壓縮包,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手嘗試一下2022-06-06關(guān)于Springboot+gateway整合依賴并處理依賴沖突問題
這篇文章主要介紹了Springboot+gateway整合依賴并處理依賴沖突問題,給大家提到了spring boot版本和spring cloud版本,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01