今天看啥  ›  专栏  ›  林一天327

for循环的三种写法

林一天327  · 简书  ·  · 2019-09-26 14:46

1、遍历循环

for (循环变量类型 循环变量名称;循环条件;更新语句) 循环体

String[] arr = { "a", "b", "c", "d" };

   for (int i = 0; i < arr.length; i++) {
        System.out.println(arr[i]);
    }

打印台
a
b
c
d

2、迭代器循环

String[] arr = { "a", "b", "c", "d" };

  List<String> list = Arrays.asList(arr);

  for (Iterator<String> iterator = list.iterator();iterator.hasNext();) 
{
        System.out.println(iterator.next());
}

控制台信息        

a
b
c
d

3、增强型for循环

for (循环变量类型 循环变量名称 : 要被遍历的对象) 循环体

String[] arr = { "a", "b", "c", "d" };

        for (String a : arr) {
            System.out.println(a);
        }

控制台    
a
b
c
d



原文地址:访问原文地址
快照地址: 访问文章快照