JavaSE学习

JavaSE

Java基础

快捷指令(psvm、sout)

public class hello {
    psvm
}
变成
public class hello {
    public static void main(String[] args) {
        
    }
}

-----
public class hello {
    public static void main(String[] args) {
        sout
    }
}
变成
public class hello {
    public static void main(String[] args) {
        System.out.println();
    }
}

注释

单行注释 //
多行注释 /* */
文档注释 /** */

数据类型

//整数
int num1 = 10; //最常用,只要别超过21亿(2^31-1)
byte num2 = 20; //-128~127
short num3 = 30;
long num4 = 30L; //long类型数字后面要加个L(尽量用大写,小写l容易与1搞混)

//小数:浮点数
float num5 = 50.1F; //float类型数字后面要加个F
double num6 = 3.141592653589793238;

//字符
char name = '国';
//字符串, String不是关键字,是类
//String namea = "薛之谦";

//布尔值:是非
boolean flag = true

数据类型扩展

public class demo2 {
    public static void main(String[] args) {
        //整数扩展: 进制、二进制0b、十进制、八进制0、十六进制0x

        int i = 10;
        int i2 = 010; //八进制0
        int i3 = 0x10; //十六进制0x

        System.out.println(i);
        System.out.println(i2);
        System.out.println(i3);
        System.out.println("================================");
        //==============================================
        //浮点数
        //float 有限离散 含入误差 大约 接近但不等于
        float f = 0.1f; //0.1
        double d = 1.0/10;//0.1

        System.out.println(f==d); //false
        System.out.println(f);
        System.out.println(d);

        float d1 = 2313131323123123f;
        float d2 = d1 + 1;
        System.out.println(d1==d2);//true

        //字符扩展
        char c1 = 'a';
        char c2 = '中';
        System.out.println(c1);
        System.out.println((int)c1);
        System.out.println(c2);
        System.out.println((int)c2);
        //所有的字符本质还是数字
        // U0000 UFFFF
        char c3 = '\u0061';
        System.out.println(c3);

        //转义字符
        // \t 制表符
        System.out.println("Hello\tWorld");
        System.out.println();

        // \n 换行
        System.out.println("hello\nWorld");

        System.out.println("-----------------------");
        String sa = new String("hello");
        String sb = new String("hello");
        System.out.println(sa==sb);

        String sc = "hello";
        String sd = "hello";
        System.out.println(sc==sd);
    }
}
		

类型转换

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

public class demo3 {
    public static void main(String[] args) {
        int i = 128;
        //强制转换   (类型)变量名   高到低
        byte b = (byte)i;//内存溢出

        //自动转换   低到高
        double c = i;
        System.out.println(i);
        System.out.println(b);
        System.out.println(c);

        /*
        注意点:
        1.不能对布尔值进行转换
        2.不能把对象类型转换为不相干的类型
        3.在把高容量转换到低容量的时候,强制转换
        4.转换的时候可能存在内存溢出,或者精度问题
         */
        System.out.println("============");
        System.out.println((int)23.7);
        System.out.println((int)-45.89f);

        System.out.println("==========");
        char e = 'a';
        int f = e+1;
        System.out.println(f);
        System.out.println((char)f);
    }
}

public class demo4 {
    public static void main(String[] args) {
        //数字之间可以用下划线分割
        int money = 10_0000_0000;
        int years = 20;
        int total = money*years;//-1474836480,溢出
        long total2 = money*years;//默认是int,

        long total3 = money*((long)years);//先把一个数转换为LOng
        System.out.println(total3);
    }
}

变量、常量、作用域

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

public class Demo5 {
    public static void main(String[] args) {
        //int a=1,b=2,c=3;
        String name ="cq";
        char x = 'X';
        double pi = 3.14;
    }
}

public class Demo6 {

    //类变量 static
    static double salary = 2500;

    //属性:变量

    //实例变量:从属于对象,如果不自行初始化,这个类型的默认值
    //布尔值默认是false
    //除了基本类型,其余的默认值都是null
    String name;
    int age;

    //main方法
    public static void main(String[] args) {
        //局部变量:必须声明和初始化值
        int i = 10;
        System.out.println(i);

        //变量类型 变量名字 = new Demo06
        Demo6 demo6 = new Demo6();
        System.out.println(demo6.age);
        System.out.println(demo6.name);

        //类变量 static
        System.out.println(salary);
    }

    //其他方法
    public void add(){

    }
}

public class Demo7 {
    //final代表常量
    //修饰符:不存在先后顺序
    static final double pi =3.14;

    public static void main(String[] args) {
        System.out.println(pi);
    }
}

基本运算符

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package operator;

public class Demo1 {
    public static void main(String[] args) {
        //二元运算符
        int a = 10;
        int b = 20;
        int c = 25;
        int d = 25;

        System.out.println(a+b);
        System.out.println(a-b);
        System.out.println(a*b);
        System.out.println(a/b);
        System.out.println(a/(double)b);
    }
}

