• Java笔记 泛型


    泛型:将对象的类型作为参数,指定到其他类或者方法上,从而保证类型转换的安全性和稳定性

     语法结构如下:类或者接口<类型实参> 对象 = new 类<类型实参>( );

    泛型集合  

            泛型集合可以约束集合内的元素类型

            典型泛型集合ArrayList<E>、HashMap<K,V>

                    <E>、<K,V>表示该泛型集合中的元素类型

                    泛型集合中的数据不再转换为Object

    1. public class NewsTitle {
    2. private int id;
    3. private String name;
    4. private String author;
    5. public NewsTitle() {
    6. super();
    7. }
    8. public NewsTitle(int id, String name, String author) {
    9. super();
    10. this.id = id;
    11. this.name = name;
    12. this.author = author;
    13. }
    14. public int getId() {
    15. return id;
    16. }
    17. public void setId(int id) {
    18. this.id = id;
    19. }
    20. public String getName() {
    21. return name;
    22. }
    23. public void setName(String name) {
    24. this.name = name;
    25. }
    26. public String getAuthor() {
    27. return author;
    28. }
    29. public void setAuthor(String author) {
    30. this.author = author;
    31. }
    32. @Override
    33. public String toString() {
    34. return "NewsTitle [id=" + id + ", name=" + name + ", author=" + author+ "]";
    35. }
    36. }
    1. import java.util.ArrayList;
    2. import java.util.Iterator;
    3. public class Test {
    4. public static void main(String[] args) {
    5. NewsTitle nt1=new NewsTitle(001,"合肥又发现一名新冠肺炎感染者","合肥日报");
    6. NewsTitle nt2=new NewsTitle(001,"合肥近日多天有雨","合肥气象局");
    7. NewsTitle nt3=new NewsTitle(001,"震惊!有一程序员猝死","张三");
    8. //定义一个集合,调用ArrayList类的无参的构造方法,默认构造一个初始容量为 10 的空列表。
    9. ArrayList<NewsTitle> al=new ArrayList<NewsTitle>();
    10. //将新闻标题的对象存储到al集合中
    11. al.add(nt1);
    12. al.add(nt2);
    13. al.add(nt3);
    14. //获取新闻标题的总数,即获取集合中的元素个数
    15. int num=al.size();
    16. System.out.println("新闻的标题总数为"+num);
    17. //void add(int index,Object o):在指定的索引位置添加元素。索引位置必须介于0和列表中元素个数之
    18. al.add(0, nt3);
    19. NewsTitle news =al.get(1);
    20. System.out.println("获取元素:"+news);
    21. //boolean contains(Object o):判断列表中是否存在指定元素,如果集合中存在你要找的元素,返回true,否则返回false
    22. boolean result1=al.contains(nt3);
    23. System.out.println("集合中你需要找到的元素nt3:"+result1);
    24. //boolean remove(Object o):从列表中删除元素,删除成功返回true,删除失败返回false
    25. boolean result2=al.remove(nt2);
    26. System.out.println("删除是否成功:"+result2);
    27. //toArray():将集合变成数组
    28. Object[] obj3 =al.toArray();
    29. for (int i = 0; i < obj3.length; i++) {
    30. System.out.println("转换成数组:"+obj3[i]);
    31. }
    32. System.out.println("------------------");
    33. for(NewsTitle ob:al){
    34. System.out.println(ob);
    35. }
    36. //遍历打印出每个新闻的名称
    37. for(int i=0;i<al.size();i++){
    38. Object obj=al.get(i);
    39. NewsTitle newsTitle=(NewsTitle)obj;
    40. System.out.println(newsTitle.getName());
    41. }
    42. //清除集合所有的元素
    43. // al.clear();
    44. // al.clone();
    45. //判断是否为空
    46. boolean result=al.isEmpty();
    47. System.out.println(result);
    48. Iterator<NewsTitle> it =al.iterator();
    49. while(it.hasNext()){
    50. //取出元素
    51. NewsTitle nt =it.next();
    52. System.out.println(nt);
    53. }
    54. }
    55. }
    1. import java.util.HashMap;
    2. import java.util.Map;
    3. import java.util.Set;
    4. public class Test {
    5. public static void main(String[] args) {
    6. //创建学生对象
    7. Student stu1 = new Student("张三", "男");
    8. Student stu2 = new Student("李四", "男");
    9. Student stu3 = new Student("如花", "女");
    10. //创建HashMap集合对象
    11. HashMap<String, Student> hm=new HashMap<String, Student>();
    12. //将学生对象添加到集合
    13. hm.put("jack", stu1);
    14. hm.put("tom", stu2);
    15. hm.put("rose", stu3);
    16. Object obj=hm.get("jack");
    17. Student stu =(Student)obj;
    18. System.out.println("jack对应的学员信息为:姓名:"+stu.getName()+",性别:"+stu.getGender());
    19. System.out.println("---------------");
    20. Set<Map.Entry<String, Student>> set=hm.entrySet();
    21. for (Map.Entry<String, Student> me : set) {
    22. //获取键值对应的键
    23. String key=me.getKey();
    24. //获取键值对应的值
    25. Student student=me.getValue();
    26. System.out.println(key+"对应的学员信息为:姓名:"+student.getName()+",性别:"+student.getGender());
    27. }
    28. }
    29. }

    Collections算法类

            Java集合框架将针对不同数据结构算法的实现都保存在工具类中

            Collections类定义了一系列用于操作集合的静态方法

    Collections类常用方法

            Collections和Collection不同,前者是集合的操作类,后者是集合接口

            Collections提供的常用静态方法

                    sort():排序

                    binarySearch():查找

                    max()\min():查找最大\最小值

    1. import java.util.ArrayList;
    2. import java.util.Collections;
    3. import java.util.Iterator;
    4. public class Demo01 {
    5. public static void main(String[] args) {
    6. //创建一个ArrayList集合对象,存储String类型数据
    7. ArrayList<String> al=new ArrayList<String>();
    8. al.add("fadga");
    9. al.add("addfag");
    10. al.add("hmjej");
    11. al.add("fagevsf");
    12. al.add("jwryjwyr");
    13. System.out.println("集合排序之前:");
    14. for (String string : al) {
    15. System.out.println(string);
    16. }
    17. //排序
    18. Collections.sort(al);
    19. System.out.println("集合排序之后:");
    20. for (String string : al) {
    21. System.out.println(string);
    22. }
    23. //查找元素
    24. int index=Collections.binarySearch(al, "fagevsf");
    25. System.out.println(index);
    26. //求集合中的最大值最小值
    27. String max = Collections.max(al);
    28. System.out.println(max);
    29. String min = Collections.min(al);
    30. System.out.println(min);
    31. Collections.reverse(al);
    32. System.out.println("集合反转之后:");
    33. Iterator<String> it=al.iterator();
    34. while(it.hasNext()){
    35. String str =it.next();
    36. System.out.println(str);
    37. }
    38. }
    39. }

    Collections排序

            Collections类可以对集合进行排序、查找和替换操作

            实现一个类的对象之间比较大小,该类要实现Comparable接口

                    重写compareTo()方法

    1. public class Student implements Comparable<Student> {
    2. private String name;
    3. private int stuId;
    4. public Student() {
    5. super();
    6. }
    7. public Student(String name, int stuId) {
    8. super();
    9. this.name = name;
    10. this.stuId = stuId;
    11. }
    12. public String getName() {
    13. return name;
    14. }
    15. public void setName(String name) {
    16. this.name = name;
    17. }
    18. public int getStuId() {
    19. return stuId;
    20. }
    21. public void setStuId(int stuId) {
    22. this.stuId = stuId;
    23. }
    24. @Override
    25. public String toString() {
    26. return "Student [name=" + name + ", stuId=" + stuId + "]";
    27. }
    28. @Override
    29. public int compareTo(Student student) {
    30. //比较此对象与指定对象的顺序。
    31. return this.stuId-student.stuId;
    32. }
    33. }
    34. import java.util.ArrayList;
    35. import java.util.Collections;
    36. public class StudentTest {
    37. public static void main(String[] args) {
    38. //创建4个Student类对象
    39. Student stu1 = new Student("张三", 1001);
    40. Student stu2 = new Student("李四", 1002);
    41. Student stu3 = new Student("王五", 1003);
    42. Student stu4 = new Student("赵六", 1004);
    43. //创建ArrayList集合对象
    44. ArrayList<Student> al = new ArrayList<Student>();
    45. al.add(stu3);
    46. al.add(stu2);
    47. al.add(stu4);
    48. al.add(stu1);
    49. System.out.println("集合排序前:");
    50. for (Student student : al) {
    51. System.out.println(student);
    52. }
    53. Collections.sort(al);
    54. System.out.println("集合排序后:");
    55. for (Student student : al) {
    56. System.out.println(student);
    57. }
    58. }
    59. }

  • 相关阅读:
    众和策略可靠吗?中国资产,一夜狂飙!
    R语言检验时间序列中是否存在自相关性:使用box.test函数执行box-pierce检验验证时间序列中是否存在自相关性
    同步篇——内核对象
    Linux系统使用宝塔面板安装MySQL服务并实现公网远程访问本地数据库【内网穿透】
    Spring的总结
    java基础10题
    gpt扣款失败,openai扣款失败无法使用-如何解决gpt扣款失败的问题?
    Spring IoC和DI的理解
    微信小程序快速上手(学习笔记总结)
    软考-图的遍历笔记
  • 原文地址:https://blog.csdn.net/qq_69761234/article/details/125541431