java的第十五天

01、可变参数

目标:学习可变参数的使用

讲解:

求2个int的和,求3个int的和

​ 参数的类型都是相同的,但是个数不一样的,写很多重载方法也很麻烦,可以使用可变参数.

什么是可变参数: 是JDK1.5的新特性

​ 参数类型相同,参数个数任意

可变参数格式(重点):

​ 修饰符 返回值类型 方法名(数据类型… 变量名) {

​ }

可变参数的好处:

​ 一个可变参数的方法,可以传入任意多个参数,就不同写多个重载方法

小结:
1.可变参数格式?

​ 修饰符 返回值类型 方法名(数据类型… 变量名) {
​ }

​ 和普通方法相比,再数据类型后面添加…即可

2.可变参数的好处?

​ 一个可变参数的方法,可以传入任意多个参数,就不同写多个重载方法

3.说出可变参数的2个注意事项

​ 1.一个方法只能有一个可变参数
​ 2.可变参数需要放在参数列表的最后面

   public class Demo011 {
    public static void main(String[] args) {
        // add();
        int x = add(1, 2, 3, 4, 5, 6);
        System.out.println(x);
    }

    // 定义一个可变参数的方法
    public static int add(int... a) {
        // System.out.println(a); // [I@1c53fd30
        // 遍历数组得到所有的参数
        int sum = 0;
        for (int i = 0; i < a.length; i++) {
            sum += a[i];
        }

        return sum;
    }

/*    // 求2个int的和
    public static int add(int a, int b) {
        return a + b;
    }

    // 求3个int的和
    public static int add(int a, int b, int c) {
        return a + b + c;
    }
    // 如果需要求5个,6个,任意个*/
}

02、Collections常用功能

目标:学习Collections常用功能

讲解:
Collections是集合工具类,里面提供了很多操作集合的方法.
这个类没有看到构造方法,里面的方法全是静态方法,我们直接使用类名调用

Collections类的方法:

​ static boolean addAll(Collection<?> c, T… elements) 将后面的数据添加到前面的集合中
​ static void shuffle(List<?> list) 随机交换集合中的元素
​ static void sort(List list) 根据元素的自然顺序升序排

课堂代码:
public class Demo02 {
    public static void main(String[] args) {
        // test01();
        // test02();
        test03();
    }

    // static void sort​(List<T> list) 根据元素的自然顺序升序排
    public static void test03() {
        ArrayList<Integer> list = new ArrayList<>();
        Collections.addAll(list, 8, 5, 9, 1, 3, 2, 7);

        // 给集合中的元素排序
        Collections.sort(list);
        System.out.println("排序后:" + list); // 排序后:[1, 2, 3, 5, 7, 8, 9]

        ArrayList<String> list2 = new ArrayList<>();
        Collections.addAll(list2, "a", "bb", "ba", "A", "bc");

        Collections.sort(list2);
        //         按照字母的ASCII码值来排序,如果第一个相同,再比较第二个字母
        System.out.println("排序后: " + list2); // 排序后: [A, a, ba, bb, bc]
    }

    // static void shuffle (List<?> list) 随机交换集合中的元素
    public static void test02() {
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "aa", "bb", "cc", "dd");
        System.out.println(list);

        Collections.shuffle(list);
        System.out.println("交换后: " + list);
    }

    // static <T> boolean addAll (Collection<?> c, T... elements) 将后面的数据添加到前面的集合中
    public static void test01() {
        ArrayList<String> list = new ArrayList<>();

        Collections.addAll(list, "aa", "bb", "cc");

        System.out.println(list);
    }
}

03、Comparator比较器

目标:学习Comparator比较器的使用

讲解:

Collections类中

​ static void sort(List list, Comparator<? super T> c) 根据指定的排序规则来对集合中的数据进行排序
​ Comparator是一个接口

小结:
1.shuffle(List<?> list)方法作用?

​ 将集合中的元素随机打乱

2.void sort(List list)方法作用?

​ 按照元素的自然顺序排序(数字从小到大,字母按照ASCII码值)

3.int compare(Xxx o1, Xxx o2)方法如何升序如何降序?

​ return 前面 - 后面是升序
​ return 后面 - 前面是降序

课堂代码:
public class Student {
    private String name;
    private int age;
    private double socre;
	public Student() {
	}

public Student(String name, int age, double socre) {
    this.name = name;
    this.age = age;
    this.socre = socre;
}

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;
}

public double getSocre() {
    return socre;
}

public void setSocre(double socre) {
    this.socre = socre;
}

@Override
public String toString() {
    return "Student{" +
            "name='" + name + '\'' +
            ", age=" + age +
            ", socre=" + socre +
            '}';
}
}


import java.util.Comparator;

public class StudentComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        // 比较学生年龄: o1 - o2升序
        // return o1.getAge() - o2.getAge();
        // 比较学生年龄: o2 - o1降序
        // return o2.getAge() - o1.getAge();

        // 按照学生成绩降序
        return (int)(o2.getSocre() - o1.getSocre());
    }
}


public class Demo03 {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        Student s1 = new Student("赤木晴子", 16, 99);
        Student s2 = new Student("赤木刚宪", 19, 100);
        Student s3 = new Student("宫城良田", 20, 59);

        Collections.addAll(list, s1, s2, s3);

