(6.16)-java基础复习

好久没更新博客了,只是最近挺迷茫的,一直在提升学历和实习找工作中迷茫。近期我在想要不要接点外包的单,但又想起来我没技术,自己挺想写小说的,但又没时间每天写。【纠结体日常】

在这里插入图片描述
我就边复习java,边找面试题把
【顺便,看下我能鸽多少天才更新】

把返回类型设置成void,表示该方法不返回任何值

八种基本类型
整型(四种):byte,short,int,long
字符型(一种):char
浮点型(两种):float,double
布尔型(一种):Boolean

String类型并不是基本引用类型

\t 制表符
\r 回车
\n 换行
’ " ’ 双引号
’ ’ ’ 单引号
’ \ ’ 反斜杠

今天还顺便复习了cmd命令:
shutdown -s 关闭计算机
shutdown -s -t 60 60秒后关机
shutdown -a 取消关机
shutdown -r 关机后重启

HashMap和Hashtable的区别
1.HashMap可以存放null,Hashtable不能存放null
2.HashMap不是线程安全的类,Hashtable是线程安全的类

StringBuffer和StringBuilder的区别
1.StringBuffer是线程安全的,StringBuilder是非线性安全的

因为非线性安全的不需要同步,所以会比线程安全的快。

ArrayList和Vector的区别
Vector是线程安全的类,而ArrayList是非线程安全的。

数组的选择法排序

public class HelloWorld {
    public static void main(String[] args) {
        int a [] = new int[]{18,62,68,82,65,9};
        //排序前,先把内容打印出来
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println(" ");
        //选择法排序
     
        //第一步: 把第一位和其他所有位进行比较
        //如果发现其他位置的数据比第一位小,就进行交换
         
        for (int i = 1; i < a.length; i++) {
            if(a[i]<a[0]){  
                int temp = a[0];
                a[0] = a[i];
                a[i] = temp;
            }
        }
        //把内容打印出来
        //可以发现,最小的一个数,到了最前面
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println(" ");
         
        //第二步: 把第二位的和剩下的所有位进行比较
        for (int i = 2; i < a.length; i++) {
            if(a[i]<a[1]){  
                int temp = a[1];
                a[1] = a[i];
                a[i] = temp;
            }
        }
        //把内容打印出来
        //可以发现,倒数第二小的数,到了第二个位置
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println(" ");       
         
        //可以发现一个规律
        //移动的位置是从0 逐渐增加的
        //所以可以在外面套一层循环
         
        for (int j = 0; j < a.length-1; j++) {
            for (int i = j+1; i < a.length; i++) {
                if(a[i]<a[j]){  
                    int temp = a[j];
                    a[j] = a[i];
                    a[i] = temp;
                }
            }
        }
         
        //把内容打印出来
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println(" ");       
    }
}

冒泡法排序

public class HelloWorld {
    public static void main(String[] args) {
        int a [] = new int[]{18,62,68,82,65,9};
        //排序前,先把内容打印出来
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println(" ");
        //冒泡法排序
      
        //第一步:从第一位开始,把相邻两位进行比较
        //如果发现前面的比后面的大,就把大的数据交换在后面
          
        for (int i = 0; i < a.length-1; i++) {
            if(a[i]>a[i+1]){  
                int temp = a[i];
                a[i] = a[i+1];
                a[i+1] = temp;
            }
        }
        //把内容打印出来
        //可以发现,最大的到了最后面
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println(" ");
          
        //第二步: 再来一次,只不过不用比较最后一位
        for (int i = 0; i < a.length-2; i++) {
            if(a[i]>a[i+1]){  
                int temp = a[i];
                a[i] = a[i+1];
                a[i+1] = temp;
            }
        }
        //把内容打印出来
        //可以发现,倒数第二大的到了倒数第二个位置
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println(" ");       
          
        //可以发现一个规律
        //后边界在收缩
        //所以可以在外面套一层循环
          
        for (int j = 0; j < a.length; j++) {
            for (int i = 0; i < a.length-j-1; i++) {
                if(a[i]>a[i+1]){  
                    int temp = a[i];
                    a[i] = a[i+1];
                    a[i+1] = temp;
                }
            }
        }
          
        //把内容打印出来
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println(" ");       
    }
}

leetcode的排序数组题

题目:
给你一个整数数组 nums,请你将该数组升序排列。

示例 1:

输入:nums = [5,2,3,1]
输出:[1,2,3,5]
示例 2:

输入:nums = [5,1,1,2,0,0]
输出:[0,0,1,1,2,5]

提示:

1 <= nums.length <= 50000
-50000 <= nums[i] <= 50000
题解:

class Solution {
    public int[] sortArray(int[] nums) {
        randomizedQuicksort(nums, 0, nums.length - 1);
        return nums;
    }

    public void randomizedQuicksort(int[] nums, int l, int r) {
        if (l < r) {
            int pos = randomizedPartition(nums, l, r);
            randomizedQuicksort(nums, l, pos - 1);
            randomizedQuicksort(nums, pos + 1, r);
        }
    }

    public int randomizedPartition(int[] nums, int l, int r) {
        int i = new Random().nextInt(r - l + 1) + l; // 随机选一个作为我们的主元
        swap(nums, r, i);
        return partition(nums, l, r);
    }

