Java入门学习笔记 -- Day4

Java入门学习笔记 – Day4

第八章 一维数组

8.0 数组概述-静态初始化

  1. 案例: 随机点名小脚本
package com.pdwei;

public class ArrayDemo1 {
    public static void main(String[] args) {
        // 目标: 认识使用数组的好处,数组的定义,访问
        randomCall();
    }

    // 15名学生
    public static void randomCall()
    {
        // 1. 定义一个数组,存储15个学生的姓名
        // 静态初始化数组,定义数组的时候,数据已经确定好了
        String[] names = { "张伟", "王芳", "李娜", "刘洋", "陈静", "杨柳", "黄志强", "周敏", "徐丽", "吴勇", "赵娟", "孙浩", "胡芳", "郭涛", "何磊"};
//        String[] names = new String[]{ "张伟", "王芳", "李娜", "刘洋", "陈静", "杨柳", "黄志强", "周敏", "徐丽", "吴勇", "赵娟", "孙浩", "胡芳", "郭涛", "何磊"};
//        String[] names[] = new String[]{ "张伟", "王芳", "李娜", "刘洋", "陈静", "杨柳", "黄志强", "周敏", "徐丽", "吴勇", "赵娟", "孙浩", "胡芳", "郭涛", "何磊"};

        // 2. 随机获取一个索引值
        // Math.random() : [0, 1) 之间的小数
        // names.length: 元素个数: 15
        int index = (int)(Math.random() * names.length);  // [0, 14]

        // 3. 根据索引值,获取数据中的元素
        String name = names[index];
        System.out.println(name);
    }
}
  1. 数组是什么?
  • 数组是一个数据容器,可用来存储一批同类型的数据。
// 静态初始化数组,定义时已经确定了数据
数据类型[] 数组名 = {元素1,元素2,元素3...};
int[] arr = {12, 24, 36}
// 完整格式
数据类型[] 数组名 = new 数据类型[] {元素1,元素2,元素3...};
int[] arr = new int[]{12, 24, 36}

M a r k :   \mathcal{Mark}:~ Mark: 数据类型[] 数组名也可以写成数据类型 数组名[]

  1. 数组的访问
数组名[索引]
System.out.println(arr[0]);  // 12
System.out.println(arr[1]);  // 24
arr[1] = 100;
  1. 获取数组的长度(元素个数)
// 获取数组的长度(就是数组元素的个数)
System.out.println(arr.length);  // 3

8.1 动态初始化

  1. 案例
package com.pdwei;

import java.util.Scanner;

public class ArrayDemo2 {
    public static void main(String[] args) {
        // 目标: 掌握数组的另一种定义方式,动态初始化数组
        inputScore();
    }

    public static void inputScore()
    {
        // 1. 你需要一个数组来存储8名学生的成绩
        // 动态初始化数组,只确定数组的类型和存储数据的容量,不事先存入具体的数据。
        // 数据类型[] 数组名 = new 数据类型[长度];
        double[] scores = new double[8];
        // scores = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
        //            0    1    2    3    4    5    6    7

        // 2. 录入8名学生的成绩,存入列数据中去
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < scores.length; i++)
        {
            // i = 0 1 2 3 4 5 6 7
            System.out.println("请输入第" + (i+1) + "个学生的成绩: ");
            scores[i] = sc.nextDouble();
        }

        // 3. 遍历数据,统计总分,统计最高分,统计最低分
        double allScore = 0.0;
        for (int i = 0; i < scores.length; i++)
        {
            // i = 0 1 2 3 4 5 6 7
            double score = scores[i];
            // 4. 累加当前遍历到的这个分数
            allScore += score;
        }
        System.out.println("平均分" + allScore / scores.length);
    }
}
  1. 动态初始化数组
  • 定义数组时先不存入具体的元素值,只确定数组存储的数据类型和数组的长度
数据类型[] 数组名 = new 数据类型[长度];
int[] arr = new int[3];
  • 动态初始化数组元素默认值规则:
数据类型明细默认值
基本类型byte、short、char、int、long0
基本类型float、double0.0
基本类型booleanfalse
引用类型类、接口、数组、Stringnull
  1. 数组的遍历
  • 什么是遍历?
    就是一个一个数据的访问
  1. 求最值案例
package com.pdwei;

import java.util.Scanner;

public class ArrayDemo2 {
    public static void main(String[] args) {
        // 目标: 掌握数组的另一种定义方式,动态初始化数组
        inputScore();
    }

    public static void inputScore()
    {
        // 1. 你需要一个数组来存储8名学生的成绩
        // 动态初始化数组,只确定数组的类型和存储数据的容量,不事先存入具体的数据。
        // 数据类型[] 数组名 = new 数据类型[长度];
        double[] scores = new double[8];
        // scores = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
        //            0    1    2    3    4    5    6    7

        // 2. 录入8名学生的成绩,存入列数据中去
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < scores.length; i++)
        {
            // i = 0 1 2 3 4 5 6 7
            System.out.println("请输入第" + (i+1) + "个学生的成绩: ");
            scores[i] = sc.nextDouble();
        }

        // 3. 遍历数据,统计总分,统计最高分,统计最低分
        double allScore = scores[0];
        double maxScore = scores[0];
        double minScore = scores[0];

        for (int i = 1; i < scores.length; i++)
        {
            // i = 0 1 2 3 4 5 6 7
            double score = scores[i];
            // 4. 累加当前遍历到的这个分数
            allScore += score;

            // 5. 找最高分
            if (score > maxScore)
            {
                maxScore = score;
            }

            if (score < minScore)
            {
                minScore = score;
            }
        }
        System.out.println("平均分:" + allScore / scores.length);
        System.out.println("最高分:" + maxScore);
        System.out.println("最低分:" + minScore);
    }
}

8.2 综合案例实战

案例: 简易斗地主游戏

package com.pdwei;

public class ArrayTest4
{
    public static void main(String[] args)
    {
        // 目标: 完成斗地主游戏的做牌和洗牌
        start();
    }

    public static void start()
    {
        // 1. 做牌,创建一个动态初始化的数组存储54张牌
        String[] poker = new String[54];
        // poker = [null, null, ...]
        //           0      1

        // 2. 准备四种花色,还有点数,再楷书做牌存入数组中去
        String[] colors = {"♦", "♠", "♥", "♣"};
        String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};

        // 3. 遍历点数,再遍历花色
        int index = 0;
        for (int i = 0; i < numbers.length; i++)
        {
            for (int j = 0; j < colors.length; j++)
            {
                // 4. 将牌存入数组中去
                poker[index++] = colors[j] + numbers[i];
            }
        }

        // 4. 将大小王存入数组中去。
        poker[index++] = "♕";
        poker[index] = "♔";

        // 5. 打印牌
        System.out.println("新牌: ");
        for (int i = 0; i < poker.length; i++)
        {
            System.out.print(poker[i] + " ");
        }
        System.out.println(); // 换行

        // 6. 洗牌: 把54张牌的数组poker中 牌顺序打乱
        // poker = [♦3 ♠3 ♥3 ♣3 ♦4 ♠4 ♥4 ♣4 ♦5 ♠5 ♥5 ♣5 ♦6 ♠6 ...]
        //           0  1  2  3  4  5  6   7  8  9  10 11 12 13
        for (int i = 0; i < poker.length; i++)
        {
            // 随机获取一个索引
            int index1 = (int) (Math.random() * poker.length);  // [0, 53]
            // 随机获取另一个索引
            int index2 = (int) (Math.random() * poker.length);  // [0, 53]

            // 每次一次都需要让index1和index2这两个索引位置处的数据进行交换
            // 1. 定义一个临时变量,存储index2索引位置的数据
            String temp = poker[index2];
            // 2. 将 index 索引位置的数据,赋值给Index2索引位置
            poker[index2] = poker[index1];
            // 3. 将临时变量,赋值给index1索引变量
            poker[index1] = temp;
        }

        System.out.println("洗牌后: ");
        for (int i = 0; i < poker.length; i++)
        {
            System.out.print(poker[i] + "\t");
        }
    }
}

第9章 二维数组