package operator;

public class Demo2 {
    public static void main(String[] args) {
        long a = 123123123123L;
        int b = 123;
        short c = 10;
        byte d = 8;

        System.out.println(a+b+c+d);//Long
        System.out.println(b+c+d);//int
        System.out.println(c+d);//int

    }
}

package operator;

public class Demo3 {
    public static void main(String[] args) {
        //关系运算符返回的结果:正确,错误,布尔值
        int a = 10;
        int b = 20;
        int c = 21;
        System.out.println(c%a);
        System.out.println(a>b);
        System.out.println(a<b);
        System.out.println(a==b);
        System.out.println(a!=b);
    }
}

自增自减运算符

package operator;

public class Demo4 {
    public static void main(String[] args) {
        //一元运算符
        //++ 自增
        //-- 自减
        int a = 3;
        int b = a++;
        int c = ++a;

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);


        //幂运算 2^3   2*2*2 = 8
        double pow = Math.pow(3, 2);
        System.out.println(pow);
    }
}

逻辑运算符、位运算符

package operator;
//逻辑运算符
public class Demo5 {
    public static void main(String[] args) {
        // 与(and) 或(or) 非(取反)
        boolean a = true;
        boolean b = false;

        System.out.println("a && b:"+(a&&b));
        System.out.println("a || b:"+(a||b));
        System.out.println("!(a && b):"+!(a&&b));

        //短路运算
        int c = 5;
        boolean d = (c<4)&&(c++<4);
        System.out.println(d);
        System.out.println(c);
    }
}

package operator;
//位运算
public class Demo6 {
    public static void main(String[] args) {
    /*
    A = 0011 1100
    B = 0000 1101

    A&B = 0000 1100
    A|B = 0011 1101
    A^B = 0011 0001
    ~B  = 1111 0010

    2 * 8 =16
    左移<<  *2
    右移>>  /2
    0000 0000  0
    0000 0001  1
    0000 0010  2
    0000 0100  4
    0000 1000  8

     */
    System.out.println(2<<3);
    }

}

三元运算符及小结

package operator;

public class Demo7 {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        a+=b;//a = a + b
        System.out.println(a);
        a-=b;//a = a - b
        System.out.println(a);

        //字符串连接符  +
        System.out.println(a+b);
        System.out.println(""+a+b);
        System.out.println(a+b+"");

    }
}

package operator;
//三元运算符
public class Demo8 {
    public static void main(String[] args) {
        // x ? y : z
        //如果x==ture,则结果为y,否则结果为z
        int score =80;
        String type = score < 60 ? "不及格": "及格";
        System.out.println(type);
    }
}

JavaDoc

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package operator;

/**
 * @author cq
 * @version 1.0
 * @since 1.8
 */
public class Doc {
    String name;

    /**
     * 
     * @param name
     * @return
     * @throws Exception
     */
    public String test(String name)throws Exception{
        return name;
    }
}

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

Java流程控制

用户交互Scanner

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package scanner;

import java.util.Scanner;

public class Demo1 {
    public static void main(String[] args) {
        //创建应该扫描器对象,用于接收键盘数据
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用next方式接收:");

        //判断用户有没有输入字符串
        if(scanner.hasNext()){
            //使用next方式接收
            String str = scanner.next();
            System.out.println("输出的内容为:"+str);
        }

        //凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉
        scanner.close();
    }
}


--------------------------
package scanner;

import java.util.Scanner;

public class Demo2 {
    public static void main(String[] args) {
        //从键盘接收数据
        Scanner scanner = new Scanner(System.in);

        System.out.println("使用nextLine方式接收:");

        //判断是否还有输入
        if(scanner.hasNextLine()){
            String str = scanner.nextLine();
            System.out.println("输出的内容为:"+str);
        }
        scanner.close();
    }
}

--------------------------
package scanner;

import java.util.Scanner;

public class Demo3 {
    public static void main(String[] args) {
        //从键盘接收数据
        Scanner scanner = new Scanner(System.in);

        System.out.println("使用nextLine方式接收:");

        String str = scanner.nextLine();
        System.out.println("输出的内容为:"+str);

        scanner.close();
    }
}

Scanner进阶

package scanner;

import java.util.Scanner;

public class Demo4 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        //从键盘接收数据
        int i = 0;
        float f = 0.0f;
        System.out.println("请输入整数:");

        if (scanner.hasNextInt()) {
            i = scanner.nextInt();
            System.out.println("整数数据:"+i);
        }else{
            System.out.println("输入的不是整数数据");
        }

        if (scanner.hasNextFloat()) {
            f = scanner.nextFloat();
            System.out.println("浮点数数据:"+ f);
        }else{
            System.out.println("输入的不是浮点数数据");
        }

        //关闭
        scanner.close();
    }
}


package scanner;

import java.util.Scanner;

