狂神说Java零基础学习视频通俗易懂,残缺版,随心记录的笔记。稀疏数组,二维数组,冒泡排序,IO流,HashMap,自定义异常,内部类,Scanner

1. md基础

自己搭建博客:

  • typecho 轻量,主题很多
  • wordpress

**粗体 **

斜体

粗体和斜体

删除线

摘抄别人的文档

分割线



[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ta9LhKVA-1660141152050)(C:\Users\tenkai\Desktop\1.png)]

点击跳转到百度

名字性别生日
张三1997

hello

12312321

中间写上想说的话

下划线

  • dsfds
  • aaaa

img

2. Java不同进制

0b 结尾2进制
0 八进制
0x 16进制
	    int i2 = 0b1;
        int i8 = 07;
        int i16 = 0xA;//10   。 F为15

        System.out.println(i16);
		
		// 1 和 0,组在一起
        int i2 = 0b10; //2
        int i8 = 010; //8
        int i16 = 0x10; //16

3. 浮点数的问题

        float f = 0.1f;
        double d = 1.0 / 10;
        System.out.println(f == d);//false

        float d1 = 231312312312321f;
        float d2 = d1 + 1;
        System.out.println(d1 == d2); //true 。浮点数是:有限 离散 舍入误差 大约 接近不等于

        //最好完全 避免浮点数进行比较。
        //适用:BigDecimal

4. char定义

        System.out.println((int) 'a');
        System.out.println((int) '中');
        //97 。大写的A为 65
        //20013
        //所有的字符,本质还是数字。Unicode编码,2字节,65536个(现在更多)
        //最早的Excel 有 2*16个,65536个表格

        // U0000 UFFFF
        char c3 = '\u0061';
        System.out.println(c3); //为a 。62为b

5. 转义符 和 String

 \t 制表符
 \n 换行
 
 回车符'\r'后面没有内容
 System.out.println("abcdef\r\r\r");		// abcdef
 
		System.out.println("abcde\r123");  	//	123 现在 idea前面的都不打印
		System.out.println("abcde\r\r123");		// 123
        System.out.println("1" == "1"); //true
        System.out.println(new String("1") == new String("1")); //false

6. Math类

        System.out.println(10 % 3);

        double pow = Math.pow(2, 3);
        System.out.println(pow); //8.0

7. 位运算符

& | ^ ~ >> << >>>

 A = 0011 1100
 B = 0000 1101
 
A&B= 0000 1100  并且:只有同时位1,结果才为1
A|B= 0011 1101  或者:只要一个为1,结果就为1
A^B= 0011 0001  亦或:相同为0,不同为1
~B = 1111 0010  取反
2* 2^3 = 162*2*2*2

0000 0000 0
0000 0001 1
0000 0010 2

0000 0011 3
0000 0100 4
0000 1000 8
0001 0000 16
    	//左移 X 位,乘以 2^x方。效率极高
        System.out.println(2<<3); // 为16
        System.out.println(16>>3); // 为2

8. Java生成文档

javadoc -encoding UTF-8 -charset UTF-8 Doc.java
/**
 * @author tenkai
 * @version 1.0
 * @since 1.8
 */
public class TestMain {}

javac

javac .\TestMain2.java //和文件同一个目录。
public class TestMain2 {
    public static void main(String[] args) {
        for (int i = 0; i < args.length; i++) {
            System.out.println("args" + i + "==" + args[i]);
        }
    }
}
java com.hai.tempmybatis.TestMain2 aa bb cc    //在 包路径外执行
args0==aa
args1==bb
args2==cc

9. Scanner

next()
nextLine()

hasNext()
hasNextLine()
        Scanner sc = new Scanner(System.in);

        System.out.println("使用next接收");

		//循环的时候 使用
        if (sc.hasNext()) {//hasNextLine()()
            String next = sc.next();//nextLine()
            System.out.println("输出的内容:" + next);
        }
        sc.close();

//输入: hello world ,next只会接收到 hello

next()

  • 一定要 读取到 有效字符后 才可以 结束输入。(一定要输入,否则程序不停止)
  • 只有输入 有效字符后,才将其 后面输入的 空白 作为分隔符 或者 结束符。
    • 空格后面的会当做 分割的结束符。以空格为结束符。
  • next 不能得到带有空格的字符串。

nextLine()

  • 以 回车 为 结束符。
  • 可以获得空白。

