Bigdata Development Java_Guidance_02

数组

/*
    数组,表示大量类型相同的数据。
    数组中每一个数据被称为元素。
    每个元素在数组中的位置被称为索引。

    []数组的标志。
    声明数组的格式:
    类型 [] 数组名 = new 类型[长度]  推荐写法
    类型,数组中元素的类型  int  double String 
    长度:数组的容,能存放多少个元素。
*/
    //声明一个能存放10个整数的数组,叫做scores。
    int scores [] = new int [10];
    //声明一个能够存放7个小数的数组叫做heights。
    double heights []  = new double[7];


    //对数组的存取都需要使用索引完成,索引是从0开始的,最大值是长度-1.

    //把元素存放到数组中的指定位置。

    scores[0] = 90;
    scores[6] = 70;

    //取出元素
        int score6 = scores[6];
        System.out.println(score9+","+score3);

        //数组的静态初始化,在数组生命的时候就加入元素。
        //数组的长度就是加入元素的个数,多个元素以,分隔。
        int number [] =new int {70,80,90,100};


        //数组长度一旦被设定,就不会发生变化,不会增加不也不会减少。
        //元素类型一定要和数组声明时的类型保持一致,不然无法添加进数组。


int[] scores = {36, 40, 37, 10, 90, 87, 96, 20, 23, 80};

        int s1 = scores[0];
        int s2 = scores[1];
        int s3 = scores[3];
        // ...
        int s11 = scores[8];

        // 根据数组的长度决定循环的次数
        for (int i = 0; i < scores.length; i++) {

            // int score = scores[0];
            // int score = scores[1];
            // int score = scores[2];
            // ...
            // int score = scores[10];
            int score = scores[i];

            System.out.print(score + " ");
        }

        // 遍历:获取数组中的每一个元素,通常使用 for 循环完成

        // for + alt + / 自动补全遍历最近的一个数组的代码
        for (int i = 0; i < scores.length; i++) {

            int j = scores[i];
        }

        System.out.println();

        // 增强 for 循环:for each 也是遍历数组的一种方式
        // for (类型 变量名 : 数组名) {}
        // 类型:需要遍历的数组中存放的元素的类型
        // 变量名:数组中的每一个元素,
        //          第一次循环表示第一个元素的值
        //          第二次循环表示第二个元素的值
        //          第 n 次循环表示第 n 个元素的值
        // 数组名:需要遍历的数组      
        for (int j : scores) {

            System.out.print(j + " ");
        }

        System.out.println();

        // 遍历数组使用上面两种方式都可以,都可以使用 for + alt + / 自动生成
        // for each 遍历没有 索引,如果需要使用索引,使用 普通 for 循环

        // 倒序遍历,从 长度 - 1 开始,到 0 结束
        for (int i = scores.length - 1; i >= 0 ; i--) {

            int score = scores[i];

            System.out.print(score + " ");
        }

        System.out.println();

冒泡排序

        int[] scores = {36, 40, 37, 10, 90, 87, 96, 20, 23, 80};

        // 求数组中最大的值
        // 定义变量 max 表示数组中的最大值,给个默认值,数组中的第一个元素
        int max = scores[0];

        int min = scores[0];

        int sum = scores[0]; // 所有数字相加的和

        // 拿着 max 和数组中的每一个元素进行比较,让 max = 这两个数组中较大的一个
        for (int i = 1; i < scores.length; i++) {
            // 从第二个元素开始进行比较,因为 max 和 第一个元素 相等

            int j = scores[i]; // 数组中的每一个元素

            // 让 max = max 和 j 中较大的一个
            max = max > j ? max : j;

            min = min < j ? min : j;

            sum += j;
        }

        System.out.println("最大值是:" + max);
        System.out.println("最小值是:" + min);
        System.out.println("总和是:" + sum);


        //冒泡排序为相邻的两个元素进行比较,左边>右边则交换位置。

                for (int j = 0; j < scores.length - 1; j++) {

            // 1. 比较相邻的两个元素,左边 > 右边,交换位置
            for (int i = 0; i < scores.length - 1; i++) {

                // 相邻的两个元素
                int s1 = scores[i];
                int s2 = scores[i + 1];

                if (s1 > s2) {

                    // 两者需要交换位置
                    scores[i] = s2;
                    scores[i + 1] = s1;
                }
            }
        }

