Java?for循環(huán)標(biāo)簽跳轉(zhuǎn)到指定位置的示例詳解
Java for循環(huán)標(biāo)簽跳轉(zhuǎn)到指定位置
大家是否見(jiàn)過(guò)這種for循環(huán),在for循環(huán)前加了個(gè)標(biāo)記的:
outerLoop:
for (; ; ) {
for (; ; ) {
break outerLoop;
}
}我之前有一次在公司業(yè)務(wù)代碼中見(jiàn)過(guò)有這種寫(xiě)法的,沒(méi)在意,今天在看JDK線程池的代碼時(shí),又看到ThreadPoolExecutor的addWorker方法中有這種寫(xiě)法。于是就查了相關(guān)資料,也比較簡(jiǎ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),會(huì)打印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)簽的寫(xiě)法確實(shí)很少見(jiàn),學(xué)Java的時(shí)候都沒(méi)學(xué)這個(gè)東西,實(shí)際寫(xiě)業(yè)務(wù)代碼的時(shí)候能避免就避免,內(nèi)層循環(huán)能抽就抽個(gè)方法。如果業(yè)務(wù)太復(fù)雜抽不了,這種寫(xiě)法也不失為一種策略。
補(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)的的地方這么來(lái)寫(xiě)
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis映射文件中的動(dòng)態(tài)SQL實(shí)例詳解
在本文中,我們深入探討了動(dòng)態(tài)SQL的各種標(biāo)簽,包括<if>、<choose>、<trim>、<foreach>等,通過(guò)實(shí)際的例子演示了它們的用法,感興趣的朋友一起揭開(kāi)動(dòng)態(tài)SQL的神秘面紗,帶你領(lǐng)略它的魅力2024-01-01
使用SpringBoot開(kāi)發(fā)Restful服務(wù)實(shí)現(xiàn)增刪改查功能
Spring Boot是由Pivotal團(tuán)隊(duì)提供的全新框架,其設(shè)計(jì)目的是用來(lái)簡(jiǎn)化新Spring應(yīng)用的初始搭建以及開(kāi)發(fā)過(guò)程。這篇文章主要介紹了基于SpringBoot開(kāi)發(fā)一個(gè)Restful服務(wù),實(shí)現(xiàn)增刪改查功能,需要的朋友可以參考下2018-01-01
利用MyBatis進(jìn)行不同條件的like模糊查詢的方法
這篇文章主要介紹了利用MyBatis進(jìn)行不同條件的like模糊查詢,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Redis內(nèi)存數(shù)據(jù)庫(kù)示例分析
Redis本身的內(nèi)容比較復(fù)雜。如果你上來(lái),你應(yīng)該研究一個(gè)細(xì)節(jié)點(diǎn),比如連接池和數(shù)據(jù)結(jié)構(gòu)。雖然可以直接了解某一點(diǎn)的詳細(xì)來(lái)源內(nèi)容,甚至盡快解決一些意外,但是容易淹沒(méi)在失眠的細(xì)節(jié)中,整體控制不了Redis2022-12-12
Java 將字符串動(dòng)態(tài)生成字節(jié)碼的實(shí)現(xiàn)方法
本篇文章主要是對(duì)Java將字符串動(dòng)態(tài)生成字節(jié)碼的實(shí)現(xiàn)方法進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01
淺析Java中Apache BeanUtils和Spring BeanUtils的用法
這篇文章主要介紹了Java中Apache BeanUtils和Spring BeanUtils的用法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
通過(guò)Java實(shí)現(xiàn)自己動(dòng)手寫(xiě)ls命令
在前面的文章中,我們仔細(xì)的介紹了關(guān)于ls命令的使用和輸出結(jié)果,在本篇文章當(dāng)中我們用Java代碼自己實(shí)現(xiàn)ls命令,更加深入的了解ls命令2022-10-10

