2021.6.22

Day4

数组的四个基本特点

  • 其长度是确定的。数组一旦被创建,它的大小就是不可以改变的。
  • 其元素必须是相同类型,不允许出现混合类型。
  • 数组中的元素可以是任何数据类型,包括基本类型和引用类型。
    数组变量属引用类型,数组也可以看成是对象,数组中的每个元素相当于该对象的成员变量。
  • 数组本身就是对象,Java中对象是在堆中的,因此数组无论保存原始类型还是其他对象类型,数组对象本身是在堆中的。

数组使用

for Each循环

package com.liu.array;

public class Demo04 {
    public static void main(String[] args) {
        int[] arrays = {1, 2, 3, 4, 5};
        //
        //for (int array : arrays) {
        //     System.out.println(array);

        //    }
        // }

       // printArray(arrays);
        int[] reverse = reverse(arrays);
        printArray(reverse);
    }
    //反转数组
    public  static int[] reverse(int[] arrays){
        int[] result = new int[arrays.length];
        for (int i = 0,j=result.length-1; i < arrays.length ; i++,j--) {//length -1 意味从堆的末置位开始排
            result[j]=arrays[i];


        }
        return result;//返回结果到定义的result中
    }
    //打印一个数组
    public static void printArray(int[] arrays) {
        for (int i = 0; i < arrays.length; i++) {
            System.out.print(arrays[i] +" ");
        }
    }

}

5 4 3 2 1
Process finished with exit code 0

二维数组

package com.liu.array;

public class Demo05 {
    public static void main(String[] args) {
        int a [][] = {{1,2},{2,3,4}};
      //  System.out.println(a[1][2]);

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

            }

        }
        printArray(a[1]);
    }





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

}

2 3 4
Process finished with exit code 0

Array类

  • 数组的工具类java.util.Arrays
  • 由于数组对象本身并没有什么方法可以供我们调用,但API中提供了一个工具类Arrays供我们使用,从而可以对数据对象进行一些基本的操作。
    查看JDK帮助文档
  • Arrays类中的方法都是static修饰的静态方法,在使用的时候可以直接使用类名进行调用,而"不用"使用对象来调用(注意:是"不用”而不是"不能")
  • 具有以下常用功能:
    给数组赋值:通过fil方法。
  • 对数组排序:通过sort方法,按升序。
  • 比较数组:通过equals方法比较数组中元素值是否相等。
  • 查找数组元素:通过binarySearch 方法能对排序好的数组进行二分查找法操作。
package com.liu.array;

import java.util.Arrays;

public class Demo06 {
    public static void main(String[] args) {
        int [] a  = {1,6,4,88846,86,318,648,78};
        System.out.println(a);//[I@1b6d3586
        //打印数组元素Arrays.toString
        System.out.println(Arrays.toString(a));
        Arrays.sort(a);//排序,降序
        System.out.println(Arrays.toString(a));



    }

}

[I@1b6d3586
[1, 6, 4, 88846, 86, 318, 648, 78]
[1, 4, 6, 78, 86, 318, 648, 88846]

Process finished with exit code 0

冒泡排序

package com.liu.array;

import java.util.Arrays;

public class Demo07 {
    public static void main(String[] args) {
        //冒泡排序
        int[] a = {1, 15, 8, 84, 89, 4589, 478, 48, 316};
        int[] sort = sort(a);
        System.out.println(Arrays.toString(sort));

    }

    public static int[] sort(int[] array) {
        int temp = 0;
        for (int i = 0; i < array.length-1; i++) {
               int temp = 0;
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j+1]>array[j]){
                    temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                }

            }
                        if (flag==false){
                break;//加上flag可以试时间更好利用
        }
        return array;

    }

}

[4589, 478, 316, 89, 84, 48, 15, 8, 1]

Process finished with exit code 0

1,比较数组中,两个相邻的元素,如果第一个数比第二个数大,我们就交换他们的位置

2。每一次比较,都会产生出一个最大,或者最小的数字;

3.下一轮则可以少一次排序!

4。依次循环,直到结束!

稀疏数组

  • 当一个数组中大部分元素为0,或者为同一值的数组时,可以使用稀疏数组来保存该数组。
  • 稀疏数组的处理方式是:
  • 记录数组一共有几行几列,有多少个不同值
    把具有不同值的元素和行列及值记录在一个小规模的数组中,从而缩小程序的规模
  • 如下图:左边是原始数组,右边是稀疏数组

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8HgFpGRg-1624374329370)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210620160055668.png)]

package com.liu.array;

