這篇文章將為大家詳細(xì)講解有關(guān)Java遍歷怎么弄,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)企業(yè)建站,十年網(wǎng)站建設(shè)經(jīng)驗(yàn),專注于網(wǎng)站建設(shè)技術(shù),精于網(wǎng)頁(yè)設(shè)計(jì),有多年建站和網(wǎng)站代運(yùn)營(yíng)經(jīng)驗(yàn),設(shè)計(jì)師為客戶打造網(wǎng)絡(luò)企業(yè)風(fēng)格,提供周到的建站售前咨詢和貼心的售后服務(wù)。對(duì)于網(wǎng)站制作、成都網(wǎng)站制作中不同領(lǐng)域進(jìn)行深入了解和探索,創(chuàng)新互聯(lián)在網(wǎng)站建設(shè)中充分了解客戶行業(yè)的需求,以靈動(dòng)的思維在網(wǎng)頁(yè)中充分展現(xiàn),通過(guò)對(duì)客戶行業(yè)精準(zhǔn)市場(chǎng)調(diào)研,為客戶提供的解決方案。package com.zlh; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class TravelseDemo { public static void main(String[] args) { // 數(shù)組遍歷 System.out.println("1 數(shù)組遍歷"); String[] str1 = { "數(shù)", "組", "遍", "歷" }; arrayTravelse(str1); System.out.println("==================="); // 字符串遍歷 System.out.println("2 字符串遍歷"); String str2 = "字符串遍歷"; stringTravelse(str2); System.out.println("==================="); // list遍歷 ArrayListlist = new ArrayList (); list.add("集"); list.add("合"); list.add("遍"); list.add("歷"); // list遍歷-for-each System.out.println("3 list遍歷-for-each"); forTravelse(list); System.out.println("==================="); // list遍歷-迭代器 System.out.println("4 list遍歷-迭代器"); iteratorTravelse(list); System.out.println("==================="); // map遍歷 Map map = new HashMap (); map.put("jack", 1); map.put("tom", 2); map.put("jerry", 3); // map遍歷-for-each與keySet System.out.println("5 map遍歷-for-each與keySet"); forKeySetTravelse(map); System.out.println("==================="); // map遍歷-迭代器與keySet System.out.println("6 map遍歷-迭代器與keySet"); iteratorKeySetTravelse(map); System.out.println("==================="); // map遍歷-for-each與entry System.out.println("7 map遍歷-for-each與entry"); forEntryTravelse(map); System.out.println("==================="); // map遍歷-迭代器與entry System.out.println("8 map遍歷-迭代器與entry"); iteratorEntryTravelse(map); } // 數(shù)組遍歷 private static void arrayTravelse(String[] str) { for (int i = 0; i < str.length; i++) { String j = str[i]; System.out.print(j + " "); } System.out.println(); } // 字符串遍歷 private static void stringTravelse(String str) { for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); System.out.print(c + " "); } System.out.println(); } // for-each循環(huán)遍歷 private static void forTravelse(ArrayList list) { for (String str : list) { System.out.print(str + " "); } System.out.println(); } // 迭代器遍歷 private static void iteratorTravelse(ArrayList list) { Iterator it = list.iterator(); while (it.hasNext()) { String next = it.next(); System.out.print(next + " "); } System.out.println(); } // map遍歷-for-each與keySet private static void forKeySetTravelse(Map map) { Set set = map.keySet(); for (String key : set) { Integer value = map.get(key); System.out.print(key + ":" + value + " "); } System.out.println(); } // map遍歷-迭代器與keySet private static void iteratorKeySetTravelse(Map map) { Set set = map.keySet(); Iterator it = set.iterator(); while(it.hasNext()) { String key = it.next(); Integer value = map.get(key); System.out.print(key + ":" + value + " "); } System.out.println(); } // map遍歷-for-each與entry private static void forEntryTravelse(Map map) { Set > set = map.entrySet(); for (Map.Entry entry : set) { String key = entry.getKey(); Integer value = entry.getValue(); System.out.print(key + ":" + value + " "); } System.out.println(); } // map遍歷-迭代器與entry private static void iteratorEntryTravelse(Map map) { Set > set = map.entrySet(); Iterator > it = set.iterator(); while (it.hasNext()) { Entry en = it.next(); String key = en.getKey(); Integer value = en.getValue(); System.out.print(key + ":" + value + " "); } System.out.println(); } }
關(guān)于Java遍歷怎么弄就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。