Array在数组中常用到的

//Array类提供了一些操纵数组的方法。

        int scores[] = { 10, 50, 90, 80, 70, 60, 40, 30, 20 };

        Array.sort(scores);
        //对数组自动排序,自动选择最恰当的方式进行处理。

        int index = Array.binarySearch(scores,40);
        //查询数组中某个元素的索引。
        //1、数组必须是排序过的。
        //2、如果数字中没有这个索引,则返回负数,如果不包含指定元素,则—插入位置-1.
        //插入位置为把key添加到数组后的索引。


        //把数组转换成字符串。
        //toString方法把一个数组转换为字符串[元素1,元素2,元素3,。。。]
        //如果只是向输出数组,可以用用这个方法,不用进行遍历
        String text = Array.toString(scores);
        systeom.out.print(text);        


        ```

二维数组


        //二维数组,拥有3行4列。
        int array [][] = new int [3][4];

            array[0][0] = 1;
        array[0][2] = 3;
        array[0][3] = 4;
        array[2][3] = 4;

        // 多维数组实际是 一维数组 的嵌套使用
        // 把长度为 4 的数组作为元素添加到长度为 3 的数组中
        for (int[] is : array) {

            for (int i : is) {

                System.out.print(i + " ");
            }

            System.out.println();
        }

类和方法

// 使用代码描述事物
// 修饰词 class 类名 { 所有和类相关的代码都要写在这里 }
public class Mouse {

    // 属性:有什么
    //      修饰词 变量类型 变量名;
    public String name; // 有名字

    public double price; // 有价格

    public boolean type; // 有类型 (true == 有线鼠标,false == 无线鼠标)

    // 方法:做什么

    // 把光标移动到点 (x, y)
    public void move(int x, int y) {

        System.out.println("光标移动到:( " + x + ", " + y + ")");
    }

    // 点击 (type = 1 = 左键,type = 2 = 右键)
    public void click(int type) {

        if (type == 1) {

            System.out.println("点击了左键");
        }else if (type == 2) {

            System.out.println("点击了右键");
        }
    }

/*类==零件。
许多相同和不同的零件组装成一个完成的产品。
许多相同不同的类组装成一个软件。

不同的零件有不同的外观,也有不同的功能。
就像鼠标和键盘,都是电脑的组成部分,但是长得不一样,功能也不一样。

类: 属性和方法。
类的属性=零件的外观
属性:有什么

类的方法=零件的功能
方法,做什么

类==图纸,对象==实物,
图纸不能直接使用,必须勇士根据图纸制造的产品。
类不能直接使用,必须使用类创建的对象。

对象:声明要使用的类 的类型的变量。

类名,变量名=new 类名();

*/


        Mouse mouse = new Mouse();

        // 通过 mouse 变量使用类的属性和方法,全部通过 . 调用

        mouse.name = "雷蛇"; // 为属性赋值
        mouse.price = 499.00;
        mouse.type = true;

        // 获取属性的值
        System.out.println("鼠标名字:" + mouse.name);
        System.out.println("鼠标价格:" + mouse.price);
        System.out.println("鼠标类型:" + (mouse.type ? "有线鼠标" : "无线鼠标"));

        // 调用方法:变量名.方法名(参数1, 参数2,...);
        mouse.move(100, 100);
        mouse.move(300, 50);

        mouse.click(1);
        mouse.click(2);


        //调用属性没有(),调用方法一定要有()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值