9.1 概述、定义

  1. 二维数组是啥?
  • 数组中的每个元素都是一个一维数组,这个数组就是二维数组。
  • 静态初始化
数据类型[][] 数组名 = new 数据类型[][] {元素1, 元素2...};
int[][] arr = new int[][]{ {12, 24, 36}, {666, 888}, {10, 20, 30}, {999} };
  • 动态初始化
数据类型[][] 数组名 = new 数据类型[长度1][长度2];
int[][] arr = new int[3][5];
  1. 二维数组的访问
数组名称[行索引]
数组名称[行索引][列索引]
package com.pdwei;

public class ArrayDemo5 {
    public static void main(String[] args)
    {
        // 目标: 二维数组的认识
        printArray();
        printArray2();
    }

    public static void printArray()
    {
        // 初始化二维数组存储学生姓名,考虑到作为不规则,直接定义每排的座位
        // 静态初始化数组
        String[][] classroom =
        {
            {"唐僧", "孙悟空", "猪八戒"},
            {"沙僧", "白龙马", "牛魔王"},
            {"红孩儿", "铁扇公主", "太上老君", "观音菩萨"},
            {"如来佛祖", "菩提祖师"}
        };

        // 访问: 数组名[行索引]
        String[] names = classroom[2];
        for (int i = 0; i < names.length; i++) {
            System.out.println(names[i]);
        }

        // 访问2: 数组名[行索引][列索引]
        System.out.println(classroom[1][1]);
        System.out.println(classroom[2][2]);

        // 长度访问: 数组名.length
        System.out.println(classroom.length);  // 4
        System.out.println(classroom[3].length);  // 2

        // 动态初始化数组
        int[][] arr = new int[3][5];  // 3行5列

    }

    // 遍历二维数组
    public static void printArray2()
    {
        String[][] classroom =
            {
                {"唐僧", "孙悟空", "猪八戒"},
                {"沙僧", "白龙马", "牛魔王"},
                {"红孩儿", "铁扇公主", "太上老君", "观音菩萨"},
                {"如来佛祖", "菩提祖师"}
            };
        for (int i = 0; i < classroom.length; i++) {
            // i = 0 1 2 3
            String[] names = classroom[i];
            for (int j = 0; j < names.length; j++) {
                System.out.print(names[j] + "\t");
            }
            System.out.println();
        }

        for (int i = 0; i < classroom.length; i++) {
            for (int j = 0; j < classroom[i].length; j++) {
                System.out.print(classroom[i][j] + "\t");
            }
            System.out.println();
        }
    }
}

9.2 综合案例: 石头迷阵游戏

package com.pdwei;

public class ArrayTest6 {
    public static void main(String[] args) {
        // 目标: 完成数字华容道的初始化和随机乱序。
        start(5);
    }

    public static void  start(int n)
    {
        // 1. 定义一个二维数组存储数字列表
        int[][] arr = new int[n][n];

        // 2. 遍历二维数组,给二维数组赋值
        int count = 1;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                arr[i][j] = count ++;
            }
        }

        printArray(arr);

        // 3. 打乱二维数组中的元素顺序
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                // 遍历到的当前位置: arr[i][j]
                // 随机的索引位置处: m(随机的行) p(随机的列)
                int m = (int) (Math.random() * arr.length);
                int p = (int) (Math.random() * arr.length);

                // 定义一个临时变量存储 m p 位置处的数据
                int temp = arr[m][p];
                // 把i j 位置处的数据赋值给m p
                arr[m][p] = arr[i][j];
                // 把 m p 位置处的数据temp记住的赋值给 i
                arr[i][j] = temp;
            }
        }

        System.out.println("====================");

        printArray(arr);
    }

    public static void printArray(int[][] arr)
    {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + "\t");
            }
            System.out.println();
        }
    }
}

D a y   4 C o m p l e t e d   !   !   ! \mathcal{Day~4 \quad Completed~!~!~!} Day 4Completed ! ! !

请添加图片描述

  首先他们忽略你,接着他们会嘲笑你,然后他们会打你,最后他们会输给你。

—— 甘地
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值