【JAVA黑马程序员笔记】三P233-P312(List、Set、比较器、泛型、Map、File、IO流...)

2021/12/15 9:47:17

本文主要是介绍【JAVA黑马程序员笔记】三P233-P312(List、Set、比较器、泛型、Map、File、IO流...),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

P233 数据结构

  1. 栈:数据进入栈模型叫做压栈,数据离开栈模型为弹栈。栈是后进先出的模型。
  2. 队列:数据从后端进入队列模型的过程叫做入队列,离开队列的过程叫出队列。队列是先进先出的模型。
  3. 数组:查询数组通过索引定位,查询任意数据耗时相同,查询效率高,删除数据时,要将原始数据删除,同时每个后面数据要前移,删除效率低。添加元素也是,添加后的每个数据后移,再添加元素,添加效率低。数据是查询快增删慢的模型。
  4. 链表:链表是增删快,查询慢的模型。

P235 List集合子类的特点

  1. ArrayList,底层数据结构是array,查询快增减慢
  2. LinkedList,底层数据结构是link,增减快,查询慢

linklist的特有方法
在这里插入图片描述

P238 set集合概述和特点

    public static void main(String[] args) {
        Set<String> set = new HashSet<String>(); // 对迭代顺序不做保证

        set.add("hello");
        set.add("world");
        set.add("world"); // 不包含重复元素
        set.add("java");

        for(String s :set)
            System.out.println(s);

    }

P239 哈希值![在这里在这里插入图片描述

插入图片描述](https://www.www.zyiz.net/i/ll/?i=47cda787bed04956b359fb1b8f974434.png)

P240 HashSet集合概述和特点

在这里插入图片描述

P244 LinkedHashSet集合概述和特点

在这里插入图片描述

P245 TreeSet集合概述和特点

在这里插入图片描述

P246 自然排序

就是让所属类实现Comparable接口,重写compareTo(T o)方法。
在这里插入图片描述

public class Student implements Comparable<Student>{
    private String name;
    private int age;

    public Student(){
        System.out.println("无参构造方法");
    }
    public Student(String name,int age){
        this.name = name;
        this.age = age;
        System.out.println("有残构造方法");
    }
    public void setName(String nanme)
    {
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public int getAge(){return age;}
    public void setAge(int a)
    {
        age = a;
    }
    public void show(){
        System.out.println(name+"_" + age);
    }

    @Override
    public int compareTo(Student s) {
        int num = this.getAge() - s.getAge();
        int num2 = num == 0?this.getName().compareTo(s.getName()):num;
        return num2;

    }
}

P247 比较器comparator的使用

       TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {

                int num = o1.getAge() - o2.getAge();
                int num2 = num==0?o1.getName().compareTo(o2.getName()):num;
                return num2;


            }
        });

P250 泛型概述

如果没有传入类型的话,将默认为Object的类型。
在这里插入图片描述

在这里插入图片描述

P251 泛型类

public class Generic<T> {
    private T t;

    public T getT(){
        return t;
    }

    public void setT(T t) {
        this.t = t;
    }
}



主函数使用

    Generic<Integer> g2 = new Generic<Integer>();
        g2.setT(30);
        System.out.println(g2.getT());

P252泛型方法

举个例子

  public <T> void show(T t){
        System.out.println(t);
    }

P253泛型接口

// 接口
public interface GenericInterface<T>{
     void show(T t);
}
//接口实现

public class GenericInterfaceImpl<T> implements GenericInterface<T>{
    @Override
    public void show(T t) {
        System.out.println(t);
    }
}

//测试例中实例对象
     GenericInterface<String> gg = new GenericInterfaceImpl<String>();
                gg.show("hello");

P254 类型通配符

在这里插入图片描述

        // 下面这行代码报错,因为他的上限是Number,所以他不能是Number的父类
        //List<? extends Number> list4 = new ArrayList<Object>();
        List<? extends Number> list5 = new ArrayList<Number>();
        List<? extends Number> list6 = new ArrayList<Integer>();

        // 限定下限
        List<? super Number> list7 = new ArrayList<Object>();
        List<? super Number> list8 = new ArrayList<Number>();

        // 下面这行代码报错,反正就是报错
        //List<?super Number> list9 = new ArrayList<Integer>();