    public int partition(int[] nums, int l, int r) {
        int pivot = nums[r];
        int i = l - 1;
        for (int j = l; j <= r - 1; ++j) {
            if (nums[j] <= pivot) {
                i = i + 1;
                swap(nums, i, j);
            }
        }
        swap(nums, i + 1, r);
        return i + 1;
    }

    private void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}

感觉基础前面流程,变量,数组什么的看了几百遍了,但现实中没咋用到
在这里插入图片描述
然后类和对象,因为是面向对象,现实用的多一点

又复习了重写和重载
面试被问到
1、重写实现的是运行时的多态,而重载实现的是编译时的多态。 2、重写的方法参数列表必须相同;而重载的方法参数列表必须不同。 3、重写的方法的返回值类型只能是父类类型或者父类类型的子类,而重载的方法对返回值类型没有要求

单例模式
单例模式又叫做 Singleton模式,指的是一个类,在一个JVM里,只有一个实例存在。

饿汉式单例模式

GiantDragon 应该只有一只,通过私有化其构造方法,使得外部无法通过new 得到新的实例。
GiantDragon 提供了一个public static的getInstance方法,外部调用者通过该方法获取12行定义的对象,而且每一次都是获取同一个对象。 从而达到单例的目的。
这种单例模式又叫做饿汉式单例模式,无论如何都会创建一个实例

package charactor;
 
public class GiantDragon {
 
    //私有化构造方法使得该类无法在外部通过new 进行实例化
    private GiantDragon(){
         
    }
 
    //准备一个类属性,指向一个实例化对象。 因为是类属性,所以只有一个
 
    private static GiantDragon instance = new GiantDragon();
     
    //public static 方法,提供给调用者获取12行定义的对象
    public static GiantDragon getInstance(){
        return instance;
    }
     
}


package charactor;
 
public class TestGiantDragon {
 
    public static void main(String[] args) {
        //通过new实例化会报错
//      GiantDragon g = new GiantDragon();
         
        //只能通过getInstance得到对象
         
        GiantDragon g1 = GiantDragon.getInstance();
        GiantDragon g2 = GiantDragon.getInstance();
        GiantDragon g3 = GiantDragon.getInstance();
         
        //都是同一个对象
        System.out.println(g1==g2);
        System.out.println(g1==g3);
    }
}

懒汉式单例模式

懒汉式单例模式与饿汉式单例模式不同,只有在调用getInstance的时候,才会创建实例

package charactor;
 
public class GiantDragon {
  
    //私有化构造方法使得该类无法在外部通过new 进行实例化
    private GiantDragon(){       
    }
  
    //准备一个类属性,用于指向一个实例化对象,但是暂时指向null
    private static GiantDragon instance;
      
    //public static 方法,返回实例对象
    public static GiantDragon getInstance(){
        //第一次访问的时候,发现instance没有指向任何对象,这时实例化一个对象
        if(null==instance){
            instance = new GiantDragon();
        }
        //返回 instance指向的对象
        return instance;
    }
      
}



package charactor;
 
public class TestGiantDragon {
 
    public static void main(String[] args) {
        //通过new实例化会报错
//      GiantDragon g = new GiantDragon();
         
        //只能通过getInstance得到对象
         
        GiantDragon g1 = GiantDragon.getInstance();
        GiantDragon g2 = GiantDragon.getInstance();
        GiantDragon g3 = GiantDragon.getInstance();
         
        //都是同一个对象
        System.out.println(g1==g2);
        System.out.println(g1==g3);
    }
}

关于饿汉懒汉how2j说的比较明显
饿汉式是立即加载的方式,无论是否会用到这个对象,都会加载。如果在构造方法里写了性能消耗较大,占时较久的代码,比如建立与数据库的连接,那么就会在启动的时候感觉稍微有些卡顿。懒汉式,是延迟加载的方式,只有使用的时候才会加载。 并且有线程安全的考量,使用懒汉式,在启动的时候,会感觉到比饿汉式略快,因为并没有做对象的实例化。 但是在第一次调用的时候,会进行实例化操作,感觉上就略慢。
看业务需求,如果业务上允许有比较充分的启动和初始化时间,就使用饿汉式,否则就使用懒汉式

面试会问:
什么是单例模式?
回答的时候,要答到三元素

  1. 构造方法私有化
  2. 静态属性指向实例
  3. public static的 getInstance方法,返回第二步的静态属性

枚举基本上都是用四季举例子,其实就是在swich用的多

public class HelloWorld {
    public static void main(String[] args) {
        Season season = Season.SPRING;
        switch (season) {
        case SPRING:
            System.out.println("春天");
            break;
        case SUMMER:
            System.out.println("夏天");
            break;
        case AUTUMN:
            System.out.println("秋天");
            break;
        case WINTER:
            System.out.println("冬天");
            break;
        }
    }
}
public enum Season {
    SPRING,SUMMER,AUTUMN,WINTER
}

在这里插入图片描述
new出来啊【不是】

posted on 2021-06-16 21:09  Arya32f  阅读(24)  评论(0编辑  收藏  举报

导航