public class Demo5 {
    public static void main(String[] args) {
        //输入多个数字,求平均数
        Scanner scanner = new Scanner(System.in);

        //和
        double sum = 0;
        //计算输入多少个数字
        int m = 0;
        //通过循环判断是否还有输入
        while (scanner.hasNextDouble()){
            double x = scanner.nextDouble();
            m = m + 1;
            sum = sum + x;
            System.out.println("你输入了第"+m+"个数据,sum="+sum);
        }

        System.out.println(m+"个数:"+sum);
        System.out.println(m+"个数平均值是"+(sum/m));

        scanner.close();
    }
}


顺序结构

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package struct;

public class Demo1 {
    public static void main(String[] args) {
        System.out.println("hello1");
        System.out.println("hello2");
        System.out.println("hello3");
        System.out.println("hello4");
        System.out.println("hello5");
    }
}

if选择结构

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package struct;

import java.util.Scanner;

public class Demo2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入内容:");
        String s = scanner.nextLine();

        //equals:判断字符串是否相等
        if (s.equals("hello")){
            System.out.println(s);
        }
        System.out.println("End");
        scanner.close();
    }
}

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package struct;

import java.util.Scanner;

public class Demo3 {
    public static void main(String[] args) {
        //考试分数大于60输出及格,小于60输出不及格
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入成绩:");
        int score = scanner.nextInt();

        if(score>=60){
            System.out.println("及格");
        }else {
            System.out.println("不及格");
        }

        scanner.close();
    }
}

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package struct;

import java.util.Scanner;

public class Demo4 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入成绩:");
        int score = scanner.nextInt();

        if (score == 100){
            System.out.println("满分");
        }else if (score<100 && score>=90){
            System.out.println("A");
        }else if (score<90 && score>=80){
            System.out.println("B");
        }else if (score<80 && score>=70){
            System.out.println("C");
        }else if (score<70 && score>=60){
            System.out.println("D");
        }else if (score<60 && score>=0){
            System.out.println("E");
        }else{
            System.out.println("输入不合法");
        }

        scanner.close();
    }
}

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

switch选择结构

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package struct;

public class Demo5 {
    public static void main(String[] args) {
        char grade = 'C';

        //case穿透
        switch (grade){
            case 'A':
                System.out.println("A");
                break;
            case 'B':
                System.out.println("B");
            case 'C':
                System.out.println("C");
            case 'D':
                System.out.println("D");
            case 'E':
                System.out.println("E");
            default:
                System.out.println("F");

        }
    }
}

package struct;

public class Demo6 {
    public static void main(String[] args) {
        String name = "张三";
        switch (name){
            case "李四":
                System.out.println("李四");
                break;
            case "张三":
                System.out.println("张三");
                break;
            default:
                System.out.println("null");
        }
    }
}

while循环结构

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package struct;

public class Demo7 {
    public static void main(String[] args) {
        int i = 0;
        while (i<=100){
            i++;
            System.out.println(i);
        }
    }
}
---------------------
package struct;

public class Demo8 {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;
        while (i<=100){
            sum = sum + i;
            i++;
        }
        System.out.println(sum);
    }
}


do…while循环

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package struct;

public class Demo9 {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;
        do{
            sum = sum + i;
            i++;
        }while (i<=100);
        System.out.println(sum);
    }
}

package struct;

public class Demo10 {
    public static void main(String[] args) {
        int i = 0;
        while (i<0){
            System.out.println(i);
            i++;
        }
        System.out.println("-----------------");
        do {
            System.out.println(i);
            i++;
        }while (i<0);

    }
}

for循环

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package struct;

public class Demo11 {
    public static void main(String[] args) {
        int i = 0;
        while (i<=100){
            System.out.println(i);
            i+=2;
        }

        System.out.println("-------------");
        for (int j=0; j<=100;j++){
            System.out.println(j);
        }
    }
}

package struct;

public class Demo12 {
    public static void main(String[] args) {
        int addSum = 0;
        int evenSum = 0;
        for (int i = 0; i < 100; i++) {
            if (i % 2 != 0){
                addSum +=i;
            }else {
                evenSum +=i;
            }
        }
        System.out.println(addSum);
        System.out.println(evenSum);
    }
}

package struct;

public class Demo13 {
    public static void main(String[] args) {
        //1-1000中5的倍数,每三个换行
        for (int i = 1; i <= 1000; i++) {
            if(i % 5 == 0){
                System.out.print(i+"\t");
            }
            if (i % (5*3) == 0){
                System.out.println();
            }
        }
    }
}

九九乘法表
package struct;

public class Demo14 {
    public static void main(String[] args) {
        for (int j = 1; j <= 9; j++) {
            for (int i = 1; i <= j; i++) {
                System.out.print(j+"*"+i+"="+j*i+"\t");
            }
            System.out.println();
        }

    }
}

增强for循环

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package struct;

public class Demo15 {
    public static void main(String[] args) {
        int[] numbers = {10,20,30,40,50};//定义一个数组
        for (int i=0;i<5;i++){
            System.out.println(numbers[i]);
        }
        System.out.println("================");
        //遍历数组元素
        for (int x:numbers){
            System.out.println(x);
        }
    }
}

