集合遍历

    xiaoxiao2023-03-24  3

    //set集合遍历 第一种 跌代器 <ol start="1" class="dp-j" style="line-height: 1.4em; margin: 0px 0px 1px; padding: 2px 0px; border: 1px solid rgb(209, 215, 220); color: rgb(43, 145, 175); font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace;"><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><span style="color: black;">Set<String> set = <span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">new</span> HashSet<String>();  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><span style="color: black;">Iterator<String> it = set.iterator();  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><span style="color: black;"><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">while</span> (it.hasNext()) {  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><span style="color: black;">  String str = it.next();  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><span style="color: black;">  System.out.println(str);  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><span style="color: black;">} </span></li></ol> 第二种 forEach <ol start="1" class="dp-j" style="line-height: 1.4em; margin: 0px 0px 1px; padding: 2px 0px; border: 1px solid rgb(209, 215, 220); color: rgb(43, 145, 175); font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace;"><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><span style="color: black;"><span class="keyword" style="color: rgb(127, 0, 85); font-weight: bold;">for</span> (String str : set) {  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><span style="color: black;">      System.out.println(str);  </span></li><li style="font-size: 1em; margin: 0px 0px 0px 38px; padding: 0px 0px 0px 10px; border-left-width: 1px; border-left-style: solid; border-left-color: rgb(209, 215, 220); line-height: 18px; background-color: rgb(250, 250, 250);"><span style="color: black;">} </span></li></ol> //list集合遍历 <ol class="dp-j" style="padding-left: 40px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px;"><li style="list-style-type: decimal;"><span style="line-height: 1.5;"> List list = </span><span class="keyword" style="line-height: 1.5;">new</span><span style="line-height: 1.5;"> ArrayList();   </span></li><li style="list-style-type: decimal;"><span style="line-height: 1.5;">        list.add(</span><span class="string" style="line-height: 1.5;">"gouchao"</span><span style="line-height: 1.5;">);   </span></li><li style="list-style-type: decimal;"><span style="line-height: 1.5;">        list.add(</span><span class="string" style="line-height: 1.5;">"gouli"</span><span style="line-height: 1.5;">);   </span></li></ol> 第一种 跌代器 <ol class="dp-j" style="padding-left: 40px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px;"><li style="list-style-type: decimal;"><span style="line-height: 1.5;">Iterator it = list.iterator();   </span></li><li style="list-style-type: decimal;"><span style="line-height: 1.5;">        </span><span class="keyword" style="line-height: 1.5;">while</span><span style="line-height: 1.5;">(it.hasNext()){   </span></li><li style="list-style-type: decimal;"><span style="line-height: 1.5;">            System.out.println(it.next());   </span></li><li style="list-style-type: decimal;"><span style="line-height: 1.5;">        }  </span></li></ol> 第二种 froEach <ol class="dp-j" style="padding-left: 40px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px;"><li style="list-style-type: decimal;"><span style="line-height: 1.5;"> </span><span class="keyword" style="line-height: 1.5;">for</span><span style="line-height: 1.5;">(Object o:list){   </span></li><li style="list-style-type: decimal;"><span style="line-height: 1.5;">            System.out.println(o);   </span></li><li style="list-style-type: decimal;"><span style="line-height: 1.5;">        }  </span></li></ol> 第三种 for循环 (通过下标,比set集合多了一种) <ol class="dp-j" style="padding-left: 40px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px;"><li style="list-style-type: decimal;"><span style="line-height: 1.5;"> </span><span class="keyword" style="line-height: 1.5;">for</span><span style="line-height: 1.5;">(</span><span class="keyword" style="line-height: 1.5;">int</span><span style="line-height: 1.5;"> i = </span><span class="number" style="line-height: 1.5;">0</span><span style="line-height: 1.5;"> ; i< list.size();i++){   </span></li><li style="list-style-type: decimal;"><span style="line-height: 1.5;">               </span></li><li style="list-style-type: decimal;"><span style="line-height: 1.5;">            System.out.println(list.get(i));   </span></li><li style="list-style-type: decimal;"><span style="line-height: 1.5;">        }</span></li></ol> //map集合遍历 第一种 keySet() 第二种 entrySet() 参考: http://www.cnblogs.com/kristain/articles/2033566.html

    转载请注明原文地址: https://ju.6miu.com/read-1201418.html
    最新回复(0)