第四周总结

1.泛型:

1.1 泛型在类中的语法格式:

class  Student <T>{
    
}

1.2 泛型在方法中的语法格式:

public <T> void test (T t){
    
}

但是如果在一个有泛型的类中创建一个有泛型的方法的话,是不带d的,不然类的泛型和 方法的参数就没关系了,就没意义了。

在方法中带有泛型是要有参数的,不然泛型也就没意义了。

1.3 泛型在抽象类中的语法格式:

abstract class 类名 <T> {
    
}

1.4 泛型在接口中的语法格式:

interface 接口名字 <T> {
    
}

2.集合

2.1 集合直接的关系图

在这里插入图片描述

2.2 Collection接口

Collection是一个接口,是无法直接实例化的,但是Collection下有可以直接实例它的实体类:ArrayList,LinkedList,HashSet,TreeSet

2.3.1 Collection下的方法:

增: add, addAll

 public static void main(String[] args) {
        Collection<String> test = new ArrayList<>();
        Collection<String> test1 = new ArrayList<>();
        test1.add("a");
        test1.add("b");
        test1.add("c");
        test1.add("f");
        test1.add("d");
        test.addAll(test1);
        System.out.println(test);
    }

删:remove,removeAll

  public static void main(String[] args) {
        Collection<String> test = new ArrayList<>();
        Collections.addAll(test, "a","b","c","d");
        Collection<String> test1 = new ArrayList<>();
        test1.add("b");
        test1.add("e");
        test.remove("a");
        System.out.println(test);
        boolean flag2 = test.removeAll(test1);
        System.out.println(test);

clear

 public static void main(String[] args) {
        Collection<Integer> test = new ArrayList<>();
        Collections.addAll(test,1,2,3,4);
        System.out.println(test);
        test.clear();
        System.out.println(test);
    }

查:size, toArray , contains , containsAll , isEmpty

public static void main(String[] args) {
        System.out.println(test1());
//        System.out.println(test.size());
    }
    public static int  test1(){
        Collection<String> test = new ArrayList<>();
        test.add("a");
        test.add("b");
        Collections.addAll(test,"c","d","e");
        return test.size();
    }
    public static void main(String[] args) {
        Collection<Integer> test = new ArrayList<>();
        test.add(1);
        test.add(2);
        test.add(3);
        test.add(4);
        Collection<Integer> test1 = new ArrayList<>();
        test1.add(1);
        test1.add(2);
        System.out.println(test.containsAll(test1));
        System.out.println(test.isEmpty());
    }
2.3.2 Collection的数据遍历
2.3.2.1 for循环遍历
public static void main(String[] args) {
        Collection<Character> test = new ArrayList<>();
        test.add('君');
        test.add('不');
        Collections.addAll(test,'见','黄','河','之','水','天','上','来');
        System.out.println(test);
        Character[] arr =test.toArray(new Character[test.size()]);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
            
        }
2.3.2.2 增强for循环遍历
    public static void main(String[] args) {
        Collection<Character> test = new ArrayList<>();
        test.add('君');
        test.add('不');
        Collections.addAll(test,'见','黄','河','之','水','天','上','来');
        //(数据的类型 ,临时的变量名 : 集合的名称)
        for (Character character : test) {
            System.out.println(character);
        }
    }
2.3.2.3迭代器遍历
   public static void main(String[] args) {
        Collection<Character> test = new ArrayList<>();
        test.add('君');
        test.add('不');
        Collections.addAll(test,'见','黄','河','之','水','天','上','来');
        Iterator<Character> iterator = test.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }

2.3 List接口

List 是Collection的一个子接口,也无法直接进行实例化,需要它的子类ArrayList来进行实例化,也就是多态的形式。

  public static void main(String[] args) {
        List<String> list = new ArrayList<>();
    }
2.3.1 List下Collection没有的方法:

增:add(int index ,E element) // 将指定的元素添加到指定的下标中

​ addAll(int index , E element) // 将指定的集合添加到指定的下标中

public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add(1,"3");
        System.out.println(list);
        List<String> list1 = new ArrayList<>();
        list1.add("4");
        list1.add("5");
        list.addAll(2,list1);
    }