接收数值

        Scanner sc = new Scanner(System.in);

        System.out.println("使用next接收");

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

        if (sc.hasNextFloat()) {
            float next = sc.nextFloat();
            System.out.println("小数数据:" + next);
        } else {
            System.out.println("输入的不是小数");
        }

        sc.close();
//如果输入的 是1.1 小数,nextInt不会接收,nextFloat会接收。
//输入 abc,都不会接收,打印:输入的不是整数 小数
        double sum = 0;
        int m = 0;
        while (sc.hasNextDouble()) {
            double x = sc.nextDouble();
            m = m + 1;
            sum = sum + x;
        }
        System.out.println("总和:" + sum);
        System.out.println("个数" + m);

10. 分支和循环

switch

        //输出 良好 及格 。case 穿透
        char grade = 'B';
        switch (grade) {
            case 'A':
                System.out.println("优秀");
                break;
            case 'B':
                System.out.println("良好");
            case 'c':
                System.out.println("及格");
            default:
                System.out.println("未知等级");
        }
        //jdk 7开始,支持了 字符串
        String name = "老师";
        switch (name) {
            case "老师":
                System.out.println("老师");
                break;
            default:
                System.out.println("输入的不对");
        }
  • 反编译,手动放在 idea 的项目里面(不能直接复制,通过文件管理)
public class TestMain {
    public TestMain() {
    }

    public static void main(String[] args) {
        String name = "老师";
        byte var3 = -1;
        switch(name.hashCode()) {
        case 1039911:
            if (name.equals("老师")) {
                var3 = 0;
            }
        default:
            switch(var3) {
            case 0:
                System.out.println("老师");
                break;
            default:
                System.out.println("输入的不对");
            }

        }
    }
}

doWhile

  • 即使 不满足条件,也至少执行一次。
        int a = 0;
        do {
            System.out.println(a);
            a++;
        } while (a < 0);

goto 实现 质数

101 103 107 109 113 127 131 137 139 149
        //101 到 150的质数
		outer:
        for (int i = 101; i < 150; i++) {
            for (int j = 2; j < i / 2; j++) {
                if (i % j == 0) {
                    continue outer;
                }
            }
            System.out.print(i + " ");
        }
        for (int i = 101; i < 150; i++) {
            boolean f = true;
            for (int j = 2; j < i / 2; j++) {
                if (i % j == 0) {
                    f = false;
                    break;
                }
            }
            if (f) {
                System.out.print(i + " ");
            }
        }

//因为 1 一定能整除,所以也不判断。
//5 只需要判断: 1 2
//7 只需要判断: 1 2 3

11. 可变参数

  • 只能有一个,并且在 最后
  • 真是为:数组
        printMax(1, 2,3, 4);

    public static void printMax(double... num) {
        if (num.length == 0) {
            System.out.println("参数异常");
            return;
        }

        double res = num[0];

        for (int i = 0; i < num.length; i++) {
            if (num[i] > res) {
                res = num[i];
            }
        }
        System.out.println(res);
    }

12. 递归

递归头

  • 什么时候 不调用 自身方法,如果没有头,将陷入 死循环。
  • 递归体:什么时候需要调用自身方法。

阶乘:

    //2 2 * f(1)
    //3 3 * f(2),然后在调用上面
    //边界条件,前阶段(不停的调用自己),返回阶段。
    public static int f(int n) {
        if (n == 1) {
            return 1;
        } else {
            return n * f(n - 1);
        }
    }
  • 每次调用都是压栈,每执行完这个方法,不会释放。
    • 对于 嵌套层次深的,递归就 力不从心了。
    • 能不用递归,就不用递归。

13. 内存分析

  • 存放new对象 和 数组
  • 可以被 所有的线程共享,不会存放别的 对象引用。

  • 存放 基本 变量类型 (包含 这个基本类型的数值)
  • 引用 对象的变量(会存放 这个引用 在堆里面的具体地址)

方法区

  • 可以被 所有的线程共享
  • 包含了 所有的 class 和 static变量
int [] array =null;  // 栈里 出现  array

array = new int[10]; // 堆里 出现 一个框(空间),框里 有10个小框。

array[0] = 1; //堆 小框1,赋值1

Array Index Out Of Bounds Exception
Pet d =new  Pet();
d.name = "旺财";
d.age = 3;
d.shou();