public class Demo08 {
    public static void main(String[] args) {
        int [] [] a1  = new int[11][11];
       a1[1][2] = 1;
       a1[2][3] = 2;
        System.out.println("输出原始的数组");
        for (int[] ints : a1) {//a1.for
            for (int anInt : ints) {//ints.f
                System.out.print(anInt+"\t");
            }
            System.out.println();
        }

        System.out.println("====================");
        int sum = 0;
        for (int i = 0; i < a1.length; i++) {
            for (int j = 0; j < a1[i].length; j++) {
                if (a1[i][j]!=0){
                    sum++;
                }

            }

        }
        System.out.println("有效值个数:"+sum);
        int [][] a2 = new int[sum+1][3];
        a2[0][0]=11;
        a2[0][1]=11;
        a2[0][2]=sum;
        int count = 0;
        for (int i = 0; i <a1.length; i++) {
            for (int j = 0; j < a1[i].length; j++) {
                if (a1[i][j]!=0){
                    count++;
                     a2[count][0]= i;
                     a2[count][1]= j;
                     a2[count][2]= a1[i][j];

                }

            }

        }
        //输出稀疏数组
        for (int i = 0; i < a2.length; i++) {
            System.out.println(a2[i][0]+"\t"+a2[i][1]+"\t"+a2[i][2]);

            }



        //还原
        int[][] a3 = new int[a2[0][0]][a2[0][1]];
        for (int i = 1; i < a2.length; i++) {
            a3[a2[i][0]][a2[i][1]]=a2[i][2];

         }
        System.out.println("输出还原的数组");
        for (int[] ints : a3) {
            for (int anInt : ints) {
                System.out.print(anInt+"\t");

            }
            System.out.println();
        }


    }


    }

输出原始的数组
0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0
0 0 0 2 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0

有效值个数:2
11 11 2
1 2 1
2 3 2
输出还原的数组
0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0
0 0 0 2 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0

Process finished with exit code 0

面向对象

面向对象定义

  • 面向对象编程(Object-Oriented Programming, OOP)

  • 面向对象编程的本质就是:以类的方式组织代码,以对象的组织(封装)数据。

  • 抽象

  • 三大特性:

    • 封装
    • 继承
    • 多态
  • 从认识论角度考虑是先有对象后有类。对象,是具体的事物。类,是抽象的,是对对象的抽象从代码运行角度考虑是先有类后有对象。类是对象的模板

  • 方法的定义
  • 修饰符返回类型
  • break:跳出switch,结束循环和return的区别方法名:注意规范就OK见名知意
  • 参数列表:(参数类型,参数名)…
  • 异常抛出
  • 方法的调用
    • 静态方法非静态方法形参和实参
    • 值传递和引用传递
    • this关键字

构造器详解

package com.oop.Demo02;
//学生类
public class Student {
    //属性:字段
    String name;
    int age;
//方法
    public void study(){
    System.out.println(this.name+"在学习");
    }
/*
    Student xm = new Student();
        Student xh = new Student();
        xm.name = "小明";
        xh.name = "小红";



 */

//无参构造器

    public Student() {
    }
//有参构造器
    public Student(String name){
        this.name = name;
    }
}


/*
        //类:抽象的,实例化
        Student student = new Student();
        System.out.println(student.name);

    }
        //类:抽象的,实例化
        Student student = new Student("liuke");
        System.out.println(student.name);

    }
    构造器;
1.和类名相同2.没有返回值作用:
1. .new本质在调用构造方法
2.初始化对象的值
注意点;
1.定义有参构造之后,如果想使用无参构造,显示的定义一个无参的构造

 */

创建对象内存分析

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ee1v30cP-1624374329372)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210622150121530.png)]

1.类与对象
类是一个模板:抽象,对象是一个具体的实例2.方法
定义、调用!
3。对应的引用
引用类型:基本类型(8)
对象是通过引用来操作的:栈—>堆
4。属性:字段Field 成员变量
默认初始化:
数字:00.0char : uaeee
booLean:faLlsel引用: null
修饰符﹑属性类型属性名=属性值!
5.对象的创建和使用
-必须使用new关键字创造对象,构造器Person kuangshen = new Person( );-对象的属性kuangshen.name
-对象的方法kuangshen.sLeep()
6。类:
静态的属性属性

动态的行为

封装

  • 该露的露,该藏的藏

  • 我们程序设计要追求**“高内聚,低耦合”**。高内聚就是类的内部数据操作细节自己完成,不允许外部干涉;低耦合:仅暴露少量的方法给外部使用。
    封装(数据的隐藏)

  • 通常,应禁止直接访问一个对象中数据的实际表示,而应通过操作接口来访问,这称为信息隐藏。

  • 记住这句话就够了:属性私有,get/set

    package com.oop.Demo03;
    
    public class Student {
        private String name;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    
    /*
            Student xm = new Student();
            xm.setName("小明");
            System.out.println(xm.getName());
    
    
    
     */
    

1。提高程序的安全性,保护数据

2。隐藏代码的实现细节

3。统一接口

4。系统可维护增加了

继承

  • 继承的本质是对某一批类的抽象,从而实现对现实世界更好的建模。
  • extends的意思是“扩展”。子类是父类的扩展。
  • JAVA中类只有单继承,没有多继承!
  • 继承是类和类之间的一种关系。除此之外,类和类之间的关系还有依赖、组合、聚合等。
  • 继承关系的俩个类,一个为子类(派生类),一个为父类(基类)。子类继承父类,使用关键字extends来表示。
  • 子类和父类之间,从意义上讲应该具有"is a"的关系.
  • object类
  • super
  • 方法重写

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pg0V2tRV-1624374329373)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210622164809543.png)]

