Java基础入门(非新手向)



var code = “aa1f2902-78cc-4d00-be13-2a8ac94a1f5f”

一、String

  1. String和StringBuilder的转换
    - s=sb.toString()
    - sb=new StringBuilder(s)
  2. 创建String
    - new String(char [])
    - string=“abc”
  3. string特点:
    - new出来的地址值不同,内容可能相同
    - " "出来的如果内容相同就是同一个对象(内存池)

二、输入

    public static void main(String[] args) {
        //创建一个扫描器对象
        Scanner in = new Scanner(System.in);
        
        //空白作为结束符 cin
        String s1 = in.next();
        //Enter作为结束符 getline()
        String s2 = in.nextLine();
    }

三、面向对象

1.继承

  1. 子构造默认访问父无参构造
  2. 支持多重继承,不支持多继承
  3. final不可被重写或改变,也不可被继承
  4. static方法只能访问static成员变量
  5. super.调用父类成员变量

2.多态

  1. fa x=new son() //向上转型
  2. 编译看左边,运行看右边
  3. son y=(son)x //向下转型

3.接口

  1. 很像是一个抽象类
  2. 成员默认是 public static final
  3. implements 支持"多继承"
  4. 必须要实现接口里的抽象方法

4.内部类

  1. 成员内部类:类成员
    - 外部类名.内部类名 x=外部类对象.内部类对象(如果内部类是私有,就在外部类里写方法调用内部类的函数)
    - Outer.Inner x=new Outer().new Inner()
  2. 局部内部类:类局部(成员方法里面)
  3. 匿名内部类:(tip:作为谓词使用)
new inter(){
        public void fn(){
            
        }
    }

四、基本类型包装类

以Integer为例介绍包装类

  1. 创建对象:Integer a=Integer.valueOf(1);
  2. int – string:String s = String.valueOf(int)
  3. string – int : int i = Integer.parseInt(String);
  4. 自动装箱与拆箱:i++ 是合理的

