日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

for-each循环的认识、定义、适用对象、举例、局限性

發布時間:2025/3/8 55 豆豆
生活随笔 收集整理的這篇文章主要介紹了 for-each循环的认识、定义、适用对象、举例、局限性 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 1. for-each的認識
    • 2. for-each的定義
    • 3. 哪些類型的對象可以適用For-Each?
    • 4. for-each的舉例
    • 5. for-each的局限性

1. for-each的認識

(1)for-each語句是Java中for-Index的一種加強,是Java 5帶來的新語法糖。
(2)for-each語句是在遍歷數組、集合的時候,foreach擁有不錯的性能。
(3)foreach是for語句的簡化,但是for-each并不能替代for循環。任何for-each都能改寫為for循環,反之不行。
(4)foreach不是java中的關鍵字。for-each的循環對象一般是一個集合,List、ArrayList、LinkedList、Vector、數組等。

2. for-each的定義

for(元素類型T 每次循環元素的名稱 : 循環對象){//對定義的名稱進行操作}

3. 哪些類型的對象可以適用For-Each?

(1)數組
(2)Collection類
(3)任何實現了Iterable接口的自定義類
(根據面向接口的思想,Deolin習慣把第三類對象稱之為“可迭代的”對象)

第一類,第二類在實際開發中經常用到,而第三類能夠適用For-Each的原因需要通過源碼來進行分析。

4. for-each的舉例

  • for-each遍歷數組
  • public static void main(String[] args) {String[] demo = {"a", "b", "c"};for (String t :demo ) {System.out.println(t);}}
  • for-each遍歷List
  • public static void main(String[] args) {List<String> list = new ArrayList();list.add("a");list.add("b");list.add("c");for(String str : list){System.out.println(str);}}

    5. for-each的局限性

    for-each雖然能遍歷數組或者集合,但是只能用來遍歷,無法在遍歷的過程中對數組或者集合進行修改,而for循環可以在遍歷的過程中對源數組或者集合進行修改。

    1 數組

    import java.util.ArrayList; import java.util.Arrays; import java.util.List;public class test{public static void main(String[] args) {String[] names = {"ming", "fei"};for (String name : names) {name = "xin";}//foreachSystem.out.println("foreach:"+Arrays.toString(names));//forfor (int i = 0; i < names.length; i++) {names[i] = "xin";}System.out.println("for:"+Arrays.toString(names));}}

    2.集合

    import java.util.ArrayList; import java.util.Arrays; import java.util.List;public class test {public static void main(String[] args) {List<String> names = new ArrayList<String>();names.add("ming");names.add("fei");//foreachfor(String name:names){name = "uzi";}System.out.println(Arrays.toString(names.toArray()));//forfor (int i = 0; i < names.size(); i++) {names.set(i,"uzi");}System.out.println(Arrays.toString(names.toArray()));} }

    求出一個文件的目錄名以及目錄總個數
    實現代碼:

    import java.io.File;public class Demo {public static void main(String[] args){File file=new File("D:/apache-maven1");String[] files=file.list();for(int i=0;i<files.length;i++){System.out.println(files[i]);}System.out.println(files.length);} }

    獲取本地IP地址

    實現代碼如下:

    import java.net.InetAddress; import java.net.UnknownHostException;public class Demo {public static void main(String[] args) throws UnknownHostException {InetAddress host = InetAddress.getLocalHost();String ip =host.getHostAddress();System.out.println("本機ip地址:" + ip);} }

    自定義多個字符和數字,求出6位隨機數的組合
    實現代碼:

    import java.util.Random;public class Demo {static char[] chars = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};public static String getStr(int i){StringBuffer string = new StringBuffer();Random random = new Random();while(i-->0){//獲取隨機生成的下標int x = random.nextInt(chars.length);string.append(chars[x]);}return string.toString();}public static void main(String[] args) throws Exception {int i=6;String string = getStr(i);System.out.println(string);}} 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

    總結

    以上是生活随笔為你收集整理的for-each循环的认识、定义、适用对象、举例、局限性的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。