255 可变参数

在这里插入图片描述
最后一行的意思其实就是:

sum(int b,int... a);
    //a其实是个数组:调用方法  sum(10,20,30,40);
    public static int sum(int... a){
        int sum = 0;
        for(int i:a)
            sum+=i;
        return sum;
    }

P256 可变参数的使用

在这里插入图片描述

       List<String> list = Arrays.asList("hello","world","java");
        //不能增、删
        // list.add("jaa");
        // list.remove("world");
        list.set(0,"ss");
        List<String> list2 = List.of("hello","world","java");

        // 注释的是都不能执行的,他们不能增删改
        // list2.add("jaksdf");
        //list2.remove("asdf");
        //list.set(0,"ssa");
        Set<String> set = Set.of("hello","world","java");

        // 也是不能add remove

P257 Map

在这里插入图片描述

      public static void main(String[] args) {
        Map<String,String> map = new HashMap<String,String>();
        map.put("aaa","111");
        map.put("bbb","222");
        map.put("ccc","333");

        System.out.println(map);
        System.out.println(map.remove("aaa"));

        System.out.println(map.containsKey("bbb"));
        System.out.println(map.isEmpty());
        System.out.println(map.size());

        map.clear();
        System.out.println(map);
    }

Map集合的获取功能

在这里插入图片描述

P260Map遍历的方法

       //1. 获取所有键的集合
        Set<String> keySet = map.keySet();
        for(String key:keySet){
            String value = map.get(key);
            System.out.println(key+","+value);
        }

        //2.遍历方式2
        Set<Map.Entry<String,String>> entrySet = map.entrySet();
        for(Map.Entry<String,String> me  :entrySet){
            String key = me.getKey();
            String value = me.getValue();
            System.out.println(key+","+value);
        }

P267 Collections概述和使用

collection是单列集合的顶层结构,collections是具体的类

        List<Integer> list = new ArrayList<Integer>();

        list.add(30);
        list.add(20);
        list.add(10);
        list.add(40);
        list.add(50);

        Collections.sort(list);
        Collections.reverse(list); //反转
        Collections.shuffle(list); // 随机置换 常用于模拟洗牌

对类进行排序
通过使用指定的比较器来进行排序。

      List<Student> array = new ArrayList<Student>();
        Collections.sort(array, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
               int num = o1.getAge() - o2.getAge();
               int num2 = num==0?o1.getName().compareTo(o2.getName()):num;
               
               return num2;
            }
        });

P272 File类概述和构造方法

File类的创建功能
在这里插入图片描述
File类判断和获取功能
在这里插入图片描述

        File f1 = new File("/home/usr/Code/JavaCode/test.txt");
        System.out.println(f1);

        File f2 = new File("/home/usr/Code/JavaCode","test.txt");
        System.out.println(f2);
        File f3 = new File("/home/usr/Code/JavaCode");
        File f4 = new File(f3, "text.txt");
        System.out.println(f4);




//创建文件
    public static void main(String[] args) throws IOException {
        File f1 = new File("/home/usr/Code/JavaCode/test1.txt");
        System.out.println(f1.createNewFile());//文件若不存在就创建文件并返回true,否则返回false

        File f2 = new File("/home/usr/Code/JavaCode/mkdirtest");
        System.out.println(f2.mkdir());//目录若不存在就创建文件并返回true,否则返回false
        System.out.println(f2);
        File f3 = new File("/home/usr/Code/JavaCode/mkdirtest1/mkdirtest2/mkdirtest22");
        System.out.println(f3.mkdirs());
        File f4 = new File(f3, "text.txt");
        System.out.println(f4);
        System.out.println(f1.mkdir()); // 这个时候将创建一个目录,即使文件名给的是test1.txt
    }