删 :E remove(int index) //通过下标进行删除,返回值是删除的那个数据。

List<String> list1 = new ArrayList<>();
        list1.add("4");
        list1.add("5");
        list.addAll(2,list1);
        System.out.println(list);
        //删除索引下标为2的元素
        String remove = list.remove(2);
        System.out.println(remove);
        System.out.println(list);

改:E set(int index, E e) //通过指定的索引修改数据元素,返回值是被修改的原数据

   //修改索引下标为1的元素,返回值为被修改的数据
        String li = list.set(1, "li");
        System.out.println(li);
        System.out.println(list);

查:E get(int index); //通过索引下标去获取指定元素

​ int indexOf(Object obj);//通过元素获取指定的元素下标

​ List subList(int formIndex, int toIndex); //截取一部分元素数据 form为开头, to为结尾。

 String li = list.set(1, "li");
        System.out.println(li);
        System.out.println(list);
        //通过下标获取元素数据
        System.out.println(list.get(2));
        //截取一部分数据元素
        System.out.println(list.subList(2, 4));
2.3.2 List的数据遍历
2.3.2.1 for循环遍历
 public static void main(String[] args) {
        List<Integer> arr = new ArrayList<>();
        arr.add(1);
        arr.add(2);
        arr.add(3);
        System.out.println(arr);
        for (int i = 0; i <arr.size() ; i++) {
            System.out.println(arr.get(i));
        }
    }
