Java?for循環(huán)標(biāo)簽跳轉(zhuǎn)到指定位置的示例詳解
Java for循環(huán)標(biāo)簽跳轉(zhuǎn)到指定位置
大家是否見過這種for循環(huán),在for循環(huán)前加了個標(biāo)記的:
outerLoop:
for (; ; ) {
for (; ; ) {
break outerLoop;
}
}我之前有一次在公司業(yè)務(wù)代碼中見過有這種寫法的,沒在意,今天在看JDK線程池的代碼時,又看到ThreadPoolExecutor的addWorker方法中有這種寫法。于是就查了相關(guān)資料,也比較簡單。
總結(jié)下它的用法吧:
- 上面代碼中的
outerLoop是一個標(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就可以實現(xiàn);而continue outerLoop的作用是結(jié)束外層的本次循環(huán),繼續(xù)外層的下一次循環(huán)。
舉個例子:
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í)行邏輯為:
- 第一個
if跳出當(dāng)前內(nèi)層循環(huán),會打印str.equals(1); - 第二個
if執(zhí)行外層for循環(huán)的下一次循環(huán); - 最后一次循環(huán),
str的值為3,跳出外層循環(huán),結(jié)束整個循環(huán),然后打印str.equals(3)。
這種for加標(biāo)簽的寫法確實很少見,學(xué)Java的時候都沒學(xué)這個東西,實際寫業(yè)務(wù)代碼的時候能避免就避免,內(nèi)層循環(huán)能抽就抽個方法。如果業(yè)務(wù)太復(fù)雜抽不了,這種寫法也不失為一種策略。
補充: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)完畢時需要跳出最外層循環(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)文章
使用SpringBoot開發(fā)Restful服務(wù)實現(xiàn)增刪改查功能
Spring Boot是由Pivotal團(tuán)隊提供的全新框架,其設(shè)計目的是用來簡化新Spring應(yīng)用的初始搭建以及開發(fā)過程。這篇文章主要介紹了基于SpringBoot開發(fā)一個Restful服務(wù),實現(xiàn)增刪改查功能,需要的朋友可以參考下2018-01-01
利用MyBatis進(jìn)行不同條件的like模糊查詢的方法
這篇文章主要介紹了利用MyBatis進(jìn)行不同條件的like模糊查詢,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08
Java 將字符串動態(tài)生成字節(jié)碼的實現(xiàn)方法
本篇文章主要是對Java將字符串動態(tài)生成字節(jié)碼的實現(xiàn)方法進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01
淺析Java中Apache BeanUtils和Spring BeanUtils的用法
這篇文章主要介紹了Java中Apache BeanUtils和Spring BeanUtils的用法,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11

