Night小课堂18 Java数组

问:为什么需要数组?

答:当功能模块中变量的初始化过多的时候,需要有一个有序系列能够容纳多个变量

数组定义:

(1)数组是一个变量,存储相同数据类型的一组数据

(2)数组是在内存空间划出一串连续的空间

数组基本要素:

标识符:数组的名称

数组元素:数组中存放的数据

元素下标:对数组元素进行编号,从0开始,数组中每个元素都可以通过下标进行访问

元素类型:数组元素的数据类型

数组的语法结构:

1.创建数组的方式:

静态初始化:int[ ] array = new int [ ]{1,2,3,4,5} 

动态初始化:int[ ] array = new int[5];

                      array[0] = 1;

                      array[1] = 2;

                      array[2] = 3;

                      array[3] = 4;

                      array[4] = 5;

实战:

package cn.csc.test;

import java.util.Scanner;

public class Demo7 {
    public static void main(String[] args) {
        //1.定义数组
        int[] scores = new int[5];
        //2.定义变量
        double sum = 0;
        double avg = 0;

        //3.循环输入数值至数组
        System.out.println("请输入5位学员的成绩");
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < scores.length; i++) {
            scores[i] = input.nextInt();
            sum = sum + scores[i];
        }
        avg = sum/scores.length;
        System.out.println("5位学生的平均分是:"+avg);
    }
}

拓展:在数组中未给某个位置赋值,则系统会给默认值,默认值为0

拓展2:遍历数组的两种方式

package cn.csc.test;

public class Demo23 {
    public static void main(String[] args) {
        int[] array = new int [5];
        array[0] = 10;
        array[1] = 20;
        array[2] = 30;
        array[3] = 50;
        array[4] = 100;
        
        //传统for循环遍历数组
        for (int i=0; i<array.length;i++){
            System.out.println(array[i]+"\t");
        }
        System.out.println();
        
        //增强for循环遍历数组
        for (int a: array){
            System.out.println(a+"\t");
        }
        
    }
}

Arrays工具类使用

arrays描述:

(1)java.util.Arrays

(2)sort( )升序查询

实战:循环录入5位学生成绩,进行升序排列后输出结果

package cn.csc.test;

import java.util.Arrays;
import java.util.Scanner;

public class Demo1 {
    public static void main(String[] args) {
        //1.定义数据
        int[] scores = new int[5];
        System.out.println("请输入5位学员得成绩:");
        Scanner scanner = new Scanner(System.in);
        //2.循环输出数组中得数据
        for(int i =0;i<scores.length;i++){
        scores[i] = scanner.nextInt();
        }
        //3.循环输出遍历数组
//        for(int i =0;i<scores.length;i++){
//            System.out.println(scores[i]);
//        }
        //4.对数组进行升序排列
        Arrays.sort(scores);
        //5.循环输出遍历数组
        System.out.println("学员成绩按");
        for (int i = 0;i<scores.length;i++){
            System.out.println(scores[i]);
        }
    }
}

数组的应用:

求最大值:已知数组存储5位学员成绩,求最大值

package cn.csc.test;

import java.util.Scanner;

public class Demo2 {
    public static void main(String[] args) {
        int [] a =new int[5];
        int max = 0;
        System.out.println("请输入5位学员得成绩:");
        Scanner scanner = new Scanner(System.in);

        for (int i=0;i<a.length;i++){
            a[i] = scanner.nextInt();
        }

        max = a[0];
        for (int i=1;i<a.length;i++){
            if (a[i]>max){
                max = a[i];
            }
        }
        System.out.println("成绩最高分是:"+max);
    }
}

冒泡排序:已知数组存储多位学员成绩,求从小到大排列顺序

package cn.csc.test;

public class Demo3 {
    public static void main(String[] args) {
        int[] a =new int[]{6,5,48,86,1};
        for (int i =0;i<a.length-1;i++){
            for (int j =0;j<a.length-1-i;j++){
                if (a[j]>a[j+1]){
                    int temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                }
            }
        }
        System.out.println("冒泡排序后:");
        for (int b:a){
            System.out.println(b+"\t");
        }
    }
}

二维数组:

定义:二维数组其实就是每个元素为一个一维数组得数组

语法结构:(两种写法)

(1)int[ ] [ ] scores = new int [5] [50];

(2)int scores[ ] [ ] = new int [5] [50];

细节描述:

[5][50]表示二维数组里包含了5个一维数组,每个一维数组里又包含了50个元素

[5][ ]表示二维数组里包含了5个一维数组,每个一维数组长度不确定

实战:有5个班各5名学生某门课程的成绩,计算5个班各自的总成绩

package cn.csc.test;

import java.util.Scanner;

public class Demo4 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int [][] array =new int[5][5];
        for (int i =0;i<array.length;i++){
            System.out.println("第"+(i+1)+"个班级");
            for (int j=0;j<array[i].length;j++){
                System.out.println("请输入第"+(j+1)+"个学生的成绩:");
                array[i][j] = scanner.nextInt();
            }
        }
        System.out.println("***************统计***************");
        int total = 0;
        for (int i =0;i<array.length;i++){
            String str =(i+1)+"班";
            total = 0;
            for (int j = 0;j<array[i].length;j++){
                total+=array[i][j];
            }
            System.out.println(str+"总成绩:"+total);
        }

    }
}

本人才疏学浅,文中如有出现错误请多多谅解

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值