7.14 一维数组的使用和排序算法

一维数组的使用

数组的声明、分配空间、赋值与使用

public class Demo1 {
    public static void main(String[] args) {
        //声明
        int[] a;
        String b[];
        double[] c;
        //分配空间,使用new关键字来分配空间,new关键字是java中优先级最高的
        a = new int[5];
        b = new String[6];
        c = new double[4];
        //赋值
        a[0] = 10;
        a[1] = 14;
        a[2] = 18;
        a[3] = 24;
        a[4] = 33;
        //声明的同时分配空间
        char[] ch = new char[10];
        float[] f = {11.1f, 22.2f, 33.3f};//声明、分配空间并赋值
        int[] d = new int[]{1, 2, 3};
    }
}

各种状态下的数组地址

public class Demo1_1 {
    public static void main(String[] args) {
        int []a = null;
        String[] b = new String[3];
        char[] c = {'a','b','c'};
        double[] d = {1.1,2.2,3.3};
        System.out.println(a);//直接打印数组的话打印的是数组的地址
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
    }
}

数组的增删改查

增删改查
数组范围内:
增:数组的定义、赋值
删:程序运行结束
改:对数组元素的重新赋值,或者地址改变
查:查看数组的地址和元素的值

长度范围外的:(长度要变化需要新的数组)
增:元素个数的增加
删:元素个数的减少
改:修改其中的元素

数组增加元素

public class Demo4 {
    public static void main(String[] args) {
        int[] a = {99, 85, 82,63,60};
        int[] b = new int[6];
        for (int i = 0; i < a.length; i++) {
            b[i] = a[i];
        }
        System.out.println(Arrays.toString(b));
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个数:");
        int num = sc.nextInt();
        boolean isInsert = false;
        for (int i = a.length - 1; i >= 0; i--) {//从后往前遍历
            if(num > b[i]){
                b[i + 1] = b[i];//下标后移
            }else{
                b[i + 1] = num;
                isInsert = true;
                break;
            }
        }
        if(isInsert == false){
            b[0] = num;
        }
        System.out.println(Arrays.toString(b));
    }
}

从数组中删除一个元素

public class Test03 {
    public static void main(String[] args) {
        int[] a = {99, 85, 82, 63, 60};
        int[] b = new int [4];
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的下标:");
        int num = sc.nextInt();
        int j = 0;
        for (int i = 0; i < a.length; i++) {
            if(i == num)continue;
            b[j] = a[i];
            j ++;
        }
        System.out.println(Arrays.toString(b));
    }
}

选择排序

public class Demo5 {
    public static void main(String[] args) {
     /*选择排序逻辑:
     * 从前向后对每一个下标对应的基准作为基准位
     * 与后面所有的元素进行比较,找出最小值
     * 与当前的下标的元素进行交换
     * 进行下一次循环
     * */

        int [] a = {1,3,2,5,4,6};
        for (int i = 0; i < a.length - 1; i++) {
            int b = 0;
            for (int j = i; j < a.length; j++) {
                if(a[i] > a[j]){
                    b = a[i];
                    a[i] = a[j];
                    a[j] = b;
                }
            }
        }
        System.out.println(Arrays.toString(a));
        for (int i = 0; i < a.length - 1; i++) {
            int min = a[i];
            int tmp = i;
            for (int j = i; j < a.length; j++) {
                if(min > a[j]){
                    min = a[j];
                    tmp = j;
                }
            }
            a[tmp] = a[i];
            a[i] = min;
        }
        System.out.println(Arrays.toString(a));
    }
}

插入排序

public class Demo6 {
    public static void main(String[] args) {
        /*插入排序
        *逻辑:从第二位开始向前比较进行插入逻辑
        * */
        int [] a = {1,3,2,5,4,6};
        for (int i = 1; i < a.length; i++) {
            int tmp = a[i];
            boolean isInsert = false;
            for (int j = i - 1; j >= 0 ; j--) {
                //如果遇到比自己大的,大的值就后移
                if(tmp < a[j]){
                    a[j + 1] = a[j];
                }else{//如果遇到比自己小的,就在后方插入
                    a[j + 1] = tmp;
                    isInsert = true;
                    break;
                }

            }
            //如果一直未插入,就插入首位
            if(!isInsert){
                a[0] = tmp;
            }
        }
        System.out.println(Arrays.toString(a));
    }
}

数组练习

1、计算30位同学的平均分,同时打印每一个分数

public class Test1 {
    public static void main(String[] args) {
        int[] scores = new int[30];
        for (int i = 0; i < 30; i++) {
            //使用随机数对数组赋值
            scores[i] = (int)(Math.random()*80 + 20);
        }
        //遍历查看数组中每一个元素
        System.out.print("[");
        for (int i = 0; i < scores.length; i++) {
            System.out.print(scores[i] + ",");
        }
        System.out.print("]");
        //计算平均分
        double sum = 0;
        for (int i = 0; i < scores.length; i++) {
            sum += scores[i];
        }
        System.out.println("\n总分为:" + sum);
        System.out.println("平均分为:" + (sum / scores.length));
        for (int a : scores){//增强for的用法
            System.out.print(a + ",");
        }
    }
}

2、求一个数组中数的总和,并且用键盘输入一个数判断该数是否在数组内。

public class Test2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int [] a = {8,4,2,1,23,344,12};
        int sum = 0;
        for (int b : a) {
            System.out.print(b + ",");
            sum += b;
        }
        System.out.println("数组中所有书的和为:" + sum);
        System.out.println("请输入一个值:");
        int num = sc.nextInt();
        for (int i = 0; i < a.length; i++) {
            if(num == a[i]){
                System.out.println("数列中包含这个数");
                break;
            }
            if(i == a.length - 1 && num != a[i]){
                System.out.println("该数列中不包含这个数!!");
            }
        }
    }
}

3、输入五个学生的成绩,存入数组,打印出来,并找出最大值。

public class Test3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] scores = new int[5];
        for (int i = 0; i < scores.length; i++) {
            System.out.println("请输入第" + (i + 1) + "个学生的成绩!");
            scores[i] = sc.nextInt();
        }
        for (int i = 0; i < scores.length; i++) {
            System.out.print(scores[i] + ",");
        }
        System.out.println();
        //求最大值
        int max = scores[0];
        for (int i = 0; i < scores.length; i++) {
            if(scores[i] > max){
                max = scores[i];
            }
        }
        System.out.println("最大值为:" + max);
    }
}

4、输入五个价格,找出最小价格。

public class Test4 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请依次输入五个价格:");
        int[] a = new int[5];
        for (int i = 0; i < 5; i++) {
            a[i] = sc.nextInt();
        }
//        int [] a = {3000,3150,2900,2950};
        int min = a[0];
        for (int i = 0; i < a.length; i++) {
            if(min > a[i]){
                min = a[i];
            }
        }
        System.out.println("最小值为:" + min);
    }
}

5、输入五笔金额,同时打印出,并求出金额总和。

public class Test5 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double[] money = new double[5];
        double sum = 0;
        for (int i = 0; i < money.length; i++) {
            System.out.print("请输入第" + (i + 1) + "笔金额:");
            money[i] = sc.nextDouble();
            sum += money[i];
        }
        System.out.println();
        System.out.println("序号\t\t" + "金额(元)");
        for (int i = 0; i < money.length; i++) {
            System.out.println((i + 1) + "\t\t" + money[i]);
        }
        System.out.println("总金额:\t" + sum);
    }
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值