2.3.2.2 增强for循环遍历
 public static void main(String[] args) {
        List<Integer> arr = new ArrayList<>();
        arr.add(1);
        arr.add(2);
        arr.add(3);
        System.out.println(arr);
        for (Integer integer : arr) {
            System.out.println(integer);
        }
2.3.2.3 迭代器遍历
 public static void main(String[] args) {
        List<Integer> arr = new ArrayList<>();
        arr.add(1);
        arr.add(2);
        arr.add(3);
        System.out.println(arr);
        ListIterator<Integer> ill = arr.listIterator(arr.size());
        while (ill.hasNext()){
            System.out.println(ill.next());
        }
        while (ill.hasPrevious()){
            System.out.println(ill.previous());
        }

3.集合中存对象

class Person{
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
//重写toString方法
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
public class Demo8 {
    public static void main(String[] args) {
        Collection<Person> list = new ArrayList<>();
        list.add(new Person("张三",5));
        System.out.println(list);
        List<Person> list1 = new ArrayList<>();
        list1.addAll(list);
        list1.add(new Person("王五",6));
        list1.add(new Person("彩云",8));
        list1.add(1,new Person("李四",7));
        System.out.println(list1);

    }
}

2.4 Set集合

set集合是无序不可重复的,set集合下有HashSet和TreeSet两个实体类。

2.4.1 HashSet

HashSet存储对象数据时需要比较hash值和内容,因为时不可以重复的。

class Student{
    int id;
    String name;

    public Student(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
// 重写equals方法用于比较存储的对象的内容是否一样
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return id == student.id && Objects.equals(name, student.name);
    }
//重写hashCode方法,比较存储的对象的hash值。
    @Override
    public int hashCode() {
        return Objects.hash(id, name);
    }
}
public class Demo2 {
    public static void main(String[] args) {
        // 创建HashSet的一个集合用于存放对象
        Set<Student> stu = new HashSet<>();
        //往集合中添加数据,一个数据是一个对象
        stu.add(new Student(1,"小雨"));
        stu.add(new Student(2,"小雪"));
        stu.add(new Student(2,"小雪"));
        //遍历对象数据的内容
        for (Student student : stu) {
            System.out.println(student);
        }
//        System.out.println(stu);
    }
}

2.4.2 TreeSet

TreeSet集合在存储对象数据时需要实现一个Comparable接口,Comparable接口是可以进行比较的,TreeSet集合在添加数据时是要比较的因为底层是二叉树,会排序。

实例:

//创建的Dog类实现Comparable接口
class Dog implements Comparable<Dog>{
    String name;
    int age;
//创建一个有参的构造方法
    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }
//重写toString方法
    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
//重写Comparable下的抽象方法compareTo方法,返回值为正整数,0和负整数。
    @Override
    public int compareTo(Dog o) {
        int num = this.name.compareTo(o.name);
        if (num == 0){
            return this.age - o.age;
        } else {
            return num;
        }
    }
}
public class Demo4 {
    public static void main(String[] args) {
        //创建一个TreeSet集合
        Set<Dog> dogs = new TreeSet<>();
        //往集合中存放对象数据,一个数据是一个对象。
        dogs.add(new Dog("彩云",3));
        dogs.add(new Dog("大黄",4));
        dogs.add(new Dog("彩云",5));
        dogs.add(new Dog("小黑",2));
        System.out.println(dogs);
    }
}
2.4.3 比较器:

在TreeSet集合还可以添加一个比较器来比较内容存储,比较器为Comparator接口,需要创建一个新的类去实现这个接口。

//创建一个Animal类
class Animal {
    String name;
    int id;
    public Animal(String name, int id) {
        this.name = name;
        this.id = id;
    }

//    @Override
//    public int compareTo(Animal o) {
//        int arr = this.id - o.id;
//        return arr;
//    }

    @Override
    public String toString() {
        return "Animal{" +
                "name='" + name + '\'' +
                ", id=" + id +
                '}';
    }
}
//创建一个Com类去实现Comparator接口
class Com implements Comparator<Animal>{
//重写Comparator接口下的compare方法,返回值是正整数,0和负整数。
    @Override
    public int compare(Animal o1, Animal o2) {
        int arr = o1.id - o2.id;
        return arr;
    }
}
//}
public class Demo1 {
    public static void main(String[] args) {
        //创建新的对象数据
        Animal animal = new Animal("牛",2);
        Animal animal1 = new Animal("马",7);
        Animal animal2 = new Animal("羊",8);
        Animal animal3 = new Animal("老虎",3);
        Animal animal4 = new Animal("蛇",6);
        //新建一个TreeSet集合同时调用Com这个类中的重写的Comparator接口下的compare方法
        TreeSet<Animal> animals = new TreeSet<>(new Com());
        //往集合中添加对象数据
        animals.add(animal);
        animals.add(animal1);
        animals.add(animal2);
        animals.add(animal3);
        animals.add(animal4);
        System.out.println(animals);



    }
}

2.5 Map集合

Map集合的语法格式:

HashMap<Integer, String> obj = new HashMap<>();

Map集合中存储的数据是一个个键值对的形式,在Map集合中键是不可重复的,而值是可以的。

Map集合中的方法:

增:put, putAll

public class Demo3 {
    public static void main(String[] args) {
        //创建一个新的HashMap集合,键为int类型数据,值为String类型数据
        HashMap<Integer, String> obj = new HashMap<>();
        //往集合中添加数据
        obj.put(1,"奔跑");
        obj.put(2,"祈祷");
        HashMap<Integer, String> obj1 = new HashMap<>();
        obj1.put(3,"愿望");
        obj1.put(4,"思考");
        obj1.put(5,"倒影");
        obj1.put(6,"奔跑");
        System.out.println(obj);
        //通过键位取值。
        System.out.println(obj.get(1));
        // 将一个HashMap集合的添加到另一个中,但是键值不能一样
        obj1.putAll(obj);
    }
}

删:remove

        //remove()根据键删除数据,返回值是值
        obj1.remove(1);

查:

        //size()查看集合中的数据数量
        obj.size();
        //isEmpty() 判断集合是否为空
        obj.isEmpty();
        //通过键位取值。
        System.out.println(obj.get(1));
        //将Map集合中的键取出放进set集合中
        Set<Integer> integers = obj.keySet();
        System.out.println(integers);
        //将Map集合中的值取出放进Collection集合中
        Collection<String> values = obj.values();
        System.out.println(values);
        //将Map集合中的键和值都放进set集合中
        Set<Map.Entry<Integer, String>> entries = obj.entrySet();
        System.out.println(entries);

4. file类

主要用于文件和目录的创建、查找和删除等操作

构造方法

  • public File(String pathname) :通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例。
  • public File(String parent, String child) :从父路径名字符串和子路径名字符串创建新的 File实例。
  • public File(File parent, String child) :从父抽象路径名和子路径名字符串创建新的 File实例。

成员方法

大多常用的方法都在示例中存在

package com.qfedu.Test;

import java.io.File;
import java.io.IOException;

public class Demo03 {
    public static void main(String[] args) throws IOException {
        //我在我的电脑D盘下面有这个aaa文件夹,并且该文件夹下面还有文件夹和文件
        File file = new File("D:"+ File.separator + "aaa");
        //判断该文件是否存在
        System.out.println(file.exists());  //true
        //获取当前文件的绝对路径
        System.out.println(file.getAbsolutePath());  //D:\aaa
        //获取当前文件的名字
        System.out.println(file.getName());  //aaa
        //将此File转换为路径名字符串。
        System.out.println(file.getPath());  //D:\aaa
        //判断当前文件是否是文件夹
        System.out.println(file.isDirectory());  //true
        //判断当前文件是否是文件
        System.out.println(file.isFile());  //false
        //返回此抽象路径名表示的文件上次修改的时间。
        System.out.println(file.lastModified());  //1659697522993,时间戳从1970.1.1,00:00:00
        //返回一个字符串数组,命名由此抽象路径名表示的目录中的文件和目录。
        String[] list = file.list();
        for (String s : list) {
            System.out.println(s);
            //3.txt
            //bbb
            //ccc
        }
        //返回一个抽象路径名数组,表示由该抽象路径名表示的目录中的文件。
        File[] files = file.listFiles();
        for (File file1 : files) {
            System.out.println(file1);
            //D:\aaa\3.txt
            //D:\aaa\bbb
            //D:\aaa\ccc

            //删除由此抽象路径名表示的文件或目录。 返回值是布尔类型的值
            //file.delete();

            //如果路径中没有文件,会创建一个新的文件,需要throws IOException
            //file.createNewFile();

            //测试此抽象路径名命名的文件是否为隐藏文件。
            System.out.println(file.isHidden()); //false

            //创建由此抽象路径名命名的目录。 返回值是布尔类型的值
            //file.mkdir();

            //创建由此抽象路径名命名的目录,包括任何必需但不存在的父目录。返回值是布尔类型的值
            //file.mkdirs();
        }
    }
}

5.递归

递归:我自己调用我自己,即当前方法调用本身

分类

  1. 直接递归:自身调用自己
  2. 间接递归:方法1调用方法2,方法2调用方法3,方法3调用方法1

注意

递归一定要有条件,能够使用递归停下来,不然会内存移除

案例:找到指定文件夹下的所有文件

package com.qfedu.Test;

import java.io.File;


public class Demo04 {
    public static void main(String[] args) {
        File file = new File("D:" + File.separator + "aaa");
        recursion(file);
    }

    public static void recursion(File file) {
        /**
         * 判断如果是文件夹,我们就遍历其所有文件,
         * 将这些文件再次调用recursion(File file),知道找到所有文件进行输出
         */
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File file1 : files) {
                recursion(file1);
            }
        } else {
            System.out.println(file.getName());
        }
        /**
         * 控制台输出:
         * 3.txt
         * 1.txt
         * 2.txt
         */
    }
}

ava.io.File;

public class Demo04 {
public static void main(String[] args) {
File file = new File(“D:” + File.separator + “aaa”);
recursion(file);
}

public static void recursion(File file) {
    /**
     * 判断如果是文件夹,我们就遍历其所有文件,
     * 将这些文件再次调用recursion(File file),知道找到所有文件进行输出
     */
    if (file.isDirectory()) {
        File[] files = file.listFiles();
        for (File file1 : files) {
            recursion(file1);
        }
    } else {
        System.out.println(file.getName());
    }
    /**
     * 控制台输出:
     * 3.txt
     * 1.txt
     * 2.txt
     */
}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值