方法区(堆里特殊的区域): main() ,常量池 旺财, Pet 的 name 和 age 属性,shout() 方法。
    静态方法区 暂无东西 static的 和类一起加载。

栈:
    d : 引用变量名。main()方法 入栈。
堆:
    new pet() 地址:0x0001
    name="旺财" //调用的 方法区的方法
    age=3
    shout()//叫的方法
        
    //在创建一个对象,又多一个区域

14. 数组

数组初始化

静态初始化

int[] a = {1,2,3};
Man[] mans = {new Man(1,1),new Man(2,2)};

动态初始化,如上,先new出来,然后对 索引赋值。

array = new int[10];  //未赋值,默认初始化为0

数组翻转

    public static int[] reverse(int[] arr) {
        int res[] = new int[arr.length];

        for (int i = 0, j = res.length - 1; i < arr.length; i++, j--) {
            res[j] = arr[i];
        }
        return res;
    }

        int[] arr = {1, 2, 3, 4, 5};
        System.out.println(Arrays.toString(reverse(arr)));

多维数组

数组的数组,二维数组 是特殊的一维数组。

  • 其 每一个元素 都是一个 一维数组。
int a[][] = new int[2][5];
//两行,5列的数组。
arr 数组中 : arr[0] arr[1]
arr[0] 中有 arr[0][0] 和 arr[0][1]
        //int [3][2]
        int[][] array = {{1, 2}, {3, 4}, {5, 6}};
        System.out.println(array.length); //数组长度为3
        System.out.println(array[0].length);//第一个索引的数组,长度为2
        System.out.println(array[2][1]);//3位置的 第二个数,为 6\
//二维数组,是一个 直角坐标系
//1 2  a[0][0] a[0][1]
//3 4  a[1][0] a[1][1]
//5 6  a[2][0] a[2][1]

        int[][] arr2 = new int[3][2];
        arr2[2][1] = 7;
        System.out.println(arr2[2][1]);

//int[3] 就是 ,3行。
//a[0]  a[0][0] a[0][1]
//a[1]
//a[2]

//a[0]  a[0][0] a[0][1]
//a[1]  a[1][0] a[1][1]
//a[2]  a[2][0] a[2][1]
        int[][] arr = {{1, 2}, {3, 4}, {5, 6}};

        //总长度为4
        for (int i = 0; i < arr.length; i++) {
            //子数组 长度为2
            for (int j = 0; j < arr[j].length; j++) {
                System.out.println(arr[i][j]);
            }
        }
Arrays
fill 赋值
sort 排序,升序
equals 比较数组中 元素值是否相等
binary Search 二分查找
asList
copyOf
toString
        int[] a2 = new int[]{1, 2, 3};
        //Arrays.fill(a2, 0); 还可以设置索引 如: a2,2,4,0。2,3 索引,填充为0

        System.out.println(Arrays.binarySearch(a2,2));

冒泡排序

  • 8种排序算法

两层循环,外层 冒泡轮数,里层 依次比较。

  • 时间复杂度 O(n2)

我的随便编写,可行

		int[] arr = new int[]{33, 21, 3, 4, 44, 23, 1};

		//从0开始 遍历,最大为:最大索引
        for (int i = 0; i < arr.length; i++) {
            //从i+1比较。
            for (int j = i + 1; j < arr.length; j++) {
                //保证:第一个数,永远是最小的。
                if (arr[i] > arr[j]) {
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }//内层循环
        }//外层循环

老师讲解的,本质是一样的

        //1. 比较 两个相邻的元素,如果 第一个数 比 第二个数大,就交换他们的位置。
        //2. 每次比较,都会产生出一个最大,(这里用最大)或者 最小的数字。
        //3. 下一轮 则可以少 一次排序。
        //4. 依次循环

        //遍历,判断 要走多少次。走:最大索引 -1次。 长度为5,最大索引为4。那就走:0,1,2,3。一共4次。因为:比较时,会用+1索引
        int maxIndex = arr.length - 1;

        for (int i = 0; i < maxIndex; i++) {

            boolean f = false;

            //比较 两个数,如果 第一个数 比 第二个数大,则交换
            for (int j = 0; j < maxIndex - i; j++) {

                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;

                    f = true;
                }
            }//内层循环

            //如果 从来没有 交换过,则跳出循环。
            if (f == false) {
                break;
            }
        }//外层循环