//File类的常见功能
  File f1 = new File("test3.txt");
        System.out.println(f1.createNewFile());
        System.out.println(f1.isDirectory());
        System.out.println(f1.isFile());
        System.out.println(f1.exists());

        System.out.println(f1.getAbsoluteFile());
        System.out.println(f1.getPath());
        System.out.println(f1.getName());


        File f2 = new File("idea_test/src/ArraysPkg");
        String[] strArry = f2.list();
        for(String str:strArry){
            System.out.println(str);
        }
        File[] f3 = f2.listFiles();
        for(File file:f3){
            System.out.println(file);//输出了绝对路径
        }

//删除文件
        File f1 = new File("test1.txt");
        System.out.println(f1.createNewFile());
        System.out.println(f1.delete());


        // 创建目录
        File f2 = new File("test");
        //System.out.println(f2.mkdir());
        File f3 = new File("test/test1.txt");
        //System.out.println(f3.createNewFile());


        // 若要删除的目录下有内容,应该先把内容给删了,再删目录
        System.out.println(f2.delete());

递归遍历文件名

  public static void main(String[] args) {

        File srcFile = new File("idea_test");
        getAllFilePath(srcFile);

    }
    public static void getAllFilePath(File srcFile){
        File[] fileArray = srcFile.listFiles();

        if(fileArray !=null){
            for(File file:fileArray){
                if(file.isDirectory()){
                    getAllFilePath(file);
                }
                else{
                    System.out.println(file.getAbsolutePath());
                }
            }
        }
    }

P279 IO流概述和分类

在这里插入图片描述

字节流写数据

        // 创建字节输出流对象:1.调用系统功能创建了文件;2.创建了字节输出流对象;3.让字节输出流对象指向文件
        FileOutputStream f1 = new FileOutputStream("test.txt");
        f1.write(95);
        f1.close(); // 释放

字节流写数据的三种形式、追加写入、换行

    public static void main(String[] args) throws IOException {
        // 第二个参数为true,可以让文件的每次写入都写入到末尾,而不是开头
        FileOutputStream f1 = new FileOutputStream("test2.txt",true);

        f1.write(97);
        f1.write(98);
        f1.write(99);
        f1.write(100);
        f1.write(101);

        byte[] bys = {97,98,99,100,101,102,103};
        byte[] bys2 = "abcde".getBytes(StandardCharsets.UTF_8);
        f1.write(bys);
        f1.write(bys2,0,3);//从0开始,写入长度为3的数据
        f1.write("\n".getBytes(StandardCharsets.UTF_8)); // 实现换行,windows下需要\r\n

        f1.close();




    }

