Java修炼之凡界篇 筑基期 第05卷 数组 第04话 数组的使用

数组使用

  • 程序案例:

    public class ArrayUse {
        public static void main(String[] args) {
            int[] arrayInt = {4, 7, 1, 3, 5, 8, 9, 2, 6};
            System.out.println("===========打印数组集合===========");
            // 打印数组元素
            for (int i = 0; i < arrayInt.length; i++) {
                System.out.print(arrayInt[i] + "\t");
            }
            System.out.println();
            System.out.println("===========求数组元素和===========");
            // 求数组元素和
            int sum = 0;
            for (int i = 0; i < arrayInt.length; i++) {
                sum += arrayInt[i];
            }
            System.out.println("sum = " + sum);
            System.out.println("===========求数组元素最大===========");
            // 求数组元素最大
            int max = arrayInt[0];
            for (int i = 0; i < arrayInt.length; i++) {
                if (arrayInt[i] > max) {
                    max = arrayInt[i];
                }
            }
            System.out.println("max = " + max);
            System.out.println("=======增强For循环-打印数组元素=======");
            for (int i : arrayInt) {
                System.out.print(i + "\t");
            }
            System.out.println();
            System.out.println("============数组当做入参============");
            printArray(arrayInt);
            System.out.println("============数组排序后返回数组============");
            System.out.println("----降序排序----");
            // 降序排序
            printArray(exeArrayInt(arrayInt, "DESC"));
            System.out.println("----升序排序----");
            // 升序排序
            printArray(exeArrayInt(arrayInt, "ASC"));
        }
        public static void printArray(int[] arrayInt) {
            for (int i : arrayInt) {
                System.out.print(i + "\t");
            }
            System.out.println();
        }
        public static int[] exeArrayInt(int[] arrayInt, String orderBy) {
            // 将数组元素进行排序从大到小
            // 前后数据比较,n个数据需要比较n - 1次
            // 此处需要循环8次,并且每循环一次就需要将已经比较出最大的数据进行排除
            for (int i = 0; i < arrayInt.length - 1; i++) {
                for (int j = 0; j < arrayInt.length - 1 - i; j++) {
                    if ("DESC".equals(orderBy)) {
                        if (arrayInt[j] < arrayInt[j + 1]) {
                            int tempNum = arrayInt[j + 1];
                            arrayInt[j + 1] = arrayInt[j];
                            arrayInt[j] = tempNum;
                        }
                    }
                    if ("ASC".equals(orderBy)) {
                        if (arrayInt[j] > arrayInt[j + 1]) {
                            int tempNum = arrayInt[j + 1];
                            arrayInt[j + 1] = arrayInt[j];
                            arrayInt[j] = tempNum;
                        }
                    }
                }
            }
            return arrayInt;
        }
    }
    // --------------------------------------
    // 控制栏输出打印:
    /**
    ===========打印数组集合===========
    4	7	1	3	5	8	9	2	6	
    ===========求数组元素和===========
    sum = 45
    ===========求数组元素最大===========
    max = 9
    =======增强For循环-打印数组元素=======
    4	7	1	3	5	8	9	2	6	
    ============数组当做入参============
    4	7	1	3	5	8	9	2	6	
    ============数组排序后返回数组============
    ----降序排序----
    9	8	7	6	5	4	3	2	1	
    ----升序排序----
    1	2	3	4	5	6	7	8	9
    */
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

莫在问

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

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

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

打赏作者

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

抵扣说明:

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

余额充值