稀疏数组

  • 使用二维数组记录棋盘

  • 该 二维数组的很多值 默认值 0,记录了 很多没有意义的数据。

    • 大部分元素为0,或者 同一值得数组时。

处理方式:

  • 记录 一共有 几行几列,有多少个不同的值。
  • 把具有 不同值的元素 和 行列 及值记录在 一个小规模的数组中,
    • 从而缩小 程序的规模。
   行 列 值
[0] 6 7 8 第一行为:6行 7列,一共8个数。
[1] 0 3 22 索引:0,3 第一行,第4个值为 22 。也可以叫:0行3列(从1开始数)。
[2] 0 6 15
[3] 1 1 11 第二行,第2个值为11
0 0 0 0
0 0 1 0
0 0 0 2
0 0 0 0

4 4 2  4行4列,2个值
1 2 1  第1行,2列。 值为1
2 3 2  第2行,3列。值为2
        int[][] arr = new int[4][4];
        arr[1][2] = 1;
        arr[2][3] = 2;

        int count = 0;
        for (int[] as : arr) {
            for (int a : as) {
                System.out.print(a + "\t");
                if (a != 0) {
                    ++count;
                }
            }
            System.out.println();
        }

        System.out.println("============打印并统计完毕=================");
        //有2个值,就要存3行。0索引 存原始的行列信息。列:固定为3列(行 列 位置+值)。
        int[][] arr2 = new int[count + 1][3];

        //4行,4列,有效元素个数。
        arr2[0][0] = 4;
        arr2[0][1] = 4;
        arr2[0][2] = count;

        //稀疏数组的坐标
        int index = 0;

        //遍历 原始数组。固定原始的2层遍历。
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                //具体的值
                int t = arr[i][j];
                //如果值不为空
                if (t != 0) {
                    index++;
                    //存 行 列 和 值。因为:元数组,也是从0开始索引的,所以无需+1
                    arr2[index][0] = i;
                    arr2[index][1] = j;
                    arr2[index][2] = t;
                }//判空
            }//内层循环
        }//外层

        for (int i = 0; i < arr2.length; i++) {
            System.out.println(arr2[i][0] + "\t" + arr2[i][1] + "\t" + arr2[i][2]);
        }
0	0	0	0	
0	0	1	0	
0	0	0	2	
0	0	0	0	
============打印并统计完毕=================
4	4	2
1	2	1
2	3	2
  • 还原逻辑
        int row = arr2[0][0];
        int col = arr2[0][1];
        //定义 原数组的长度
        int[][] arr3 = new int[row][col];

        for (int i = 1; i < arr2.length; i++) {
            //行 列 值
            int hang = arr2[i][0];
            int lie = arr2[i][1];
            arr3[hang][lie] = arr2[i][2];
        }
        System.out.println("=========还原了======");
        for (int[] as : arr3) {
            for (int a : as) {
                System.out.print(a + "\t");
            }
            System.out.println();
        }

15. instanceof

Object o = new Student();
o instanceof Student; //true
o instanceof Person; //true
o instanceof Object; //true

16. 内部类

  • 成员内部类
  • 静态内部类
  • 局部内部类
  • 匿名内部类

成员

public class Outer {
    private int id = 10;

    public void out() {
        System.out.println("外部类:" + id);
    }

    //成员 内部类
    public class Inner {
        public void in() {
            System.out.println("内部类的方法");
        }

        public void getId() {
            //私有属性,私有方法都可以。
            System.out.println("内部类获得外部类的私有属性" + id);
        }
    }

    public static void main(String[] args) {
        Outer outer = new Outer();
        Inner inner = outer.new Inner();
        inner.in();
    }
}
  • 静态
    //成员 内部类
    public static class Inner {
        public void in() {
            System.out.println("内部类的方法");
        }

        //public void getId() 拿不到Id ,Id还没初始化呢,除非Id也 静态。
    }
  • 一个文件2个类
//增加,不能用 静态
class A{
    
}
  • 局部
    public void method() {
        class Inner {
        }
    }
接口 A = new 接口(){
	//实现接口的方法
};

17. 异常

  • 检查性异常
  • 运行时异常
    • 可被程序处理的
  • 错误:ERROR,比如:a调用b,b调用a
    • 脱离程序员控制的问题。如:栈溢出。
    • 灾难性的致命错误,程序无法控制和处理,
    • JVM 一般会 选择 终止线程
