码迷,mamicode.com
首页 > 编程语言 > 详细

Java For Loop

时间:2018-08-21 17:14:52      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:bre   ==   junit   simple   example   死循环   port   using   http   

Java For Loop

Simple For Loop

技术分享图片

For-each or Enhanced For Loop

技术分享图片

Labeled For Loop

技术分享图片

Example

import java.util.Arrays;
import java.util.List;

import org.junit.Test;

public class ForExample {
    @Test
    public void test() {
        // Simple For Loop
        for (int i = 1; i <= 10; i++) {
            System.out.println(i);
        }
        System.out.println("---------------------");
    }

    @Test
    public void test1() {
        int arr[] = { 12, 23, 44, 56, 78 };
        // Printing array using for-each loop
        for (int i : arr) {
            System.out.println(i);
        }
        System.out.println("---------------------");
    }

    @Test
    public void test2() {
        List<Integer> list = Arrays.asList(12, 23, 44, 56, 78);
        list.stream().forEach((i) -> System.out.println(i));
        System.out.println("---------------------");
    }

    @Test
    public void test3() {
        for (int i = 1; i <= 3; i++) {
            bb: for (int j = 1; j <= 3; j++) {
                if (i == 2 && j == 2) {
                    break bb;
                }
                System.out.println(i + " " + j);
            }
        }
        System.out.println("---------------------");
    }

    @Test
    public void test4() {
        aa: for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 3; j++) {
                if (i == 2 && j == 2) {
                    break aa;
                }
                System.out.println(i + " " + j);
            }
        }
    }

    @Test
    public void test5() {
        // 死循环
        for (;;) {
            System.out.println("infinitive loop");
        }
    }

}

Java For Loop

标签:bre   ==   junit   simple   example   死循环   port   using   http   

原文地址:https://www.cnblogs.com/hglibin/p/9512309.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!