object类

在java中,所有的类,都默认直接或间接继承Object类

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9kdOZVDa-1624374329374)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210622170029181.png)]

super详解
super注意点:
1. super调用父类的构造方法,必须在构造方法的第一个
2. super必须只能出现在子类的方法或者构造方法中!
3. superthis 不能同时调用构造方法!


Vs this:
代表的对象不同:
this:本身调用者这个对象
super:代表父类对象的应用
前提

this:没哟继承也可以使用
super:只能在继承条件才可以使用
构造方法

this();本类的构造
super();父类的构造!
方法重写
  • 静态的方法和非静态的方法区别很大!

  • 静态方法:方法的调用只和左边,定义的数据类型有关

  • 非静态:重写

    子类

public class A extends B{
    public void test(){
        System.out.println("A=>test()");
    }


    }

父类

public class B {
    public void test(){
        System.out.println("B=>test()");
    }

}
public class Application {
    public static void main(String[] args){
        A a = new A();
        a.test();//A,son
        B b = new A();//子类重写了父类的方法
        b.test();


    }

A=>test()
A=>test()

Process finished with exit code 0

输出的都是是A=>test()

说明都是用的子类的方法体 这叫对父类的改变也叫重写

重写:需要有继承关系,子类重写父类的方法!
1.方法名必须相同
2.参数列表列表必须相同
3.修饰符:范围可以扩大但不能缩小: public>Protected>Default>private
4.抛出的异常:范围,可以被缩小,但不能扩大; ClassNotFoundException --> Exception()

重写,子类的方法和父类必要一致;方法体不同!
为什么需要重写:
1.父类的功能,子类不一定需要,或者不一定满足!
Alt + Insert ; override;

多态

  • 动态编译:类型:可扩展性

  • 即同一方法可以根据发送对象的不同而采用多种不同的行为方式。

  • 一个对象的实际类型是确定的,但可以指向对象的引用的类型有很多

  • 多态存在的条件

    • 有继承关系
    • 子类重写父类方法
    • 父类引用指向子类对象
  • 注意:多态是方法的多态,属性没有多态性。

  • instanceof

    public class Application {
        public static void main(String[] args){
            //一个对象的实际类型是确定的l
            // /new Student();
            //new Person( );
            //可以指向的引用类型就不确定了:父类的引用指向子类l / Student能调用的方法都是自己的或者继承父类的!
            Student s1 = new Student();
            //Person父类型,可以指向子类,但是不能调用子类独有的方法
            Person s2 = new Student();
            Object s3 = new Student();
            s1.run();
            s2.run();
            //对象能执行哪些方法,主要看对象左边的类型,和右边关系不大!
            ((Student)s2).eat();//子类重写了父类的方法,执行子类的方法
            s1.eat();
    
    
        }
    

子类

public class Student extends Person{
    @Override
    public void run() {
        super.run();
    }

    public void eat(){
        System.out.println("eat");
    }

}

父类

public class Person {
    public void run(){
        System.out.println("run");
    }
}

run
run
eat
eat

Process finished with exit code 0

多态注意事项:
1.多态是方法的多态,属性没有多态
2.父类和子类,有联系类型转换异常! cLassCastException !
3.存在条件:继承关系,方法需要重写,父类引用指向子类对象! Father f1 = new Son();

  1. static方法,属于类,它不属于实例
    2.final常量;
  2. private方法;
instanceof
public class Application {
    public static void main(String[] args){
        Teacher teacher = new Teacher();
        System.out.println(teacher instanceof Object);
        System.out.println(teacher instanceof Person);
        // System.out.println(teacher instanceof String);//报错
        //  System.out.println(teacher instanceof Student);//报错


    }

}
类型转换
public static void main(String[] args){
    Person person = new Student();//由低转高自动转换
   // Student student = new Person();//由高转低会报错
    Student student = (Student) person;//由高转低需要强转
    student.run();
    ((Student)person).run();//两句写成一句



}

1.父类引用指向子类的对象

2.把子类转换为父类,向上转型;

3.把父类转换为子类,向下转型;强制转换

4.方便方法的调用,减少重复的代码!简介

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值