java.lang.Throwable
  • Throwable
    • Error 都要加这个前缀
      • VirtulMachine E
        • StackOverFlow E
        • Out Of Memory E
      • AWT E
    • Exception 都要加这个
      • IO E
        • EOF E
        • FileNotFound E
      • Runtime E ,比如上面的IO 和 Exception下的其他 都是非运行时
        • Arithmetic E 被除数不能为0 异常
        • Missing Resource E 资源丢失
        • ClassNotFound E
        • Illegal Argument E
        • ArrayIndex Out Of Bounds E
        • Unkown Type E

处理机制

  • try catch finally
  • throw throws
        try {
            a();
        } catch (Throwable e) { //Error 依然可以捕获到
            e.printStackTrace(); //打印栈 信息
            System.out.println("出现异常");
            System.exit(0);//结束程序
        } finally {
            System.out.println("最终");
        }


    public static void a() {
        b();
    }

    public static void b() {
        a();
    }

        if (0==0){
            throw new ArithmeticException();
        }

    public static void a() throws ArithmeticException{
        b();
    }
  • ctrl+alt+t 添加环绕方式,如 try catch finally ,循环等

自定义异常

项目中
public class CarToPException extends RuntimeException {

    private BaseStatusInter baseStatusInter;

    /**
     * 如果此字段为 null 则使用 baseStatusInter 的 msg 进行填充
     */
    public CarToPException(BaseStatusInter baseStatusInter, String msg) {
        super(null == msg ? baseStatusInter.getMsg() : msg);
        this.baseStatusInter = baseStatusInter;
    }

    public BaseStatusInter getBaseStatusInter() {
        return baseStatusInter;
    }

}

public interface BaseStatusInter {
    Integer getCode();

    String getMsg();
}

public enum ResponseCodeEnum implements BaseStatusInter {
    DEFAULT(10100, "服务器正忙,请稍后再试")
}
使用
public class MyException extends Exception {

    private int detail;

    public MyException(int a) {
        this.detail = a;
    }

    @Override
    public String toString() {
        return "MyException{" + detail + '}';
    }
}
public class MyException extends Exception {

    private int detail;

    public MyException(int a) {
        this.detail = a;
    }

    @Override
    public String toString() {
        return "MyException{" + detail + '}';
    }

    public static void main(String[] args) {

        try {
            if (1 == 1) {
                throw new MyException(1);
            }
        } catch (MyException e) {
            System.out.println("打印:" + e);
            e.printStackTrace();
        } finally {
        }
    }
}
打印:MyException{1}
MyException{1}
	at com.hai.tempmybatis.mytest.MyException.main(MyException.java:20)

18. HashMap

  • 有冲突时,使用链表,

  • 链表长度 >= 8 时,连表转为 红黑树

    • 红黑树的开销 维护非常大,
  • 初始容量,是 1<<4; 为:16

    • HashMap 实现 存储高效,尽量做到,把数据分配均匀。实际就是:hash%length
      • 转为位运算 hash&(length-1)
      • 因为:2的n次方 实际就是1后面n个0,2的n次方-1,就是n个1
      • 使用扰动函数,
      • 负载因子 0.75
        • 大的时候: 节省空间资源,浪费时间资源
        • 小的时候:节省时间资源,浪费空间资源。
  • jdk1.7 数组+链表

  • jdk1.8 hash表=数组+链表+红黑树

19. IO流

  • 序列化,反序列化,Serializable

    • transient 某一个属性是透明的,序列化 不考虑
  • 字节流

    • Output 程序接收
    • input 数据源
      • InputStream
      • OutputStream
  • 字符流 有中文,用此

    • Writer
    • Reader
  • 节点流:

    • CharArrayReader,CharArrayWriter,InputStream,OutputStream
    • StringReader,StringWriter 内存中 操作String
    • PipedOutputStream,PipedInputStream 管道流
    • File
  • 处理流

    • buffer 四个
      • BufferInputStream
      • BufferWriter
    • filter 过滤流 四个。
      • FilterInputStream
    • Converting 转换流
      • InputstreamReader 输入对应读
      • OutputStreamWriter 输出对应写
    • Data
      • DataInputStream
      • DataOutputStream
    • Object流
      • 四个
    • print
      • PrintWriter
      • PrintStream
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值