字节流数据异常处理

  public static void main(String[] args) {
            FileOutputStream f1 = null;
        try {
            // 创建字节输出流对象:1.调用系统功能创建了文件;2.创建了字节输出流对象;3.让字节输出流对象指向文件
             f1 = new FileOutputStream("test.txt");
            f1.write(95);
            f1.close(); // 释放
        }catch (IOException e){
            e.printStackTrace();;
        }
        finally {
            if(f1 !=null){
            // z执行清除操作,防止write异常后,无法成功退出
            try{
                f1.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }}

字节读数据(单字节读入和循环读入)

       FileInputStream f1 = new FileInputStream("test2.txt");

        int by = f1.read();
        System.out.println(by);
        System.out.println((char)by);


        //如果达到文件末尾,输出-1

        while((by =f1.read())!=-1){
            System.out.println((char)by);

        }

        f1.close();

字节读输入2

       FileInputStream f1 = new FileInputStream("test2.txt");
       byte[] bys = new byte[5];
        // 一次读取5个数据,f1.read()返回实际读取的个数
        int len = f1.read(bys);
        System.out.println(len);
        System.out.println(new String(bys,0,len));

        // 循环读取
        byte[] bys1 = new byte[1024];
                int len2;
        while ((len2=f1.read(bys))!=-1){
            System.out.println(new String(bys,0,len2));

        }


        f1.close();

字节缓冲流读写数据

       // FileOutputStream fos = FileOutputStream("test2.txt");
        BufferedOutputStream bos= new BufferedOutputStream(new FileOutputStream("test2.txt"));

        //write data
        bos.write("hello".getBytes(StandardCharsets.UTF_8));
        bos.write("world\r\n".getBytes(StandardCharsets.UTF_8));
        bos.close();

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("test2.txt"));

        int by;
        while((by=bis.read())!=-1){
            System.out.println((char)by);
        }

       //第二种方式
        byte[] bys = new byte[1024];
        int len;
        while((len=bis.read(bys))!=-1){
            System.out.println(new String(bys,0,len));
            
        }


        bis.close();

P290 为什么出现字符流

在这里插入图片描述

P291 编码表和GBK

在这里插入图片描述

P292 字符串中的解码问题

在这里插入图片描述

    String s = "中国";
        byte[] bys = s.getBytes(StandardCharsets.UTF_8);   // [-28, -72, -83, -27, -101, -67]
        byte[] bys2 = s.getBytes("GBK"); // [-42, -48, -71, -6]
        System.out.println(Arrays.toString(bys2));
        String ss = new String(bys); // 解码
        System.out.println(ss);

P293 字符流中的编码解码问题

注意保证读写流的编码应该相同。

    FileOutputStream fos = new FileOutputStream("test.txt");

        OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");

        osw.write("中国!!!!!!!!");

        osw.close();

        InputStreamReader isr = new InputStreamReader(new FileInputStream("test.txt"), "UTF-8");
        

        int ch;
        while((ch=isr.read())!=-1){
            System.out.println((char)ch);
        }


        isr.close();

字符流写数据的5中方式

在这里插入图片描述

     OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("test.txt"));

        osw.write(97); //
        osw.flush(); //刷新流 刷新到文件里,或者等到close执行完,close执行先刷新再关闭的

        char[]  chs = {'a','b','c','d','e'};
        osw.write(chs);
        osw.write(chs,0,2);
        osw.write("hello");
        osw.write("helllllllo",0,5);
        


        osw.close();

字符流读数据的两种方式

和字节流读数据的方式是相同的

        InputStreamReader isr = new InputStreamReader(new FileInputStream("test.txt"));

        //way 1
        int ch;
        while((ch=isr.read())!=-1){
            System.out.println((char)ch);

        }
        //way 2
        char[] chs=new char[1024];
        int len;
        while((len=isr.read(chs))!=-1){
            System.out.println(new String(chs,0,len));
        }


        isr.close();

在P297中还提到了FileReader和FileWriter,不写了。。

P298 字符缓冲流

        FileWriter fw = new FileWriter("test.txt");
        BufferedWriter bw = new BufferedWriter(fw);

        bw.write("hello\r\n");
        bw.write("world");
		 bw.newLine();
        bw.close();
        BufferedReader br = new BufferedReader(new FileReader("test.txt"));
 	 String ss = br.readLine();
       char[] chs = new char[1024];
       int len;

       while((len=br.read(chs))!=-1){
           System.out.println(new String(chs,0,len));
       }
        br.close();

字符缓冲流的特有功能
String ss = br.readLine(); //读取一行只读内容,不读换行的符号
bw.newLine(); //换行 最好使用这种方式,而不是\r\n

一种新的读取方法

      String line;
       while((line=br.readLine())!=null){
           System.out.println(line);
       }

P302 IO流小结

在这里插入图片描述
在这里插入图片描述

P311 复制文件的异常处理

在这里插入图片描述

P312 标准流

in,out
 1

        InputStream is = System.in;
/*
        int by;
        while((by=is.read())!=-1){
            System.out.println((char)by);

        }*/
        //上面的方法不能读入中文

        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        System.out.println("请输入一个字符串,含中文也行");

        String line = br.readLine();
        System.out.println("你输入了:" + line);

        System.out.println("请输入一个整数:");
        int i = Integer.parseInt(br.readLine());
        System.out.println("你输入的整数是" + i);

out

       PrintStream ps = System.out;

        ps.print("hello");
        ps.print(100);
        ps.println("hello");
        ps.print(100);
        ps.close();


这篇关于【JAVA黑马程序员笔记】三P233-P312(List、Set、比较器、泛型、Map、File、IO流...)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程