五、异常处理

  • 格式:
		try {
            //可能出错的代码
        }catch (/*异常类名 变量名*/Exception e){
            //异常的处理代码
            e.printStackTrace();
            e.getMessage();
        }finally {
            //异常处理后需要执行的代码
        }`

六、集合类

在这里插入图片描述

1.Arraylist是vector
2.LinkedList是list
3.HashSet是纯哈斯表
4.TreeSet是set
5.HashMap是不排序的map
6.TreeMap是map

  • 其中自动排序的可以使用匿名内部类实现排序的依据
TreeSet<student> treeSet = new TreeSet<student>(new Comparator<student>() {
            @Override
            public int compare(student o1, student o2) {
                //按照谓词规则来写即可
            }
        })

1.遍历

  1. List 遍历:
  • 注意 list遍历时不可以做增删操作(借鉴vector)
		//迭代器遍历
        Iterator<String> iterator = arrayList.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
        //索引遍历
        for(int i=0;i<arrayList.size();i++){
            System.out.println(arrayList.get(i));
        }
        //增强for遍历
        for(String s:arrayList){
            System.out.println(s);
        }
  1. Map遍历
		//键值对遍历
        for (Map.Entry<String, String> entry : hashMap.entrySet()) {
            //entry是键值对
            System.out.println(entry.getKey()+entry.getValue());
        }
        //Key遍历  
        for(String s:hashMap.keySet()){
            System.out.println(s+hashMap.get(s));
        }

七、文件流

1.概述

在这里插入图片描述

  1. 输入流:
    ByteArrayInputStream、StringBufferInputStream、FileInputStream 是三种基本的介质流,它们分别从Byte 数组、StringBuffer、和本地文件中读取数据。
  2. 输出流:
    OutputStream 是所有的输出字节流的父类,它是一个抽象类。
    ByteArrayOutputStream、FileOutputStream 是两种基本的介质流,它们分别向Byte 数组、和本地文件中写入数据。
  3. 转换流:
    InputStreamReader 、OutputStreamWriter 要InputStream或OutputStream作为参数,实现从字节流到字符流的转换。

另:处理纯文本数据,就优先考虑使用字符流。 除此之外都使用字节流

2.案例

  1. 字节缓冲流一次读写一个字节数组
		BufferedInputStream bis= new BufferedInputStream(new FileInputStream("文件绝对地址"));
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("文件绝对地址"));
        
        byte[] bytes=new byte[1024];
        int len;
        while((len=bis.read(bytes))!=-1){
            bos.write(bytes);
        }

        bis.close();
        bos.close();
  1. 字符缓冲流
        //字符流一次读写一个字符数组
        BufferedReader br=new BufferedReader(new FileReader(""));
        BufferedWriter bw=new BufferedWriter(new FileWriter(""));
        
        char[] chars=new char[1024];
        int len;
        while((len=br.read(chars))!=-1){
            bw.write(chars);
        }
        
        br.close();
        bw.close();
        
        //字符缓冲流特有功能
        BufferedReader br=new BufferedReader(new FileReader(""));
        BufferedWriter bw=new BufferedWriter(new FileWriter(""));
        
        String s;
        while((s=br.readLine())!=null){
            bw.write(s);
            bw.newLine();
            bw.flush();
        }
        
        br.close();;
        bw.close();
  1. 复制单级文件夹
public class file_again {
    public static void main(String[] args) throws IOException {

        //创建数据源目录对象和目的地目录对象
        File srcFolder =new File("E:\\zFrom");
        File destFolder =new File("E:\\zTo");
        if (!destFolder.exists()){
            destFolder.mkdir();
        }

        //获取数据源目录下的所有文件
        File[] listFiles = srcFolder.listFiles();

        //遍历File数组,得到每一个File对象,该File对象,其实就是数据源
        for(File srcFile:listFiles){

            String srcFileName = srcFile.getName();
            File destFile=new File(destFolder,srcFileName);
            copyFile(srcFile,destFile);

        }
    }

    private static void copyFile(File srcFile, File destFile)throws IOException {
        BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcFile));
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destFile));

        byte[] bys=new byte[1024];
        int len;
        while((len=bis.read(bys))!=-1){
            bos.write(bys,0,len);
        }

        bos.close();
        bis.close();
    }
}
  1. 复制多级文件夹
public class file_again {
    public static void main(String[] args) throws IOException {

        //创建数据源目录对象和目的地目录对象
        File srcFile =new File("E:\\zFrom");
        File destFile =new File("E:\\zTo");

        copyFolder(srcFile,destFile);
    }

    //复制文件夹
    private static void copyFolder(File srcFile, File destFile) throws IOException {

        //递归  如果是目录就copy目录 如果不是就copy文件
        if(srcFile.isDirectory()){
            String srcFileName = srcFile.getName();
            File newFolder =new File(destFile,srcFileName);//相当于叠加路径destFile+srcFileName
            if(!newFolder.exists()){
                newFolder.mkdir();
            }

            //获取数据源File下所有文件或目录
            File[] listFiles = srcFile.listFiles();
            for(File file:listFiles){
                copyFolder(file,newFolder);
            }
        }else {
            File newfile = new File(destFile, srcFile.getName());
            copyFile(srcFile,newfile);
        }

    }
    

    //复制文件改进版
    private static void copyFile(File srcFile, File destFile)throws IOException {

        BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcFile));
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destFile));

        try(bis;bos) {

            byte[] bys=new byte[1024];
            int len;
            while((len=bis.read(bys))!=-1){
                bos.write(bys,0,len);
            }

        }catch (IOException e){
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }
}

八、线程和进程

这个部分我也不是很清楚怎么使用 先把知识点记一下

1.基础知识


//main方法中调用start()开启线程

public class MyThread implements Runnable{
    
    @Override
    public void run() {

        fn1();
        fn2();

    }
    
    //synchronized关键字为上锁
    public synchronized void fn1() {
        //同步方法: 锁对象是this
    }
    public static synchronized void fn2(){
        //同步静态方法: 锁对象为类名.class
    }
}

2.Lock

public class MyThread implements Runnable{

    private Lock lock=new ReentrantLock();
    
    @Override
    public void run() {
        
        while(true){
            try {
                lock.lock();
                //可能出错代码
            }finally {
                lock.unlock();
            }
        }
        
    }
}

3.消费者生产者案例

  1. 给定生产者类和消费者类,给定box成员变量,实例化对象并开启线程
  2. 奶箱类配备put和get方法,分别由消费者和生产者调用
  3. put和get里调用wait()和notifAll()方法,实现生产一瓶消费一瓶
public class Box {
    private int milk;

    private boolean book = false;

    public synchronized void put(int x) {
        if (book) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        this.milk = x;
        System.out.println(x + "入");

        book = true;

        notifyAll();
    }

    public synchronized void get() {
        if (!book) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println(milk + "出");

        book = false;

        notifyAll();
    }
}

总结

  • 没有总结
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值