求峰值、自定义倒序、冒泡排序、标记标签跳出for循环的简单应用

一、求峰值、自定义倒序、冒泡排序、递归、标记标签跳出for循环的简单应用

1、求峰值

//求峰值
    private static void getPeakValue(int[] a) {
        List list = new ArrayList<>();
        for (int i = 1; i < a.length - 1; i++) {
            if (a[i] > a[i - 1] && a[i] > a[i + 1]) {
                list.add(a[i]);
            }
        }
        System.out.print("峰值为:");
        list.forEach(l -> System.out.print(l + " "));
    }

2、自定义倒序

//自定义倒序算法
    public static void reverseOrder(int[] a) {
        int[] b = new int[a.length];
        for (int i = a.length - 1, j = 0; i >= 0; i--, j++) {
            b[j] = a[i];
        }
        System.out.print("倒序后:");
        for (int i = 0; i < b.length; i++) {
            System.out.print(b[i] + " ");
        }
    }

3、冒泡排序

//冒泡排序
    public static void bubbleSorting(int[] a) {
        for (int j = 0; j < a.length - 1; j++) {
            for (int i = 0; i < a.length - 1; i++) {
                if (a[i] > a[i + 1]) {
                    //中间量
                    int flag = a[i];
                    a[i] = a[i + 1];
                    a[i + 1] = flag;
                }
            }
        }
        System.out.print("冒泡排序后:");
		for (int i : a) {
            System.out.print(i+" ");
        }
    }

4、递归(自己调用自己)

//递归求阶乘
    private static int jc(int n){
        if(n==1){
            return 1;
        }
        return n*jc(n-1);
    }

5、标记标签跳出for循环

//求1-18之间的偶数为(用标记标签跳出for循环)
    private static void outFor() {
        List<Integer> list = new ArrayList<>();
        boolean flag = true;
        while (flag) {
            int n = 20;
            A:
            for (int i = 1; i < n; i++) {
                if (i % 2 == 0) {
                    list.add(i);
                    continue;
                }
                //i=18的时候,跳出循环
                if (i == 18) {
                    break A;
                }
            }
            flag = false;
        }
        Iterator<Integer> iterator = list.iterator();
        System.out.print("1-18之间的偶数为:");
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + " ");
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

楚风岸影

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值