        // static <T> void sort​(List<T> list, Comparator<? super T> c) 根据指定的排序规则来对集合中的数据进行排序
        // Comparator是一个接口,方法参数要接口,我们传入接口的实现类对象
        // StudentComparator sc = new StudentComparator();
        // Collections.sort(list, sc);

        // 方法参数要接口,我们传入匿名内部类
        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                // // 按照学生成绩升序
                return (int)(o1.getSocre() - o2.getSocre());
            }
        });

        for (Student student : list) {
            System.out.println(student);
        }

    }
}

04、冒泡排序

目标:学习冒泡排序

冒泡排序的原理:

​ 相邻元素比较,大的往后放

分析:
第一轮:

​ j j+1
​ arr[0] 和 arr[1]
​ arr[1] 和 arr[2]
​ arr[2] 和 arr[3]

第二轮:

​ arr[0] 和 arr[1]
​ arr[1] 和 arr[2]

第三轮:

​ arr[0] 和 arr[1]

小结:
冒泡排序的原理?

​ 相邻元素比较,大的往后放

课堂代码:
public class Demo04 {
    public static void main(String[] args) {
        int[] arr = {5, 2, 3, 1};

        // 外循环控制比较轮数
        for (int i = 0; i < arr.length - 1; i++) { // i = 0,1,2
            System.out.println("轮数");

            // 内循环控制每轮的比较次数, j小于几就比较几次
            for (int j = 0; j < arr.length - 1 - i; j++) {
                // System.out.println("\t" + j + "和" + (j+1) + "比较");
                // 相邻元素比较,大的往后放
                if (arr[j] > arr[j+1]) {
                    // 前面大,后面小,交换位置
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }

        System.out.println("排序后:" + Arrays.toString(arr));
    }

05、Map接口介绍

目标:学习Map集合特点

讲解:
将键映射到值的对象。 Map不能包含重复的键; 每个键可以映射到最多一个值。

Map集合特点:

​ 1.保存键和值
​ 2.键不能重复
​ 3.一个键对应一个值

有对应关系的就考虑使用Map来存储

06、Map常用方法

目标:学习Map常用方法

讲解:
Map是一个集合,常用的方法就是增删改查

Map常用方法:

​ V put(K key, V value) 当键不重复是添加键值对,返回null,当键存在是修改,返回被修改的数据
​ V get(Object key) 通过键获取值
​ V remove(Object key) 通过键删除这个键值对
​ int size() 获取map的键值对的个数

Map的实现类HashMap:

​ HashMap底层是哈希表

小结:
1.Map常用方法有哪些?

​ put: 添加/修改
​ get: 通过键获取值
​ remove: 通过键删除这个键值对
​ size: 获取个数

课堂代码:
public class Demo06 {
    public static void main(String[] args) {
        HashMap<String, String> map = new HashMap<>();

        // V put (K key, V value) 当键不重复是添加键值对,返回null,当键存在是修改,返回被修改的数据
        map.put("邓超", "孙俪");
        map.put("黄晓明", "杨颖");
        System.out.println(map.put("谢霆锋", "张柏芝")); // null
        map.put("老干爹", "老干妈");

        System.out.println(map.put("谢霆锋", "王菲")); // 张柏芝

        System.out.println(map);

        // V get (Object key) 通过键获取值
        System.out.println(map.get("老干爹")); // 老干妈

        // V remove (Object key) 通过键删除这个键值对
        map.remove("老干爹");
        System.out.println("删除后: " + map);

        // int size () 获取map的键值对的个数
        System.out.println(map.size());
    }
}

07、Map遍历1_键找值方式

目标:学习Map遍历键找值方式

讲解:

什么是Map遍历?

​ Map遍历就是依次取出每个键和值(一次获取一对)

​ Set keySet(): 返回map中所有的键,存放到Set集合中

小结:
Map遍历键找值方式步骤

​ 1.获取所有的键
​ 2.遍历获取每个键
​ 3.通过键找值

3步走
课堂代码:
    public class Demo07 {
    public static void main(String[] args) {
        HashMap<String, String> map = new HashMap<>();
        map.put("邓超", "孙俪");
        map.put("黄晓明", "杨颖");
        map.put("谢霆锋", "张柏芝");
        map.put("老干爹", "老干妈");

        /*String k1 = "邓超";
        String v1 = map.get(k1);
        System.out.println(k1 + "::" + v1);

        String k2 = "黄晓明";
        String v2 = map.get(k2);
        System.out.println(k2 + "::" + v2);

        String k3 = "谢霆锋";
        String v3 = map.get(k3);
        System.out.println(k3 + "::" + v3);

        String k4 = "老干爹";
        String v4 = map.get(k4);
        System.out.println(k4 + "::" + v4);*/
        // 以上代码是重复代码,唯独键再变化,所以我们改成循环
        // 1.获取所有的键
        Set<String> keySet = map.keySet();
        // 2.遍历获取每个键
        for (String key : keySet) {
            // 3.通过键找值
            String value = map.get(key); // 核心,通过键找到值
            System.out.println(key + ":::" + value);
        }
    }
}

08、Map遍历2_Entry键值对关系

目标:学习Map遍历Entry方式

讲解:

什么是Entry:

​ 是一个接口,表示键值对对象,里面会保存键和值,Entry相当于结婚证

如何获取Entry:

​ 没有获取一个Entry的方法,只能获取所有Entry
​ Set<Entry<K,V>> entrySet(): 获取所有的Entry

小结:
Map遍历Entry方式的步骤

​ 1.得到所有的Entry
​ 2.遍历得到每个Entry
​ 3.通过Entry得到键和值

3步走

​ Entry方式效率高一点,因为Map底层就是用Entry存储数据的

课堂代码:
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Demo08 {
    public static void main(String[] args) {
        HashMap<String, String> map = new HashMap<>();
        map.put("邓超", "孙俪");
        map.put("黄晓明", "杨颖");
        map.put("谢霆锋", "张柏芝");
        map.put("老干爹", "老干妈");

        // 1.得到所有的Entry
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        // 2.遍历得到每个Entry
        for (Map.Entry<String, String> entry : entrySet) {
            // 3.通过Entry得到键和值
            System.out.println(entry.getKey() + "==" + entry.getValue());
        }
    }
}

09、HashMap存储自定义类型

目标:学习HashMap存储自定义类型

练习:

​ 每位学生(姓名,年龄)都有自己的家庭住址。那么,既然有对应关系,则将学生对象和家庭住址存储到map集合中。学生作为键, 家庭住址作为值。

注意:

​ 学生姓名相同并且年龄相同视为同一名学生。

分析:

​ 1.定义学生类
​ 2.创建一个Map用于存放学生
​ 3.创建4个学生存储到Map中
​ 4.遍历map

小结:
HashMap存储自定义类型键需要怎么做?

​ 因为键不能够重复,需要重写键的hashCode和equals方法

课堂代码:
// 1.定义学生类
public class Student {
    private String name;
    private int age;

    public Student() {
    }

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

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        if (age != student.age) return false;
        return name != null ? name.equals(student.name) : student.name == null;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + age;
        return result;
    }

    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;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Demo09 {
    public static void main(String[] args) {
        // 1.定义学生类
        // 2.创建一个Map用于存放学生
        HashMap<Student, String> map = new HashMap<>();

        // 3.创建4个学生存储到Map中
        Student s1 = new Student("流川枫", 18);
        Student s2 = new Student("樱木花道", 17);
        Student s3 = new Student("赤木晴子", 16);
        Student s4 = new Student("三井寿", 19);
        Student s5 = new Student("三井寿", 19);

        map.put(s1, "北海道");
        map.put(s2, "东京");
        map.put(s3, "大版");
        map.put(s4, "名古屋");
        map.put(s5, "广岛");

        // 4.遍历map
        // 1.键找值方式
        Set<Student> keySet = map.keySet();
        for (Student key : keySet) {
            String value = map.get(key);
            System.out.println(key + " == " + value);
        }

        System.out.println("-------------");
        // 2.Entry方式
        Set<Map.Entry<Student, String>> entrySet = map.entrySet();
        for (Map.Entry<Student, String> entry : entrySet) {
            System.out.println(entry.getKey() + "::" + entry.getValue());
        }
    }
}

10、Map集合练习

目标:通过练习加强对Map使用

需求:

​ 计算一个字符串中每个字符出现次数。
​ “bzcbzaabAaacb”
​ ↑
​ a=4
​ A=1
​ b=4
​ c=2
​ z=2

​ map = {b=3, z=1, c=1};

分析:

​ 一个字母会对应一个次数,有对应关系就使用Map集合来存储,字母作为键,次数作为值

实现步骤:

​ 1.定义一个字符串
​ 2.创建一个Map,字母作为键,次数作为值
​ 3.遍历字符串,得到每个字母
​ 4.如果map中没有这个字母,次数设置为1次
​ 5.如果map中有这个字母,次数+1
​ 6.遍历map集合

小结:

​ 当有对应关系的时候就使用map

​ boolean containsKey(String key) 判断map中是否包含指定的键,如果包含返回true

课堂代码:
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Demo10 {
    public static void main(String[] args) {
        // 1.定义一个字符串
        String str = "bzcbzaabAaacb";
        // 2.创建一个Map,字母作为键,次数作为值
        HashMap<Character, Integer> map = new HashMap<>();

        // 3.遍历字符串,得到每个字母
        for (int i = 0; i < str.length(); i++) {
            // 通过索引得到字符
            char c = str.charAt(i);

            // Map方法: boolean containsKey​(Object key) 判断map是否有这个键
            if (map.containsKey(c)) {
                // 5.如果map中有这个字母,次数+1
                // 得到之前的次数
                int cout = map.get(c);
                // 次数+1
                map.put(c, cout + 1);
            } else {
                // 4.如果map中没有这个字母,次数设置为1次
                map.put(c, 1);
            }
        }

        // 6.遍历map集合
        // Entry方式
        Set<Map.Entry<Character, Integer>> entrySet = map.entrySet();
        for (Map.Entry<Character, Integer> entry : entrySet) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}


import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/*
肠胃消化 -> https://list.jd.com/list.html?cat=9192,12632,12641

情趣内衣 -> https://list.jd.com/list.html?cat=9192,9196,1506

人参 -> https://list.jd.com/list.html?cat=9192,9195,9230

小结:
    有对应关系的,就考虑使用map来保存
 */
public class Demo11 {
    public static void main(String[] args) {
        HashMap<String, String> map = new HashMap<>();
        map.put("肠胃消化", "https://list.jd.com/list.html?cat=9192,12632,12641");
        map.put("情趣内衣", "https://list.jd.com/list.html?cat=9192,9196,1506");
        map.put("人参", "https://list.jd.com/list.html?cat=9192,9195,9230");

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

11、图书管理系统_演示

图书管理系统功能的步骤:
1.欢迎和功能选择界面

​ 1.输出欢迎语句
​ 2.输出功能选项
​ 3.让用户输入一个选项
​ 4.对用户的输入进行匹配

2.初始化书籍

​ 1.定义书籍类Book
​ 2.定义一个ArrayList保存2本名著
​ 3.定义一个ArrayList保存2本it书籍
​ 4.定义个Map,键是书籍类型,值是对应的书籍
​ 5.将书籍类型和对应的书籍存储到Map中

3.查看书籍

​ 1.遍历Map集合,拿到键和值
​ 2.再遍历ArrayList拿到所有书籍

4.添加书籍

​ 1.让用户输入要添加的书籍的类型,书名,价格
​ 2.根据书名和价格创建一个Book
​ 3.根据用户输入的书籍类型找到对应的集合
​ 4.将新的书添加到这个集合即可

5.删除书籍

​ 1.让用户输入要删除的书籍的类型数书名
​ 2.在map中根据书籍类型找到对应的书的集合
​ 3.遍历ArrayList找到这本书
​ 4.删除这本书

6.修改书籍

​ 1.让用户输入修改的数据的类型,老的书名,新的书名,新的价格
​ 2.根据输入的类型去Map中找到这个类型的集合
​ 3.遍历集合,判断是否是要修改的书籍
​ 4.如果是,就设置新的书名和价格

7.退出
课堂代码:
// 1.定义书籍类Book
public class Book {
    private String name;
    private double price;

    public Book() {
    }

    public Book(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

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

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}
import java.util.*;
import java.util.Map.Entry;
/*
图书管理系统功能

1.欢迎和功能选择界面
    1.输出欢迎语句
    2.输出功能选项
    3.让用户输入一个选项
    4.对用户的输入进行匹配

2.初始化书籍
    1.定义书籍类Book
    2.定义一个ArrayList保存2本名著
    3.定义一个ArrayList保存2本it书籍
    4.定义个Map,键是书籍类型,值是对应的书籍
    5.将书籍类型和对应的书籍存储到Map中

3.查看书籍
    1.遍历Map集合,拿到键和值
    2.再遍历ArrayList拿到所有书籍

4.添加书籍
    1.让用户输入要添加的书籍的类型,书名,价格
    2.根据书名和价格创建一个Book
    3.根据用户输入的书籍类型找到对应的集合
    4.将新的书添加到这个集合即可

5.删除书籍
    1.让用户输入要删除的书籍的类型数书名
    2.在map中根据书籍类型找到对应的书的集合
    3.遍历ArrayList找到这本书
    4.删除这本书

6.修改书籍
    1.让用户输入修改的数据的类型,老的书名,新的书名,新的价格
    2.根据输入的类型去Map中找到这个类型的集合
    3.遍历集合,判断是否是要修改的书籍
    4.如果是,就设置新的书名和价格

7.退出

 */
public class BookManager {
    public static void main(String[] args) {
        // 初始化书籍
        // 2.定义一个ArrayList保存2本名著
        ArrayList<Book> mz = new ArrayList<>();
        mz.add(new Book("西游记", 19));
        mz.add(new Book("水浒传", 29));

        // 3.定义一个ArrayList保存2本it书籍
        ArrayList<Book> it = new ArrayList<>();
        it.add(new Book("Java入门到精通", 99));
        it.add(new Book("PHP入门到精通", 9.9));

        // 4.定义个Map,键是书籍类型,值是对应的书籍
        Map<String, ArrayList<Book>> allBooks = new HashMap<>();
        // 5.将书籍类型和对应的书籍存储到Map中
        allBooks.put("名著", mz);
        allBooks.put("it书籍", it);

        a:while (true) {
            /*
            1.欢迎和功能选择界面
             */
            // 1.输出欢迎语句
            System.out.println("--------欢迎来到图书管理系统--------");
            // 2.输出功能选项
            System.out.println("1.查看书籍");
            System.out.println("2.添加书籍");
            System.out.println("3.删除书籍");
            System.out.println("4.修改书籍");
            System.out.println("5.退出");
            System.out.println("请输入你的选择:");
            // 3.让用户输入一个选项
            Scanner sc = new Scanner(System.in);
            int operation = sc.nextInt();
            // 4.对用户的输入进行匹配
            switch (operation) {
                case 1:
                    showAllBooks(allBooks);
                    break;
                case 2:
                    addBook(allBooks);
                    break;
                case 3:
                    removeBook(allBooks);
                    break;
                case 4:
                    editBook(allBooks);
                    break;
                case 5:
                    System.out.println("大爷,走好,欢迎下次再来.");
                    break a;
                default:
                    System.out.println("大爷,没有这样的操作...");
                    break;
            }
        }
    }

    /*
    修改书籍
     */
    private static void editBook(Map<String, ArrayList<Book>> allBooks) {
        // 1.让用户输入修改的数据的类型,老的书名,新的书名,新的价格
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改书籍的类型:");
        String type = sc.next();
        System.out.println("请输入要修改书籍的书名:");
        String oldName = sc.next();
        System.out.println("请输入新的书名:");
        String newName = sc.next();
        System.out.println("请输入新的价格:");
        double price = sc.nextDouble();

        // 2.根据输入的类型去Map中找到这个类型的集合
        ArrayList<Book> books = allBooks.get(type);
        if (books == null) {
            System.out.println("没有这个类型的书籍");
            return;
        }

        // 3.遍历集合,判断是否是要修改的书籍
        for (Book book : books) {
            if (book.getName().equals(oldName)) {
                // 4.如果是要修改的书籍,就设置新的书名和价格
                book.setName(newName);
                book.setPrice(price);
                System.out.println(oldName + "修改成功");
                return;
            }
        }

        System.out.println("没有找到" + oldName + "这本书");
    }

    /*
    删除书籍
     */
    private static void removeBook(Map<String, ArrayList<Book>> allBooks) {
        // 1.让用户输入要删除的书籍的类型数书名
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除书籍的类型:");
        String type = sc.next();
        System.out.println("请输入要删除的书名:");
        String name = sc.next();

        // 2.在map中根据书籍类型找到对应的书的集合
        ArrayList<Book> books = allBooks.get(type);
        if (books == null) {
            // 没有这种类型的书籍,不用删除,不需要往下走,结束这个方法
            System.out.println("没有这种类型的书籍");
            return;
        }

        for (int i = 0; i < books.size(); i++) {
            // 3.遍历ArrayList找到这本书
            Book book = books.get(i);
            if (book.getName().equals(name)) {
                // 4.删除这本书
                books.remove(i);
                System.out.println("删除" + name + "书籍成功");
                return;
            }
        }

        System.out.println(name + "这本书不存在");
    }

    /*
    4.添加书籍
     */
    private static void addBook(Map<String, ArrayList<Book>> allBooks) {
        // 1.让用户输入要添加的书籍的类型,书名,价格
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要添加书籍的类型:");
        String type = sc.next();
        System.out.println("请输入要添加的书名:");
        String name = sc.next();
        System.out.println("请输入要添加书的价格:");
        double price = sc.nextDouble();
        // 2.根据书名和价格创建一个Book
        Book newBook = new Book(name, price);

        // 3.根据用户输入的书籍类型找到对应的集合
        ArrayList<Book> books = allBooks.get(type);
        if (books == null) {
            // 目前没有这种类型
            books = new ArrayList<>();
            allBooks.put(type, books);
        }

        // 做标记(默认书是不存在的)
        boolean exists = false;
        // 判断书是否存在(要比较完所有的书才能确定是否存在)
        for (Book book : books) {
            if (book.getName().equals(name)) {
                // 书存在
                exists = true; // 当书存在,将标记改成true
            }
        }

        // for循环完成根据标记来确定书是否存在
        if (exists) {
            // 如果这本书存在,不添加
            System.out.println(name + "这本书存在");
        } else {
            // 如果这本书不存在

            // 4.将新的书添加到这个集合即可
            books.add(newBook);
            System.out.println("添加" + name + "成功");
        }
    }

    // 查看所有书籍,需要书籍的map
    private static void showAllBooks(Map<String, ArrayList<Book>> books) {
        System.out.println("类型\t\t书名\t价格");
        // 1.遍历Map集合,拿到键和值
        Set<Entry<String, ArrayList<Book>>> entrySet = books.entrySet();
        for (Entry<String, ArrayList<Book>> entry : entrySet) {
            String key = entry.getKey();
            System.out.println(key);

            ArrayList<Book> list = entry.getValue();
            // 遍历ArrayList
            // 2.再遍历ArrayList拿到所有书籍
            for (Book book : list) {
                System.out.println("\t\t\t" + book.getName() + "\t" + book.getPrice());
            }
        }
    }
}



import java.util.*;
import java.util.Map.Entry;
public class BookManager {
    public static void main(String[] args) {
        // 初始化书籍
        // 2.定义一个ArrayList保存2本名著
        ArrayList<Book> mz = new ArrayList<>();
        mz.add(new Book("西游记", 19));
        mz.add(new Book("水浒传", 29));

        // 3.定义一个ArrayList保存2本it书籍
        ArrayList<Book> it = new ArrayList<>();
        it.add(new Book("Java入门到精通", 99));
        it.add(new Book("PHP入门到精通", 9.9));

        // 4.定义个Map,键是书籍类型,值是对应的书籍
        Map<String, ArrayList<Book>> allBooks = new HashMap<>();
        // 5.将书籍类型和对应的书籍存储到Map中
        allBooks.put("名著", mz);
        allBooks.put("it书籍", it);

        a:while (true) {
            /*
            1.欢迎和功能选择界面
             */
            // 1.输出欢迎语句
            System.out.println("--------欢迎来到图书管理系统--------");
            // 2.输出功能选项
            System.out.println("1.查看书籍");
            System.out.println("2.添加书籍");
            System.out.println("3.删除书籍");
            System.out.println("4.修改书籍");
            System.out.println("5.退出");
            System.out.println("请输入你的选择:");
            // 3.让用户输入一个选项
            Scanner sc = new Scanner(System.in);
            int operation = sc.nextInt();
            // 4.对用户的输入进行匹配
            switch (operation) {
                case 1:
                    showAllBooks(allBooks);
                    break;
                case 2:
                    addBook(allBooks);
                    break;
                case 3:
                    removeBook(allBooks);
                    break;
                case 4:
                    editBook(allBooks);
                    break;
                case 5:
                    System.out.println("大爷,走好,欢迎下次再来.");
                    break a;
                default:
                    System.out.println("大爷,没有这样的操作...");
                    break;
            }
        }
    }

    /*
    修改书籍
     */
    private static void editBook(Map<String, ArrayList<Book>> allBooks) {
        // 1.让用户输入修改的数据的类型,老的书名,新的书名,新的价格
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改书籍的类型:");
        String type = sc.next();
        System.out.println("请输入要修改书籍的书名:");
        String oldName = sc.next();
        System.out.println("请输入新的书名:");
        String newName = sc.next();
        System.out.println("请输入新的价格:");
        double price = sc.nextDouble();

        // 2.根据输入的类型去Map中找到这个类型的集合
        ArrayList<Book> books = allBooks.get(type);
        if (books == null) {
            System.out.println("没有这个类型的书籍");
            return;
        }

        // 3.遍历集合,判断是否是要修改的书籍
        for (Book book : books) {
            if (book.getName().equals(oldName)) {
                // 4.如果是要修改的书籍,就设置新的书名和价格
                book.setName(newName);
                book.setPrice(price);
                System.out.println(oldName + "修改成功");
                return;
            }
        }

        System.out.println("没有找到" + oldName + "这本书");
    }

    /*
    删除书籍
     */
    private static void removeBook(Map<String, ArrayList<Book>> allBooks) {
        // 1.让用户输入要删除的书籍的类型数书名
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除书籍的类型:");
        String type = sc.next();
        System.out.println("请输入要删除的书名:");
        String name = sc.next();

        // 2.在map中根据书籍类型找到对应的书的集合
        ArrayList<Book> books = allBooks.get(type);
        if (books == null) {
            // 没有这种类型的书籍,不用删除,不需要往下走,结束这个方法
            System.out.println("没有这种类型的书籍");
            return;
        }

        for (int i = 0; i < books.size(); i++) {
            // 3.遍历ArrayList找到这本书
            Book book = books.get(i);
            if (book.getName().equals(name)) {
                // 4.删除这本书
                books.remove(i);
                System.out.println("删除" + name + "书籍成功");
                return;
            }
        }

        System.out.println(name + "这本书不存在");
    }

    /*
    4.添加书籍
     */
    private static void addBook(Map<String, ArrayList<Book>> allBooks) {
        // 1.让用户输入要添加的书籍的类型,书名,价格
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要添加书籍的类型:");
        String type = sc.next();
        System.out.println("请输入要添加的书名:");
        String name = sc.next();
        System.out.println("请输入要添加书的价格:");
        double price = sc.nextDouble();
        // 2.根据书名和价格创建一个Book
        Book newBook = new Book(name, price);

        // 3.根据用户输入的书籍类型找到对应的集合
        ArrayList<Book> books = allBooks.get(type);
        if (books == null) {
            // 目前没有这种类型
            books = new ArrayList<>();
            allBooks.put(type, books);
        }

        // 做标记(默认书是不存在的)
        boolean exists = false;
        // 判断书是否存在(要比较完所有的书才能确定是否存在)
        for (Book book : books) {
            if (book.getName().equals(name)) {
                // 书存在
                exists = true; // 当书存在,将标记改成true
            }
        }

        // for循环完成根据标记来确定书是否存在
        if (exists) {
            // 如果这本书存在,不添加
            System.out.println(name + "这本书存在");
        } else {
            // 如果这本书不存在

            // 4.将新的书添加到这个集合即可
            books.add(newBook);
            System.out.println("添加" + name + "成功");
        }
    }

    // 查看所有书籍,需要书籍的map
    private static void showAllBooks(Map<String, ArrayList<Book>> books) {
        System.out.println("类型\t\t书名\t价格");
        // 1.遍历Map集合,拿到键和值
        Set<Entry<String, ArrayList<Book>>> entrySet = books.entrySet();
        for (Entry<String, ArrayList<Book>> entry : entrySet) {
            String key = entry.getKey();
            System.out.println(key);

            ArrayList<Book> list = entry.getValue();
            // 遍历ArrayList
            // 2.再遍历ArrayList拿到所有书籍
            for (Book book : list) {
                System.out.println("\t\t\t" + book.getName() + "\t" + book.getPrice());
            }
        }
    }
}

12、图书管理系统

功能分析:

​ 1.功能选择界面
​ 2.初始化书籍
​ 3.查看书籍
​ 4.添加书籍
​ 5.删除书籍
​ 6.修改书籍

目标:实现 1.功能选择界面

​ 打印输出相应字符串即可
​ 使用键盘输入
​ 根据用户的输入进行选择

目标:实现 2.初始化书籍

​ 1.定义Book书籍类
​ 2.创建一个名著集合,存储两本名著
​ 3.创建一个it书籍集合,存储两本it书籍
​ 4.创建一个Map,键是书籍类型,值就是对应的书籍集合
​ 5.添加对应的数据到Map集合中

目标:实现 3.查看书籍

​ 遍历map集合

目标:实现 4.添加书籍

​ 输入要添加书籍的类型
​ 输入要添加的书名
​ 输入要添加书的价格
​ 创建Book对象
​ 添加到对应的ArrayList集合中

目标: 实现 5.删除书籍

​ 输入要删除书籍的类型
​ 输入要删除的书名
​ 通过类型找到书籍的集合
​ 从集合中删除这本数

目标: 实现 6.修改书籍

​ 输入要修改书籍的类型
​ 输入要修改书籍的书名
​ 输入新的书名
​ 输入新的价格
​ 通过类型找到书籍的集合
​ 遍历书籍集合找到要修改的书籍
​ 修改书籍

课堂代码:
// 1.定义Book书籍类
public class Book {
    private String name;
    private double price;

    public Book() {
    }

    public Book(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

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

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}


import java.util.*;
public class Demo12 {
    public static void main(String[] args) {
        // 2.初始化书籍
        // 1.定义Book书籍类
        // 2.创建一个名著集合,存储两本名著
        ArrayList<Book> mz = new ArrayList<>();
        mz.add(new Book("西游记", 19));
        mz.add(new Book("水浒传", 29));

        // 3.创建一个it书籍集合,存储两本it书籍
        ArrayList<Book> it = new ArrayList<>();
        it.add(new Book("Java入门到精通", 99));
        it.add(new Book("PHP入门到精通", 9.9));

        // 4.创建一个Map,键是书籍类型,值就是对应的书籍集合
        HashMap<String, ArrayList<Book>> map = new HashMap<>();

        // 5.添加对应的数据到Map集合中
        map.put("名著", mz);
        map.put("it书籍", it);

        // 让功能选择界面可以循环多次选择
        while (true) {
            // 1.功能选择界面, 打印输出相应字符串即可
            System.out.println("--------欢迎来到图书管理系统--------");
            System.out.println("1.查看书籍");
            System.out.println("2.添加书籍");
            System.out.println("3.删除书籍");
            System.out.println("4.修改书籍");
            System.out.println("5.退出");
            // 使用键盘输入
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入你的选择:");
            int operator = sc.nextInt();

            // 根据用户的输入进行选择
            switch (operator) {
                case 1:
                    showAllBooks(map);
                    break;
                case 2:
                    addBook(map);
                    break;
                case 3:
                    deleteBook(map);
                    break;
                case 4:
                    updateBook(map);
                    break;
                case 5:
                    System.out.println("大爷,请慢走,欢迎下次再来!");
                    System.exit(0);
                    break;
                default:
                    System.out.println("大爷,你的操作不存在.请不要乱来");
                    break;
            }
        }
    }

    private static void updateBook(HashMap<String, ArrayList<Book>> map) {
        // 目标: 实现 6.修改书籍
        Scanner sc = new Scanner(System.in);
        //  输入要修改书籍的类型
        System.out.println("请输入要修改书籍的类型");
        String type = sc.next();

        //  输入要修改书籍的书名
        System.out.println("请输入要修改书籍的书名");
        String oldName = sc.next();

        //  输入新的书名
        System.out.println("请输入新的书名");
        String newName = sc.next();
        //  输入新的价格
        System.out.println("请输入新的价格");
        double newPrice = sc.nextDouble();

        //  通过类型找到书籍的集合
        ArrayList<Book> list = map.get(type);
        if (list == null) {
            System.out.println("要修改的书籍类型不存在");
            return;
        }
        //  遍历书籍集合找到要修改的书籍
        for (int i = 0; i < list.size(); i++) {
            Book book = list.get(i);
            if (book.getName().equals(oldName)) {
                // 找到了要修改的书籍
                //  修改书籍
                book.setName(newName);
                book.setPrice(newPrice);
                System.out.println("修改" + oldName + "成功");
                return;
            }
        }

        System.out.println("没有找要修改的书籍" + oldName);
    }

    private static void deleteBook(HashMap<String, ArrayList<Book>> map) {
        // 目标: 实现 5.删除书籍
        Scanner sc = new Scanner(System.in);
        //  输入要删除书籍的类型
        System.out.println("请输入要删除书籍的类型");
        String type = sc.next();
        //  输入要删除的书名
        System.out.println("请输入要删除的书名");
        String name = sc.next();

        //  通过类型找到书籍的集合
        ArrayList<Book> list = map.get(type);

        if (list == null) {
            System.out.println("没有这个类型的书籍");
            return;
        }

        //  从集合中删除这本数
        for (int i = 0; i < list.size(); i++) {
            Book book = list.get(i);
            if (book.getName().equals(name)) {
                // 找到要删除的书籍,并删除
                list.remove(i);
                System.out.println("删除" + name + "成功");
                return ; // 结束这个方法
            }
        }

        System.out.println("没有这本书籍" + name);
    }

    private static void addBook(HashMap<String, ArrayList<Book>> map) {
        // 目标:实现 4.添加书籍
        Scanner sc = new Scanner(System.in);
        //  输入要添加书籍的类型
        System.out.println("请输入要添加书籍的类型");
        String type = sc.next();
        //  输入要添加的书名
        System.out.println("请输入要添加的书名");
        String name = sc.next();
        //  输入要添加书的价格
        System.out.println("请输入要添加书的价格");
        double price = sc.nextDouble();
        //  创建Book对象
        Book book = new Book(name, price);

        // 通过书籍类型,找到书籍集合
        ArrayList<Book> list = map.get(type);

        // 如果list为null说明,没有这种类型的书籍
        if (list == null) {
            list = new ArrayList<>();
            map.put(type, list);
        }

        // 判断书籍是否存在
        // 做一个标记,表示这本书是否存在,false表示不存在
        boolean exists = false;
        for (Book b : list) {
            if (b.getName().equals(name)) {
                // 书名相同;
                exists = true;
                break; // 一旦书名相同,就是重复的书籍,不需要往后面判断啦
            }
        }

        if (exists) {
            // 如果存在,就不添加
            System.out.println(name + "书籍已经存在");
        } else {
            // 如果不存在,就添加

            //  添加到对应的ArrayList集合中
            list.add(book);
            System.out.println("添加" + name + "书籍成功");
        }
    }

    private static void showAllBooks(HashMap<String, ArrayList<Book>> map) {
        System.out.println("类型\t\t书名\t价格");
        // 遍历Map,使用Entry方式
        // 1.得到所有的Entry
        Set<Map.Entry<String, ArrayList<Book>>> entrySet = map.entrySet();
        // 2.遍历得到每个Entry
        for (Map.Entry<String, ArrayList<Book>> entry : entrySet) {
            // 打印键
            System.out.println(entry.getKey());

            // 得到值(ArrayList集合)
            ArrayList<Book> value = entry.getValue();
            // 遍历ArrayList
            for (Book book : value) {
                System.out.println("\t\t\t" +book.getName() + "\t" + book.getPrice());
            }
        }
    }
}

13、总结

能够使用集合工具类

​ Collections类:
​ addAll(List list, T… a) 将后后面的数据添加到前面的集合
​ shuffle(List list) 对集合中的元素随机交换位置
​ sort(List list) 将集合中的元素按照自然顺序排序

能够使用Comparator比较器进行排序

​ Collections.sort(list, new Comparator() {
​ @Override
​ public int compare(Student o1, Student o2) {
​ // // 按照学生成绩升序
​ return (int)(o1.getSocre() - o2.getSocre());
​ }
​ });

能够使用可变参数

​ 修饰符 返回值类型 方法名(数据类型… 变量名) {
​ }

​ 调用方法时可以传入任意多个参数

能够理解冒泡排序的原理

​ 相邻元素比较,大的往后放

能够说出Map集合特点

​ 1.保存键和值
​ 2.键不能重复
​ 3.一个键对应一个值

使用Map集合添加方法保存数据

​ put(键, 值);

使用”键找值”的方式遍历Map集合

​ HashMap<String, String> map = new HashMap<>();
​ map.put(“邓超”, “孙俪”);
​ map.put(“黄晓明”, “杨颖”);
​ map.put(“谢霆锋”, “张柏芝”);
​ map.put(“老干爹”, “老干妈”);

1.获取所有的键

​ Set keySet = map.keySet();

2.遍历获取每个键

​ for (String key : keySet) {

3.通过键获取值

​ String value = map.get(key);
​ }

使用”键值对”的方式遍历Map集合

​ HashMap<String, String> map = new HashMap<>();
​ map.put(“邓超”, “孙俪”);
​ map.put(“黄晓明”, “杨颖”);
​ map.put(“谢霆锋”, “张柏芝”);
​ map.put(“老干爹”, “老干妈”);

​ // 1.得到所有的Entry
​ Set<Map.Entry<String, String>> entrySet = map.entrySet();
​ // 2.遍历得到每个Entry
​ for (Map.Entry<String, String> entry : entrySet) {
​ // 3.通过Entry得到键和值
​ System.out.println(entry.getKey() + “==” + entry.getValue());
​ }

能够使用HashMap存储自定义键值对的数据

​ 自定类型作为键,重写hashCode和equals保证键唯一

能够理解图书管理系统案例

​ 1.功能选择界面
​ 2.初始化书籍
​ 3.查询书籍
​ 4.添加书籍
​ 5.删除书籍
​ 6.修改书籍

课堂代码:
public class Demo13 {
    public static void main(String[] args) {
        // 有3个1-1000的数字a,b,c,求出a + b + c = 1000并且 a的平方+b的平方=c的平方; 讨论再后天给答案
        // 200 375 425
        // 375 200 425
        // 排列组合

        long start = System.currentTimeMillis();

        test03();

        long end = System.currentTimeMillis();
        System.out.println("消耗时间: " + (end - start));
    }

    public static void test03() {
        // 1000 * 1000
        for (int a = 1; a <= 1000; a++) {
            // a = 700 b最大就是300
            for (int b = 1; b <= 1000 - a; b++) {

                int c = 1000 - a - b;

                if (a*a + b*b == c*c) {
                    System.out.println("a = " + a + ", b = " + b + ",c = " + c);
                }
            }
        }
    }

    public static void test02() {
        // 1000 * 1000
        for (int a = 1; a <= 1000; a++) {

            for (int b = 1; b <= 1000; b++) {

                int c = 1000 - a - b;

                if (a*a + b*b == c*c) {
                    System.out.println("a = " + a + ", b = " + b + ",c = " + c);
                }
            }
        }
    }

    public static void test01() {
        // 1000 * 1000 * 1000 = 1 000 000 000
        for (int a = 1; a <= 1000; a++) {

            for (int b = 1; b <= 1000; b++) {

                for (int c = 1; c <= 1000; c++) {

                    if (a + b + c == 1000 && a*a + b*b == c*c) {
                        System.out.println("a = " + a + ", b = " + b + ",c = " + c);
                    }
                }
            }
        }
    }
}




public class Demo131 {
    public static void main(String[] args) {
        Integer i1 = 127; // 自动装箱Integer.valueOf(127);
        Integer i2 = 127;

        Integer i3 = 128;
        Integer i4 = 128;

        // 有一个缓存区,会存储到-128到127的Integer对象,如果再-128到127范围内,直接去数组中取出来
        // 如果是-128到127以外的会创建新的Integer.
        // -128到127这些数字使用比较频繁.就先创建,并存储取来,要时候直接拿去用.
        System.out.println(i1 == i2); // true
        System.out.println(i3 == i4); // false
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值