break、continue、goto

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package struct;

public class Demo16 {
    public static void main(String[] args) {
        int i = 0;
        while(i<100){
            i++;
            System.out.println(i);
            if (i==30){
                break;
            }
        }
        System.out.println("===");
    }
}

package struct;

public class Demo17 {
    public static void main(String[] args) {
        int i = 0;
         while (i<100){
             i++;
             if (i%10 == 0){
                 System.out.println();
                 continue;
             }
             System.out.print(i+"\t");
         }
    }
}

不建议使用

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

打印三角形

package struct;

public class TestDemo {
    public static void main(String[] args) {
        //打印三角形
        for (int i=1; i<=5; i++){
            for (int j=5; j>=i; j--){
                System.out.print(" ");
            }
            for (int j=1; j<=i;j++){
                System.out.print("*");
            }
            for (int j=1; j<i;j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

Java方法

什么是方法

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package method;

public class Demo1 {
    public static void main(String[] args) {
        int sum = add(1,2);
        System.out.println(sum);
        test();
    }
    public static int add(int a,int b){
        return a+b;
    }
    public static void test(){
        //打印三角形
        for (int i=1; i<=5; i++){
            for (int j=5; j>=i; j--){
                System.out.print(" ");
            }
            for (int j=1; j<=i;j++){
                System.out.print("*");
            }
            for (int j=1; j<i;j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

方法的定义和调用

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package method;

public class Demo2 {
    public static void main(String[] args) {
        int max = max(10,20);
        System.out.println(max);
    }
    //比大小
    public static int max(int num1,int num2){
        int result = 0;
        if (num1 == num2){
            System.out.println("num1==num2");
            return 0;//终止方法
        }
        if (num1>num2){
            result = num1;
        }else{
            result = num2;
        }
        return result;
    }
}

方法的重载

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

命令行传递参数

package method;

public class Demo3 {
    public static void main(String[] args) {
        //args.Length 数组长度
        for (int i = 0; i < args.length; i++) {
            System.out.println("args["+i+"]: "+ args[i]);
            
        }
    }
}

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

可变参数

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package method;

public class Demo4 {
    public static void main(String[] args) {
        Demo4 demo4 = new Demo4();
        demo4.test(1,2,3,4);
    }
    public void test(int... i){
        System.out.println(i[0]);
        System.out.println(i[1]);
        System.out.println(i[2]);
        System.out.println(i[3]);
    }
}

递归

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package method;

public class Demo6 {
    public static void main(String[] args) {
        System.out.println(f(5));
    }
    public static int f(int n){
        if (n == 1){
            return 1;
        }else{
            return n*f(n-1);
        }
    }
}

Java数组

什么是数组

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package array;

public class Demo1 {
    public static void main(String[] args) {
        int[] nums;//1.声明一个数组
        nums = new int[10];//2.创建一个数组
        //3.给数组元素中赋值
        nums[0] = 1;
        nums[1] = 2;
        nums[2] = 3;
        nums[3] = 4;
        nums[4] = 5;
        nums[5] = 6;
        nums[6] = 7;
        nums[7] = 8;
        nums[8] = 9;
        nums[9] = 10;

        int sum = 0;
        for (int i = 0; i<nums.length;i++){
            sum = sum + nums[i];
        }
        System.out.println(sum);
    }
}

三种初始化及内存分析

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package array;

public class Demo2 {
    public static void main(String[] args) {
        //静态初始化:创建 + 赋值
        int[] a = {1,2,3,4,5,6,7,8};
        System.out.println(a[0]);
        
        //动态初始化:包含默认初始化
        int[] b = new int[10];
        b[0] = 10;
        b[2] = 10;
        System.out.println(b[0]);
        System.out.println(b[1]);
        System.out.println(b[2]);
        System.out.println(b[3]);
    }
}

下标越界及小结

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

数组的使用

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package array;

public class Demo3 {
    public static void main(String[] args) {
        int[] arrays = {1,2,3,4,5};

        //打印全部的数组元素
        for (int i = 0; i < arrays.length; i++) {
            System.out.println(arrays[i]);
        }
        System.out.println("=================");

        int sum = 0;
        //计算所有元素的和
        for (int i = 0; i < arrays.length; i++) {
            sum += arrays[i];
        }
        System.out.println("sum = "+sum);

        //查找最大元素
        int max = arrays[0];
        for (int i = 0; i < arrays.length; i++) {
            if (arrays[i] > max){
                max = arrays[i];
            }
        }
        System.out.println("max = "+max);
    }
}
package array;

public class Demo4 {
    public static void main(String[] args) {
        int[] arrays = {1,2,3,4,5};

        for (int array : arrays){
            System.out.println(array);
        }
    }
}
package array;

public class Demo4 {
    public static void main(String[] args) {
        int[] arrays = {1,2,3,4,5};

        for (int array : arrays){
            System.out.println(array);
        }
        System.out.println("===========");
        printArray(arrays);
        System.out.println();
        System.out.println("===========");
        int[] reverse = reverse(arrays);
        printArray(reverse);
    }
    //打印数组元素
    public static void printArray(int[] arrays){
        for (int i = 0; i < arrays.length; i++) {
            System.out.print(arrays[i]+" ");
        }
    }
    //反转数组
    public static int[] reverse(int[] arrays){
        int[] result = new int[arrays.length];
        //反转的操作
        for (int i = 0,j = result.length-1; i < result.length; i++,j--) {
            result[j] = arrays[i];
        }
        return result;
    }
}

二维数组

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package array;

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

Arrays类

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package array;

import java.util.Arrays;

public class Demo6 {
    public static void main(String[] args) {
        int[] a = {1,2,3,4,88,900,534,132,53,5};
        System.out.println(a);// [I@1b6d3586

        //打印数组元素Arrays.toString
        System.out.println(Arrays.toString(a));
        System.out.println("==================");
        printArrays(a);
        System.out.println();
        System.out.println("==================");
        Arrays.sort(a);//数组进行排序:升序
        System.out.println(Arrays.toString(a));
        System.out.println("==================");
        Arrays.fill(a,2,4,0);//数组下标第二和第三元素填充
        System.out.println(Arrays.toString(a));
        Arrays.fill(a,0);//数组所有元素填充
        System.out.println(Arrays.toString(a));

    }
    //仿Arrays
    public static void printArrays(int[] a){
        for (int i = 0; i < a.length; i++) {
            if (i == 0){
                System.out.print("[");
            }
            if (i == a.length - 1){
                System.out.print(a[i]+"]");
            }else {
                System.out.print(a[i]+", ");
            }
        }
    }
}

冒泡排序

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package array;

import java.util.Arrays;

public class Demo7 {
    public static void main(String[] args) {
        int[] a = {1,4,5,6,7,2,22,55,77,66,99};
        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++) {
            //内层循环,比价判断两个数,如果第一个数,比第二个大,则交换位置
            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;
                }
            }
        }
        return array;
    }
}

稀疏数组

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package array;

public class Demo8 {
    public static void main(String[] args) {
        //1.创建一个二维数组 11*11  0:没有棋子,1:黑棋,2:白棋
        int[][] array1 = new int[11][11];
        array1[1][2] = 1;
        array1[2][3] = 2;
        //输出原始的数组
        System.out.println("输出原始的数组");
        for (int[] ints : array1) {
            for (int anInt : ints) {
                System.out.print(anInt + "\t");
            }
            System.out.println();
        }

        //转换为稀疏数组保存
        //获取有效值的个数
        int sum = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (array1[i][j] != 0){
                    sum++;
                }
            }
        }
        System.out.println("有效值的个数:"+sum);

        //2.创建一个稀疏数组的数组
        int[][] array2 = new int[sum+1][3];
        array2[0][0] = 11;
        array2[0][1] = 11;
        array2[0][2] = sum;

        //遍历二维数组,将非零的值,存放到稀疏数组中
        int count = 0;
        for (int i = 0; i < array1.length; i++) {
            for (int j = 0; j < array1[i].length; j++) {
                if (array1[i][j] != 0){
                    count++;
                    array2[count][0] = i;
                    array2[count][1] = j;
                    array2[count][2] = array1[i][j];
                }
            }
        }

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

        System.out.println("================");
        System.out.println("还原");
        //1.读取稀疏数组
        int[][] array3 = new int[array2[0][0]][array2[0][1]];
        
        //2.给其中的元素还原它的值
        for (int i = 1; i < array2.length; i++) {
            array3[array2[i][0]][array2[i][1]] = array2[i][2];
        }

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

面向对象

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

什么是面向对象

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

方法的定义

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package oop;

public class Demo1 {
    //main方法
    public static void main(String[] args) {

    }
    /*
    修饰符   返回值类型   方法名(...){
        //方法体
        return 返回值;
     }
     */
    public String sayHello(){
        return "hello world";
    }
    public int max(int a, int b){
        return a>b?a:b;//三元运算符
    }
}

方法的调用

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package oop;
//学生类
public class Student {
    //非静态方法
    public void say(){
        System.out.println("学生说话了");
    }
}
-------------------------
package oop;

public class Demo2 {
    public static void main(String[] args) {
        //实例化这个类new
        //对象类型  对象名 = 对象值;
        Student student = new Student();
        student.say();
    }
}
package oop;

public class Demo2 {
    public static void main(String[] args) {
        //实例化这个类new
        //对象类型  对象名 = 对象值;
        Student student = new Student();
        student.say();
    }
    //和类一起加载的
    public static void a(){
        //b();
    }
    //类实例化之后才存在
    public void b(){
        
    }
}

package oop;

public class Demo2 {
    public static void main(String[] args) {
        //实例化这个类new
        //对象类型  对象名 = 对象值;
        Student student = new Student();
        student.say();
        
        Student.say2();
    }
}


package oop;
//学生类
public class Student {
    //非静态方法
    public void say(){
        System.out.println("学生说话了");
    }

    //动态方法
    public static void say2(){
        System.out.println("学生说话了");
    }
}

形式参数和实际参数
package oop;

public class Demo3 {
    public static void main(String[] args) {
        //实际参数和形式参数的类型要对应
        int add = Demo3.add(1,2);
        System.out.println(add);
    }
    public static int add(int a,int b){
        return a+b;
    }
}
package oop;
//值传递
public class Demo4 {
    public static void main(String[] args) {
        int a = 1;
        System.out.println(a);//1
        Demo4.change(a);
        System.out.println(a);//1
    }
    //返回值为空
    public static void change(int a){
        a = 10;
    }
}
package oop;

//引用传递:对象,本质还是值传递
public class Demo5 {
    public static void main(String[] args) {
        Person person = new Person();
        System.out.println(person.name);//null
        Demo5.change(person);
        System.out.println(person.name);//wcq

    }
    public static void change(Person person){
        //person是一个对象:指向的--》Person person = new Person();这是一个具体的人,可以改变属性!
        person.name="wcq";
    }
}
//定义一个Person类,有一个属性:name
class Person{
    String name;//null
}

类与对象的关系

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package oop.demo2;
//学生类
public class Student {
    //属性:字段
    String name;//null
    int age;//0

    //方法
    public void study(){
        System.out.println(this.name+"在学习");
    }
}
package oop.demo2;

public class Application {
    public static void main(String[] args) {
        //类:抽象的,实例化
        //类实例化后返回一个自己的对象
        //xiaoming对象就是一个Student类的具体实例

        Student xiaoming = new Student();
        Student xiaohong = new Student();

        xiaoming.name = "小明";
        xiaoming.age = 3;

        System.out.println(xiaoming.name);
        System.out.println(xiaoming.age);

        xiaohong.name = "小红";
        xiaohong.age = 10;

        System.out.println(xiaohong.name);
        System.out.println(xiaohong.age);
    }
}

构造器

package oop.demo3;

public class Person {

    //一个类即使什么都不写,它也会存在一个方法
    //显示的定义构造器

    String name;

    //实例化初始值
    //1.使用new关键字,本质是在调用构造器
    public Person(){
        this.name = "wcq";
    }
}

---------------------------

package oop.demo3;

public class Application {
    public static void main(String[] args) {
        //new 实例化一个对象
        Person person = new Person();

        System.out.println(person.name);//wcq
    }
}

package oop.demo3;

public class Person2 {
    //一个类即使什么都不写,它也会存在一个方法
    //显示的定义构造器

    String name;
    //1.使用new关键字,本质是在调用构造器
    //2.用来初始化值


    //有参构造:一旦有参构造,无参就必定显示定义
//    public Person2(String name){
//        this.name = name;
//    }

    //alt + insert 自动生成构造

    public Person2(String name) {
        this.name = name;
    }
}

----
package oop.demo3;

public class Application2 {
    public static void main(String[] args) {
        Person2 person2 = new Person2("zhangsan");
        System.out.println(person2.name);
    }
}

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

创建对象内存分析

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

小结

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

封装

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package oop.demo4;
/*
封装的好处:
    1. 提高程序的安全性,保护数据
    2. 隐藏代码的实现细节
    3. 统一接口
    4. 系统可维护性增加了
 */
public class Application {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.setName("张三");
        System.out.println(s1.getName());

        s1.setAge(60);
        System.out.println(s1.getAge());//60

        Student s2 = new Student();
        s2.setAge(600);//不合法
        System.out.println(s2.getAge());//3

    }
}
package oop.demo4;
//类  private 私有
public class Student {

    //属性私有
    private String name;//名字
    private int id;//学号
    private char sex;//性别
    private int age;//年龄
    //提供一些可以操作这个属性的方法!
    //提供一些public的get、set方法

    //get 获得这个数据
    public String getName(){
        return this.name;
    }

    //set 给这个数据设置值
    public void setName(String name){
        this.name = name;
    }

    // alt + insert

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age >120 || age<0){//不合法判断
            this.age = 3;
        }else{
            this.age = age;
        }

    }
}

继承

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package oop.demo5;
//Person 人:父类
public class Person {
    private int money = 10_0000_0000;

    public void say(){
        System.out.println("说了一句话");
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }
}

-------

package oop.demo5;
// 学生 is 人:派生类或子类
// 子类继承了父类,就会拥有父类的全部方法
public class Student extends Person {
    //Ctrl + H
}

-------

package oop.demo5;
//教师 is 人:派生类或子类
public class teacher extends Person{
}

-------
package oop.demo5;

public class Application {
    public static void main(String[] args) {
        Student student = new Student();
        student.say();
        System.out.println(student.getMoney());
    }
}


Super

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package oop.demo5;
//在java中,所有的类,都默认直接或者间接继承Object
//Person 人:父类
public class Person {
    protected String name = "lisi";

}

------------

package oop.demo5;
// 学生 is 人:派生类或子类
//子类继承了父类,就会拥有父类的全部方法
public class Student extends Person {
    private String name = "zhangsan";
    public void test(String name){
        System.out.println(name);//wangwu
        System.out.println(this.name);//zhangsan
        System.out.println(super.name);//lisi
    }
}

----------------

package oop.demo5;

public class Application {
    public static void main(String[] args) {
        Student student = new Student();
        student.test("wangwu");
    }
}


方法重写

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package oop.demo5;
//重写都是方法的重写,和属性无关
public class B {
    public static void test(){
        System.out.println("B=>test()");
    }
}

--
package oop.demo5;
//继承
public class A extends B{
    public static void test(){
        System.out.println("A=>test()");
    }
}

--
package oop.demo5;

public class Application2  {
    public static void main(String[] args) {

        //方法的调用只和左边,定义的数据类型有关
        A a = new A();
        a.test();//A

        //父类的引用指向了子类
        B b = new A();
        b.test();//B
    }
}


package oop.demo5;
//重写都是方法的重写,和属性无关
public class B {
    public void test(){
        System.out.println("B=>test()");
    }
}


package oop.demo5;
//继承
public class A extends B{
    //Override 重写
    @Override   //注释:有功能的注释!
    public void test() {
        System.out.println("A=>test()");
    }
}


package oop.demo5;

public class Application2  {
    public static void main(String[] args) {
        //静态的方法和非静态的方法区别很大!
        //静态方法:方法的调用只和左边,定义的数据类型有关
        //非静态:重写
        A a = new A();
        a.test();//A

        //父类的引用指向了子类
        B b = new A();
        b.test();//A
    }
}

多态

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package oop.demo6;

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

package oop.demo6;

public class Student extends Person {
    @Override
    public void run() {
        System.out.println("son");
    }
    public void eat(){
        System.out.println("eat");
    }
}


package oop.demo6;

public class Application {
    public static void main(String[] args) {
        //一个对象的实际类型是确定的
//        new Student();
//        new Person();
        //可指向的引用类型就不确定了:父类的引用指向子类
        //Student 能调用的方法都是自己的或者继承父类的
        Student s1 = new Student();
        //Person 弗类型,可以指向子类,当时不能调用子类独有的方法
        Person s2 = new Student();
        Object s3 = new Student();

        //对象能执行哪些方法,主要看对象左边的类型,和右边关系不大
        s2.run();//子类重写了父类的方法,执行子类的方法
//      s2.eat();
        ((Student) s2).eat();
        s1.run();
        s1.eat();
    }
}

instanceof和类型转换

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package oop.demo7;

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

package oop.demo7;

public class Student extends Person {

}

package oop.demo7;

public class Teacher extends Person {
}


package oop.demo7;

import junit.framework.TestCase;

public class Application {
    public static void main(String[] args) {
        //Object > String
        //Object > Person > Teacher
        //Object > Person > Student
        Object object = new Student();
        System.out.println(object instanceof Student);//true
        System.out.println(object instanceof Person);//true
        System.out.println(object instanceof Object);//true
        System.out.println(object instanceof Teacher);//false
        System.out.println(object instanceof String);//false
        System.out.println("==================");
        Person person = new Student();
        System.out.println(person instanceof Student);//true
        System.out.println(person instanceof Person);//true
        System.out.println(person instanceof Object);//true
        System.out.println(person instanceof Teacher);//false
        //System.out.println(person instanceof String);//编译报错
        System.out.println("==================");
        Student student = new Student();
        System.out.println(student instanceof Student);//true
        System.out.println(student instanceof Person);//true
        System.out.println(student instanceof Object);//true
        //System.out.println(student instanceof Teacher);//编译报错
        //System.out.println(student instanceof String);//编译报错
    }
}


外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

static关键字

package oop.demo8;

//static
public class Student {
    private static int age;//静态的变量
    private double score;//非静态的变量

    public void run(){

    }
    public static void go(){

    }
    public static void main(String[] args) {
        Student s1 = new Student();
        System.out.println(Student.age);
        System.out.println(s1.age);
        System.out.println(s1.score);

        go();
        new Student().run();
        s1.go();
        s1.run();
    }
}
package oop.demo8;

public class Person {
    //2:赋初始值
    {
        System.out.println("匿名代码块");
    }
    //1: 只指向一次
    static {
        System.out.println("静态代码块");
    }
    //3
    public Person(){
        System.out.println("构造方法");
    }

    public static void main(String[] args) {
        Person person = new Person();
        System.out.println("=========");
        Person person2 = new Person();

    }
}
package oop.demo8;

//静态导入包
import static java.lang.Math.random;
import static java.lang.Math.PI;
public class Test {
    public static void main(String[] args) {
        System.out.println(random());
        System.out.println(PI);
    }
}

抽象类

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package oop.demo9;

//abstract 抽象类:类 extends:单继承 (接口可以多继承)
public abstract class Action {
    //约束
    //abstract : 抽象方法:只有方法名字,没有方法的实现
    public abstract void dosomething();

    //1.不能new这个抽象类,只能靠子类去实现它:约束
    //2.抽象类中可以写普通的方法
    //3.抽象方法必须在抽象类中
    //抽象的抽象:约束
}
package oop.demo9;

//抽象类的所有方法,继承了它的子类,都必须要实现它的方法
public class A extends Action {
    @Override
    public void dosomething() {

    }
}

接口

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package oop.demo10;

//interface 定义的关键字,接口都需要有实现类
public interface UserService {
    //常量public static final
    int AGE = 99;
    
    //接口中的所有定义其实都是抽象的 public abstract
    void add(String name);
    void delete(String name);
    void update(String name);
    void query(String name);
}

package oop.demo10;

public interface TimeService {
    void timer();
}
package oop.demo10;

//抽象类:extends
//类可以实现接口 implements接口
//实现了接口的类,就需要重写接口中的方法
//多继承 利用接口实现多继承
public class UserServiceImpl implements UserService,TimeService {
    @Override
    public void add(String name) {

    }

    @Override
    public void delete(String name) {

    }

    @Override
    public void update(String name) {

    }

    @Override
    public void query(String name) {

    }

    @Override
    public void timer() {

    }
}

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

N种内部类

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

package oop.demo11;

public class Outer {
    private int id = 10;
    public void out(){
        System.out.println("这是外部类的方法");
    }
    public class Inner{
        public void in(){
            System.out.println("这是内部类的方法");
        }
        //获得外部类的私有属性
        public void getID(){
            System.out.println(id);
        }
    }
}


package oop.demo11;

public class Application {
    public static void main(String[] args) {
        Outer outer = new Outer();
        //通过这个外部类来实例化内部类
        Outer.Inner inner = outer.new Inner();
        inner.getID();
    }
}

异常

Error和Exception

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

捕获和抛出异常

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

自定义异常及经验小结

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

JavaSE总结

/1.不能new这个抽象类,只能靠子类去实现它:约束
//2.抽象类中可以写普通的方法
//3.抽象方法必须在抽象类中
//抽象的抽象:约束
}


```java
package oop.demo9;

//抽象类的所有方法,继承了它的子类,都必须要实现它的方法
public class A extends Action {
    @Override
    public void dosomething() {

    }
}

接口

[外链图片转存中…(img-Kexl0mIY-1701704327658)]

package oop.demo10;

//interface 定义的关键字,接口都需要有实现类
public interface UserService {
    //常量public static final
    int AGE = 99;
    
    //接口中的所有定义其实都是抽象的 public abstract
    void add(String name);
    void delete(String name);
    void update(String name);
    void query(String name);
}

package oop.demo10;

public interface TimeService {
    void timer();
}
package oop.demo10;

//抽象类:extends
//类可以实现接口 implements接口
//实现了接口的类,就需要重写接口中的方法
//多继承 利用接口实现多继承
public class UserServiceImpl implements UserService,TimeService {
    @Override
    public void add(String name) {

    }

    @Override
    public void delete(String name) {

    }

    @Override
    public void update(String name) {

    }

    @Override
    public void query(String name) {

    }

    @Override
    public void timer() {

    }
}

[外链图片转存中…(img-JbpUPioq-1701704327659)]

N种内部类

[外链图片转存中…(img-tPIwOBsC-1701704327660)]

package oop.demo11;

public class Outer {
    private int id = 10;
    public void out(){
        System.out.println("这是外部类的方法");
    }
    public class Inner{
        public void in(){
            System.out.println("这是内部类的方法");
        }
        //获得外部类的私有属性
        public void getID(){
            System.out.println(id);
        }
    }
}


package oop.demo11;

public class Application {
    public static void main(String[] args) {
        Outer outer = new Outer();
        //通过这个外部类来实例化内部类
        Outer.Inner inner = outer.new Inner();
        inner.getID();
    }
}

异常

Error和Exception

[外链图片转存中…(img-Tiw09Ttb-1701704327661)]

[外链图片转存中…(img-oaQEoSet-1701704327662)]

[外链图片转存中…(img-jmCJznAu-1701704327663)]

[外链图片转存中…(img-H4yeQhrQ-1701704327663)]

[外链图片转存中…(img-Of9pZldj-1701704327664)]

捕获和抛出异常

[外链图片转存中…(img-cgIKwIuS-1701704327665)]

[外链图片转存中…(img-jTsTkU6a-1701704327665)]

[外链图片转存中…(img-3GK9Zlhx-1701704327666)]

自定义异常及经验小结

[外链图片转存中…(img-jIWzRJQj-1701704327667)]

JavaSE总结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值