小白想学学java——————5

1.包装类

1.1基本类型包装类(记忆)

  • 基本类型包装类的作用

    ​ 将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据

    ​ 常用的操作之一:用于基本数据类型与字符串之间的转换

  • 基本类型对应的包装类

    基本数据类型包装类
    byteByte
    shortShort
    intInteger
    longLong
    floatFloat
    doubleDouble
    charCharacter
    booleanBoolean

1.2Integer类(应用)

  • Integer类概述

    ​ 包装一个对象中的原始类型 int 的值

  • Integer类构造方法

    方法名说明
    public Integer(int value)根据 int 值创建 Integer 对象(过时)
    public Integer(String s)根据 String 值创建 Integer 对象(过时)
    public static Integer valueOf(int i)返回表示指定的 int 值的 Integer 实例
    public static Integer valueOf(String s)返回一个保存指定值的 Integer 对象 String
  • 示例代码

    public class IntegerDemo {
        public static void main(String[] args) {
            //public Integer(int value):根据 int 值创建 Integer 对象(过时)
            Integer i1 = new Integer(100);
            System.out.println(i1);
    
            //public Integer(String s):根据 String 值创建 Integer 对象(过时)
            Integer i2 = new Integer("100");
    //        Integer i2 = new Integer("abc"); //NumberFormatException
            System.out.println(i2);
            System.out.println("--------");
    
            //public static Integer valueOf(int i):返回表示指定的 int 值的 Integer 实例
            Integer i3 = Integer.valueOf(100);
            System.out.println(i3);
    
            //public static Integer valueOf(String s):返回一个保存指定值的Integer对象 String
            Integer i4 = Integer.valueOf("100");
            System.out.println(i4);
        }
    }
    

1.3int和String类型的相互转换(记忆)

  • int转换为String

    • 转换方式

      • 方式一:直接在数字后加一个空字符串
      • 方式二:通过String类静态方法valueOf()
    • 示例代码

      public class IntegerDemo {
          public static void main(String[] args) {
              //int --- String
              int number = 100;
              //方式1
              String s1 = number + "";
              System.out.println(s1);
              //方式2
              //public static String valueOf(int i)
              String s2 = String.valueOf(number);
              System.out.println(s2);
              System.out.println("--------");
          }
      }
      
  • String转换为int

    • 转换方式

      • 方式一:先将字符串数字转成Integer,再调用valueOf()方法
      • 方式二:通过Integer静态方法parseInt()进行转换
    • 示例代码

      public class IntegerDemo {
          public static void main(String[] args) {
              //String --- int
              String s = "100";
              //方式1:String --- Integer --- int
              Integer i = Integer.valueOf(s);
              //public int intValue()
              int x = i.intValue();
              System.out.println(x);
              //方式2
              //public static int parseInt(String s)
              int y = Integer.parseInt(s);
              System.out.println(y);
          }
      }
      

1.4字符串数据排序案例(应用)

  • 案例需求

    ​ 有一个字符串:“91 27 46 38 50”,请写程序实现最终输出结果是:“27 38 46 50 91”

  • 代码实现

    public class IntegerTest {
        public static void main(String[] args) {
            //定义一个字符串
            String s = "91 27 46 38 50";
    
            //把字符串中的数字数据存储到一个int类型的数组中
            String[] strArray = s.split(" ");
    //        for(int i=0; i<strArray.length; i++) {
    //            System.out.println(strArray[i]);
    //        }
    
            //定义一个int数组,把 String[] 数组中的每一个元素存储到 int 数组中
            int[] arr = new int[strArray.length];
            for(int i=0; i<arr.length; i++) {
                arr[i] = Integer.parseInt(strArray[i]);
            }
    
            //对 int 数组进行排序
            Arrays.sort(arr);
    
            //把排序后的int数组中的元素进行拼接得到一个字符串,这里拼接采用StringBuilder来实现
            StringBuilder sb = new StringBuilder();
            for(int i=0; i<arr.length; i++) {
                if(i == arr.length - 1) {
                    sb.append(arr[i]);
                } else {
                    sb.append(arr[i]).append(" ");
                }
            }
            String result = sb.toString();
    
            //输出结果
            System.out.println(result);
        }
    }
    

1.5自动拆箱和自动装箱(理解)

  • 自动装箱

    ​ 把基本数据类型转换为对应的包装类类型

  • 自动拆箱

    ​ 把包装类类型转换为对应的基本数据类型

  • 示例代码

    Integer i = 100;  // 自动装箱
    i += 200;         // i = i + 200;  i + 200 自动拆箱;i = i + 200; 是自动装箱
    

2.时间日期类

2.1Date类(应用)

  • Date类概述

    ​ Date 代表了一个特定的时间,精确到毫秒

  • Date类构造方法

    方法名说明
    public Date()分配一个 Date对象,并初始化,以便它代表它被分配的时间,精确到毫秒
    public Date(long date)分配一个 Date对象,并将其初始化为表示从标准基准时间起指定的毫秒数
  • 示例代码

    public class DateDemo01 {
        public static void main(String[] args) {
            //public Date():分配一个 Date对象,并初始化,以便它代表它被分配的时间,精确到毫秒
            Date d1 = new Date();
            System.out.println(d1);
    
            //public Date(long date):分配一个 Date对象,并将其初始化为表示从标准基准时间起指定的毫秒数
            long date = 1000*60*60;
            Date d2 = new Date(date);
            System.out.println(d2);
        }
    }
    

2.2Date类常用方法(应用)

  • 常用方法

    方法名说明
    public long getTime()获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值
    public void setTime(long time)设置时间,给的是毫秒值
  • 示例代码

    public class DateDemo02 {
        public static void main(String[] args) {
            //创建日期对象
            Date d = new Date();
    
            //public long getTime():获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值
    //        System.out.println(d.getTime());
    //        System.out.println(d.getTime() * 1.0 / 1000 / 60 / 60 / 24 / 365 + "年");
    
            //public void setTime(long time):设置时间,给的是毫秒值
    //        long time = 1000*60*60;
            long time = System.currentTimeMillis();
            d.setTime(time);
    
            System.out.println(d);
        }
    }
    

2.3SimpleDateFormat类(应用)

  • SimpleDateFormat类概述

    ​ SimpleDateFormat是一个具体的类,用于以区域设置敏感的方式格式化和解析日期。

    ​ 我们重点学习日期格式化和解析

  • SimpleDateFormat类构造方法

    方法名说明
    public SimpleDateFormat()构造一个SimpleDateFormat,使用默认模式和日期格式
    public SimpleDateFormat(String pattern)构造一个SimpleDateFormat使用给定的模式和默认的日期格式
  • SimpleDateFormat类的常用方法

    • 格式化(从Date到String)
      • public final String format(Date date):将日期格式化成日期/时间字符串
    • 解析(从String到Date)
      • public Date parse(String source):从给定字符串的开始解析文本以生成日期
  • 示例代码

    public class SimpleDateFormatDemo {
        public static void main(String[] args) throws ParseException {
            //格式化:从 Date 到 String
            Date d = new Date();
    //        SimpleDateFormat sdf = new SimpleDateFormat();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            String s = sdf.format(d);
            System.out.println(s);
            System.out.println("--------");
    
            //从 String 到 Date
            String ss = "2048-08-09 11:11:11";
            //ParseException
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date dd = sdf2.parse(ss);
            System.out.println(dd);
        }
    }
    

2.4日期工具类案例(应用)

  • 案例需求

    ​ 定义一个日期工具类(DateUtils),包含两个方法:把日期转换为指定格式的字符串;把字符串解析为指定格式的日期,然后定义一个测试类(DateDemo),测试日期工具类的方法

  • 代码实现

    • 工具类
    public class DateUtils {
        private DateUtils() {}
    
        /*
            把日期转为指定格式的字符串
            返回值类型:String
            参数:Date date, String format
         */
        public static String dateToString(Date date, String format) {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            String s = sdf.format(date);
            return s;
        }
    
    
        /*
            把字符串解析为指定格式的日期
            返回值类型:Date
            参数:String s, String format
         */
        public static Date stringToDate(String s, String format) throws ParseException {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            Date d = sdf.parse(s);
            return d;
        }
    
    }
    
    • 测试类
    public class DateDemo {
        public static void main(String[] args) throws ParseException {
            //创建日期对象
            Date d = new Date();
    
            String s1 = DateUtils.dateToString(d, "yyyy年MM月dd日 HH:mm:ss");
            System.out.println(s1);
    
            String s2 = DateUtils.dateToString(d, "yyyy年MM月dd日");
            System.out.println(s2);
    
            String s3 = DateUtils.dateToString(d, "HH:mm:ss");
            System.out.println(s3);
            System.out.println("--------");
    
            String s = "2048-08-09 12:12:12";
            Date dd = DateUtils.stringToDate(s, "yyyy-MM-dd HH:mm:ss");
            System.out.println(dd);
        }
    }
    

2.5Calendar类(应用)

  • Calendar类概述

    ​ Calendar 为特定瞬间与一组日历字段之间的转换提供了一些方法,并为操作日历字段提供了一些方法

    ​ Calendar 提供了一个类方法 getInstance 用于获取这种类型的一般有用的对象。

    ​ 该方法返回一个Calendar 对象。

    ​ 其日历字段已使用当前日期和时间初始化:Calendar rightNow = Calendar.getInstance();

  • Calendar类常用方法

    方法名说明
    public int get(int field)返回给定日历字段的值
    public abstract void add(int field, int amount)根据日历的规则,将指定的时间量添加或减去给定的日历字段
    public final void set(int year,int month,int date)设置当前日历的年月日
  • 示例代码

    public class CalendarDemo {
        public static void main(String[] args) {
            //获取日历类对象
            Calendar c = Calendar.getInstance();
    
            //public int get(int field):返回给定日历字段的值
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH) + 1;
            int date = c.get(Calendar.DATE);
            System.out.println(year + "年" + month + "月" + date + "日");
    
            //public abstract void add(int field, int amount):根据日历的规则,将指定的时间量添加或减去给定的日历字段
            //需求1:3年前的今天
    //        c.add(Calendar.YEAR,-3);
    //        year = c.get(Calendar.YEAR);
    //        month = c.get(Calendar.MONTH) + 1;
    //        date = c.get(Calendar.DATE);
    //        System.out.println(year + "年" + month + "月" + date + "日");
    
            //需求2:10年后的10天前
    //        c.add(Calendar.YEAR,10);
    //        c.add(Calendar.DATE,-10);
    //        year = c.get(Calendar.YEAR);
    //        month = c.get(Calendar.MONTH) + 1;
    //        date = c.get(Calendar.DATE);
    //        System.out.println(year + "年" + month + "月" + date + "日");
    
            //public final void set(int year,int month,int date):设置当前日历的年月日
            c.set(2050,10,10);
            year = c.get(Calendar.YEAR);
            month = c.get(Calendar.MONTH) + 1;
            date = c.get(Calendar.DATE);
            System.out.println(year + "年" + month + "月" + date + "日");
    
        }
    }
    

2.6二月天案例(应用)

  • 案例需求

    ​ 获取任意一年的二月有多少天

  • 代码实现

    public class CalendarTest {
        public static void main(String[] args) {
            //键盘录入任意的年份
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入年:");
            int year = sc.nextInt();
    
            //设置日历对象的年、月、日
            Calendar c = Calendar.getInstance();
            c.set(year, 2, 1);
    
            //3月1日往前推一天,就是2月的最后一天
            c.add(Calendar.DATE, -1);
    
            //获取这一天输出即可
            int date = c.get(Calendar.DATE);
            System.out.println(year + "年的2月份有" + date + "天");
        }
    }
    

3.异常

3.1异常(记忆)

  • 异常的概述

    ​ 异常就是程序出现了不正常的情况

  • 异常的体系结构

    ​ [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RZJoP2H7-1627043568470)(img\01.png)]

3.2JVM默认处理异常的方式(理解)

  • 如果程序出现了问题,我们没有做任何处理,最终JVM 会做默认的处理,处理方式有如下两个步骤:

  • 把异常的名称,错误原因及异常出现的位置等信息输出在了控制台

  • 程序停止执行

3.3try-catch方式处理异常(应用)

  • 定义格式

    try {
    	可能出现异常的代码;
    } catch(异常类名 变量名) {
    	异常的处理代码;
    }
    
  • 执行流程

    • 程序从 try 里面的代码开始执行
    • 出现异常,就会跳转到对应的 catch 里面去执行
    • 执行完毕之后,程序还可以继续往下执行
  • 示例代码

    public class ExceptionDemo01 {
        public static void main(String[] args) {
            System.out.println("开始");
            method();
            System.out.println("结束");
        }
    
        public static void method() {
            try {
                int[] arr = {1, 2, 3};
                System.out.println(arr[3]);
                System.out.println("这里能够访问到吗");
            } catch (ArrayIndexOutOfBoundsException e) {
    //            System.out.println("你访问的数组索引不存在,请回去修改为正确的索引");
                e.printStackTrace();
            }
        }
    }
    

3.4Throwable成员方法(应用)

  • 常用方法

    方法名说明
    public String getMessage()返回此 throwable 的详细消息字符串
    public String toString()返回此可抛出的简短描述
    public void printStackTrace()把异常的错误信息输出在控制台
  • 示例代码

    public class ExceptionDemo02 {
        public static void main(String[] args) {
            System.out.println("开始");
            method();
            System.out.println("结束");
        }
    
        public static void method() {
            try {
                int[] arr = {1, 2, 3};
                System.out.println(arr[3]); //new ArrayIndexOutOfBoundsException();
                System.out.println("这里能够访问到吗");
            } catch (ArrayIndexOutOfBoundsException e) { //new ArrayIndexOutOfBoundsException();
    //            e.printStackTrace();
    
                //public String getMessage():返回此 throwable 的详细消息字符串
    //            System.out.println(e.getMessage());
                //Index 3 out of bounds for length 3
    
                //public String toString():返回此可抛出的简短描述
    //            System.out.println(e.toString());
                //java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
    
                //public void printStackTrace():把异常的错误信息输出在控制台
                e.printStackTrace();
    //            java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
    //            at com.itheima_02.ExceptionDemo02.method(ExceptionDemo02.java:18)
    //            at com.itheima_02.ExceptionDemo02.main(ExceptionDemo02.java:11)
    
            }
        }
    }
    

3.5编译时异常和运行时异常的区别(记忆)

  • 编译时异常

    • 都是Exception类及其子类
    • 必须显示处理,否则程序就会发生错误,无法通过编译
  • 运行时异常

    • 都是RuntimeException类及其子类
    • 无需显示处理,也可以和编译时异常一样处理

3.6throws方式处理异常(应用)

  • 定义格式

    public void 方法() throws 异常类名 {
        
    }
    
  • 示例代码

    public class ExceptionDemo {
        public static void main(String[] args) {
            System.out.println("开始");
    //        method();
            try {
                method2();
            }catch (ParseException e) {
                e.printStackTrace();
            }
            System.out.println("结束");
        }
    
        //编译时异常
        public static void method2() throws ParseException {
            String s = "2048-08-09";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date d = sdf.parse(s);
            System.out.println(d);
        }
    
        //运行时异常
        public static void method() throws ArrayIndexOutOfBoundsException {
            int[] arr = {1, 2, 3};
            System.out.println(arr[3]);
        }
    }
    
  • 注意事项

    • 这个throws格式是跟在方法的括号后面的
    • 编译时异常必须要进行处理,两种处理方案:try…catch …或者 throws,如果采用 throws 这种方案,将来谁调用谁处理
    • 运行时异常可以不处理,出现问题后,需要我们回来修改代码

3.7throws和throw的区别(记忆)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GnrF2NS8-1627043568472)(img\02.png)]

3.8自定义异常(应用)

  • 自定义异常类

    public class ScoreException extends Exception {
    
        public ScoreException() {}
    
        public ScoreException(String message) {
            super(message);
        }
    
    }
    
  • 老师类

    public class Teacher {
        public void checkScore(int score) throws ScoreException {
            if(score<0 || score>100) {
    //            throw new ScoreException();
                throw new ScoreException("你给的分数有误,分数应该在0-100之间");
            } else {
                System.out.println("成绩正常");
            }
        }
    }
    
  • 测试类

    public class Demo {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入分数:");
    
            int score = sc.nextInt();
    
            Teacher t = new Teacher();
            try {
                t.checkScore(score);
            } catch (ScoreException e) {
                e.printStackTrace();
            }
        }
    }
    

1.Collection集合

1.1集合体系结构【记忆】

  • 集合类的特点

    ​ 提供一种存储空间可变的存储模型,存储的数据容量可以随时发生改变

  • 集合类的体系图

    ​ [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Z2ruqMRU-1627106583760)(img\01.png)]

1.2Collection集合概述和基本使用【应用】

  • Collection集合概述

    • 是单例集合的顶层接口,它表示一组对象,这些对象也称为Collection的元素

    • JDK 不提供此接口的任何直接实现,它提供更具体的子接口(如Set和List)实现

  • Collection集合基本使用

    public class CollectionDemo01 {
        public static void main(String[] args) {
            //创建Collection集合的对象
            Collection<String> c = new ArrayList<String>();
    
            //添加元素:boolean add(E e)
            c.add("hello");
            c.add("world");
            c.add("java");
    
            //输出集合对象
            System.out.println(c);
        }
    }
    

1.3Collection集合的常用方法【应用】

方法名说明
boolean add(E e)添加元素
boolean remove(Object o)从集合中移除指定的元素
void clear()清空集合中的元素
boolean contains(Object o)判断集合中是否存在指定的元素
boolean isEmpty()判断集合是否为空
int size()集合的长度,也就是集合中元素的个数

1.4Collection集合的遍历【应用】

  • 迭代器的介绍
    • 迭代器,集合的专用遍历方式
    • Iterator iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到
    • 迭代器是通过集合的iterator()方法得到的,所以我们说它是依赖于集合而存在的
  • Collection集合的遍历
public class IteratorDemo {
    public static void main(String[] args) {
        //创建集合对象
        Collection<String> c = new ArrayList<>();

        //添加元素
        c.add("hello");
        c.add("world");
        c.add("java");
        c.add("javaee");

        //Iterator<E> iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到
        Iterator<String> it = c.iterator();

        //用while循环改进元素的判断和获取
        while (it.hasNext()) {
            String s = it.next();
            System.out.println(s);
        }
 
        

1.5集合使用步骤图解【理解】

  • 使用步骤

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yCSNP1ak-1627106583761)(img\02.png)]

1.6集合的案例-Collection集合存储学生对象并遍历【应用】

  • 案例需求

    ​ 创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合

  • 代码实现

    • 学生类
    public class Student {
        private String name;
        private int age;
    
        public Student() {
        }
    
        public Student(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;
        }
    }
    
    • 测试类
    public class CollectionDemo {
        public static void main(String[] args) {
            //创建Collection集合对象
            Collection<Student> c = new ArrayList<Student>();
    
            //创建学生对象
            Student s1 = new Student("林青霞", 30);
            Student s2 = new Student("张曼玉", 35);
            Student s3 = new Student("王祖贤", 33);
    
            //把学生添加到集合
            c.add(s1);
            c.add(s2);
            c.add(s3);
    
            //遍历集合(迭代器方式)
            Iterator<Student> it = c.iterator();
            while (it.hasNext()) {
                Student s = it.next();
                System.out.println(s.getName() + "," + s.getAge());
            }
        }
    }
    

2.List集合

2.1List集合概述和特点【记忆】

  • List集合概述
    • 有序集合(也称为序列),用户可以精确控制列表中每个元素的插入位置。用户可以通过整数索引访问元素,并搜索列表中的元素
    • 与Set集合不同,列表通常允许重复的元素
  • List集合特点
    • 有索引
    • 可以存储重复元素
    • 元素存取有序

2.2List集合的特有方法【应用】

方法名描述
void add(int index,E element)在此集合中的指定位置插入指定的元素
E remove(int index)删除指定索引处的元素,返回被删除的元素
E set(int index,E element)修改指定索引处的元素,返回被修改的元素
E get(int index)返回指定索引处的元素

2.3集合的案例-List集合存储学生对象并遍历【应用】

  • 案例需求

    ​ 创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合

  • 代码实现

    • 学生类

      public class Student {
          private String name;
          private int age;
      
          public Student() {
          }
      
          public Student(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;
          }
      }
      
    • 测试类

      public class ListDemo {
          public static void main(String[] args) {
              //创建List集合对象
              List<Student> list = new ArrayList<Student>();
      
              //创建学生对象
              Student s1 = new Student("林青霞", 30);
              Student s2 = new Student("张曼玉", 35);
              Student s3 = new Student("王祖贤", 33);
      
              //把学生添加到集合
              list.add(s1);
              list.add(s2);
              list.add(s3);
      
              //迭代器方式
              Iterator<Student> it = list.iterator();
              while (it.hasNext()) {
                  Student s = it.next();
                  System.out.println(s.getName() + "," + s.getAge());
              }
              
              System.out.println("--------");
      
              //for循环方式
              for(int i=0; i<list.size(); i++) {
                  Student s = list.get(i);
                  System.out.println(s.getName() + "," + s.getAge());
              }
      
          }
      }
      

2.4并发修改异常【应用】

  • 出现的原因

    ​ 迭代器遍历的过程中,通过集合对象修改了集合中的元素,造成了迭代器获取元素中判断预期修改值和实际修改值不一致,则会出现:ConcurrentModificationException

  • 解决的方案

    ​ 用for循环遍历,然后用集合对象做对应的操作即可

  • 示例代码

    public class ListDemo {
        public static void main(String[] args) {
            //创建集合对象
            List<String> list = new ArrayList<String>();
    
            //添加元素
            list.add("hello");
            list.add("world");
            list.add("java");
    
            //遍历集合,得到每一个元素,看有没有"world"这个元素,如果有,我就添加一个"javaee"元素,请写代码实现
    //        Iterator<String> it = list.iterator();
    //        while (it.hasNext()) {
    //            String s = it.next();
    //            if(s.equals("world")) {
    //                list.add("javaee");
    //            }
    //        }
    
            for(int i=0; i<list.size(); i++) {
                String s = list.get(i);
                if(s.equals("world")) {
                    list.add("javaee");
                }
            }
    
            //输出集合对象
            System.out.println(list);
        }
    }
    

2.5列表迭代器【应用】

  • ListIterator介绍

    • 通过List集合的listIterator()方法得到,所以说它是List集合特有的迭代器
    • 用于允许程序员沿任一方向遍历的列表迭代器,在迭代期间修改列表,并获取列表中迭代器的当前位置
  • 示例代码

    public class ListIteratorDemo {
        public static void main(String[] args) {
            //创建集合对象
            List<String> list = new ArrayList<String>();
    
            //添加元素
            list.add("hello");
            list.add("world");
            list.add("java");
    
            //获取列表迭代器
            ListIterator<String> lit = list.listIterator();
            while (lit.hasNext()) {
                String s = lit.next();
                if(s.equals("world")) {
                    lit.add("javaee");
                }
            }
    
            System.out.println(list);
    
        }
    }
    

2.6增强for循环【应用】

  • 定义格式

    for(元素数据类型 变量名 : 数组/集合对象名) {
        循环体;
    }
    
  • 示例代码

    public class ForDemo {
        public static void main(String[] args) {
            int[] arr = {1,2,3,4,5};
            for(int i : arr) {
                System.out.println(i);
            }
            
            System.out.println("--------");
    
            String[] strArray = {"hello","world","java"};
            for(String s : strArray) {
                System.out.println(s);
            }
            
            System.out.println("--------");
    
            List<String> list = new ArrayList<String>();
            list.add("hello");
            list.add("world");
            list.add("java");
    
            for(String s : list) {
                System.out.println(s);
            }
            
            System.out.println("--------");
    
            //内部原理是一个Iterator迭代器
            /*
            for(String s : list) {
                if(s.equals("world")) {
                    list.add("javaee"); //ConcurrentModificationException
                }
            }
            */
        }
    }
    

2.7集合的案例-List集合存储学生对象三种方式遍历【应用】

  • 案例需求

    ​ 创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合

  • 代码实现

    • 学生类

      public class Student {
          private String name;
          private int age;
      
          public Student() {
          }
      
          public Student(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;
          }
      }
      
    • 测试类

      public class ListDemo {
          public static void main(String[] args) {
              //创建List集合对象
              List<Student> list = new ArrayList<Student>();
      
              //创建学生对象
              Student s1 = new Student("林青霞", 30);
              Student s2 = new Student("张曼玉", 35);
              Student s3 = new Student("王祖贤", 33);
      
              //把学生添加到集合
              list.add(s1);
              list.add(s2);
              list.add(s3);
      
              //迭代器:集合特有的遍历方式
              Iterator<Student> it = list.iterator();
              while (it.hasNext()) {
                  Student s = it.next();
                  System.out.println(s.getName()+","+s.getAge());
              }
              System.out.println("--------");
      
              //普通for:带有索引的遍历方式
              for(int i=0; i<list.size(); i++) {
                  Student s = list.get(i);
                  System.out.println(s.getName()+","+s.getAge());
              }
              System.out.println("--------");
      
              //增强for:最方便的遍历方式
              for(Student s : list) {
                  System.out.println(s.getName()+","+s.getAge());
              }
          }
      }
      

3.数据结构

3.1数据结构之栈和队列【记忆】

  • 栈结构

    ​ 先进后出

  • 队列结构

    ​ 先进先出

3.2数据结构之数组和链表【记忆】

  • 数组结构

    ​ 查询快、增删慢

  • 队列结构

    ​ 查询慢、增删快

4.List集合的实现类

4.1List集合子类的特点【记忆】

  • ArrayList集合

    ​ 底层是数组结构实现,查询快、增删慢

  • LinkedList集合

    ​ 底层是链表结构实现,查询慢、增删快

4.2集合的案例-ArrayList集合存储学生对象三种方式遍历【应用】

  • 案例需求

    ​ 创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合

  • 代码实现

    • 学生类

      public class Student {
          private String name;
          private int age;
      
          public Student() {
          }
      
          public Student(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;
          }
      }
      
    • 测试类

      public class ArrayListDemo {
          public static void main(String[] args) {
              //创建ArrayList集合对象
              ArrayList<Student> array = new ArrayList<Student>();
      
              //创建学生对象
              Student s1 = new Student("林青霞", 30);
              Student s2 = new Student("张曼玉", 35);
              Student s3 = new Student("王祖贤", 33);
      
              //把学生添加到集合
              array.add(s1);
              array.add(s2);
              array.add(s3);
      
              //迭代器:集合特有的遍历方式
              Iterator<Student> it = array.iterator();
              while (it.hasNext()) {
                  Student s = it.next();
                  System.out.println(s.getName() + "," + s.getAge());
              }
              System.out.println("--------");
      
              //普通for:带有索引的遍历方式
              for(int i=0; i<array.size(); i++) {
                  Student s = array.get(i);
                  System.out.println(s.getName() + "," + s.getAge());
              }
              System.out.println("--------");
      
              //增强for:最方便的遍历方式
              for(Student s : array) {
                  System.out.println(s.getName() + "," + s.getAge());
              }
          }
      }
      

4.3LinkedList集合的特有功能【应用】

  • 特有方法

    方法名说明
    public void addFirst(E e)在该列表开头插入指定的元素
    public void addLast(E e)将指定的元素追加到此列表的末尾
    public E getFirst()返回此列表中的第一个元素
    public E getLast()返回此列表中的最后一个元素
    public E removeFirst()从此列表中删除并返回第一个元素
    public E removeLast()从此列表中删除并返回最后一个元素

1.Set集合

1.1Set集合概述和特点【应用】

  • Set集合的特点
    • 元素存取无序
    • 没有索引、只能通过迭代器或增强for循环遍历
    • 不能存储重复元素
  • Set集合的基本使用
public class SetDemo {
    public static void main(String[] args) {
        //创建集合对象
        Set<String> set = new HashSet<String>();

        //添加元素
        set.add("hello");
        set.add("world");
        set.add("java");
        //不包含重复元素的集合
        set.add("world");

        //遍历
        for(String s : set) {
            System.out.println(s);
        }
    }
}

1.2哈希值【理解】

  • 哈希值简介

    ​ 是JDK根据对象的地址或者字符串或者数字算出来的int类型的数值

  • 如何获取哈希值

    ​ Object类中的public int hashCode():返回对象的哈希码值

  • 哈希值的特点

    • 同一个对象多次调用hashCode()方法返回的哈希值是相同的
    • 默认情况下,不同对象的哈希值是不同的。而重写hashCode()方法,可以实现让不同对象的哈希值相同
  • 获取哈希值的代码

    • 学生类
    public class Student {
        private String name;
        private int age;
    
        public Student() {
        }
    
        public Student(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;
        }
    
        @Override
        public int hashCode() {
            return 0;
        }
    }
    
    • 测试类
    public class HashDemo {
        public static void main(String[] args) {
            //创建学生对象
            Student s1 = new Student("林青霞",30);
    
            //同一个对象多次调用hashCode()方法返回的哈希值是相同的
            System.out.println(s1.hashCode()); //1060830840
            System.out.println(s1.hashCode()); //1060830840
            System.out.println("--------");
    
            Student s2 = new Student("林青霞",30);
    
            //默认情况下,不同对象的哈希值是不相同的
            //通过方法重写,可以实现不同对象的哈希值是相同的
            System.out.println(s2.hashCode()); //2137211482
            System.out.println("--------");
    
            System.out.println("hello".hashCode()); //99162322
            System.out.println("world".hashCode()); //113318802
            System.out.println("java".hashCode()); //3254818
    
            System.out.println("world".hashCode()); //113318802
            System.out.println("--------");
    
            System.out.println("重地".hashCode()); //1179395
            System.out.println("通话".hashCode()); //1179395
        }
    }
    

1.3HashSet集合概述和特点【应用】

  • HashSet集合的特点

    • 底层数据结构是哈希表
    • 对集合的迭代顺序不作任何保证,也就是说不保证存储和取出的元素顺序一致
    • 没有带索引的方法,所以不能使用普通for循环遍历
    • 由于是Set集合,所以是不包含重复元素的集合
  • HashSet集合的基本使用

    public class HashSetDemo01 {
        public static void main(String[] args) {
            //创建集合对象
            HashSet<String> hs = new HashSet<String>();
    
            //添加元素
            hs.add("hello");
            hs.add("world");
            hs.add("java");
    
            hs.add("world");
    
            //遍历
            for(String s : hs) {
                System.out.println(s);
            }
        }
    }
    

1.4HashSet集合保证元素唯一性源码分析【理解】

  • HashSet集合保证元素唯一性的原理

    ​ 1.根据对象的哈希值计算存储位置

    ​ 如果当前位置没有元素则直接存入

    ​ 如果当前位置有元素存在,则进入第二步

    ​ 2.当前元素的元素和已经存在的元素比较哈希值

    ​ 如果哈希值不同,则将当前元素进行存储

    ​ 如果哈希值相同,则进入第三步

    ​ 3.通过equals()方法比较两个元素的内容

    ​ 如果内容不相同,则将当前元素进行存储

    ​ 如果内容相同,则不存储当前元素

  • HashSet集合保证元素唯一性的图解

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tPdgxc3G-1627113989601)(img\01.png)]

1.5常见数据结构之哈希表【理解】

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jr5T0B92-1627113989604)(img\02.png)]

1.6HashSet集合存储学生对象并遍历【应用】

  • 案例需求

    • 创建一个存储学生对象的集合,存储多个学生对象,使用程序实现在控制台遍历该集合
    • 要求:学生对象的成员变量值相同,我们就认为是同一个对象
  • 代码实现

    • 学生类

      public class Student {
          private String name;
          private int age;
      
          public Student() {
          }
      
          public Student(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;
          }
      
          @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 class HashSetDemo02 {
          public static void main(String[] args) {
              //创建HashSet集合对象
              HashSet<Student> hs = new HashSet<Student>();
      
              //创建学生对象
              Student s1 = new Student("林青霞", 30);
              Student s2 = new Student("张曼玉", 35);
              Student s3 = new Student("王祖贤", 33);
      
              Student s4 = new Student("王祖贤", 33);
      
              //把学生添加到集合
              hs.add(s1);
              hs.add(s2);
              hs.add(s3);
              hs.add(s4);
      
              //遍历集合(增强for)
              for (Student s : hs) {
                  System.out.println(s.getName() + "," + s.getAge());
              }
          }
      }
      

1.7LinkedHashSet集合概述和特点【应用】

  • LinkedHashSet集合特点

    • 哈希表和链表实现的Set接口,具有可预测的迭代次序
    • 由链表保证元素有序,也就是说元素的存储和取出顺序是一致的
    • 由哈希表保证元素唯一,也就是说没有重复的元素
  • LinkedHashSet集合基本使用

    public class LinkedHashSetDemo {
        public static void main(String[] args) {
            //创建集合对象
            LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>();
    
            //添加元素
            linkedHashSet.add("hello");
            linkedHashSet.add("world");
            linkedHashSet.add("java");
    
            linkedHashSet.add("world");
    
            //遍历集合
            for(String s : linkedHashSet) {
                System.out.println(s);
            }
        }
    }
    

2.Set集合排序

2.1TreeSet集合概述和特点【应用】

  • TreeSet集合概述

    • 元素有序,可以按照一定的规则进行排序,具体排序方式取决于构造方法
      • TreeSet():根据其元素的自然排序进行排序
      • TreeSet(Comparator comparator) :根据指定的比较器进行排序
    • 没有带索引的方法,所以不能使用普通for循环遍历
    • 由于是Set集合,所以不包含重复元素的集合
  • TreeSet集合基本使用

    public class TreeSetDemo01 {
        public static void main(String[] args) {
            //创建集合对象
            TreeSet<Integer> ts = new TreeSet<Integer>();
    
            //添加元素
            ts.add(10);
            ts.add(40);
            ts.add(30);
            ts.add(50);
            ts.add(20);
    
            ts.add(30);
    
            //遍历集合
            for(Integer i : ts) {
                System.out.println(i);
            }
        }
    }
    

2.2自然排序Comparable的使用【应用】

  • 案例需求

    • 存储学生对象并遍历,创建TreeSet集合使用无参构造方法
    • 要求:按照年龄从小到大排序,年龄相同时,按照姓名的字母顺序排序
  • 实现步骤

    • 用TreeSet集合存储自定义对象,无参构造方法使用的是自然排序对元素进行排序的
    • 自然排序,就是让元素所属的类实现Comparable接口,重写compareTo(T o)方法
    • 重写方法时,一定要注意排序规则必须按照要求的主要条件和次要条件来写
  • 代码实现

    • 学生类

      public class Student implements Comparable<Student> {
          private String name;
          private int age;
      
          public Student() {
          }
      
          public Student(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;
          }
      
          @Override
          public int compareTo(Student s) {
      //        return 0;
      //        return 1;
      //        return -1;
              //按照年龄从小到大排序
             int num = this.age - s.age;
      //        int num = s.age - this.age;
              //年龄相同时,按照姓名的字母顺序排序
             int num2 = num==0?this.name.compareTo(s.name):num;
              return num2;
          }
      }
      
    • 测试类

      public class TreeSetDemo02 {
          public static void main(String[] args) {
              //创建集合对象
              TreeSet<Student> ts = new TreeSet<Student>();
      
              //创建学生对象
              Student s1 = new Student("xishi", 29);
              Student s2 = new Student("wangzhaojun", 28);
              Student s3 = new Student("diaochan", 30);
              Student s4 = new Student("yangyuhuan", 33);
      
              Student s5 = new Student("linqingxia",33);
              Student s6 = new Student("linqingxia",33);
      
              //把学生添加到集合
              ts.add(s1);
              ts.add(s2);
              ts.add(s3);
              ts.add(s4);
              ts.add(s5);
              ts.add(s6);
      
              //遍历集合
              for (Student s : ts) {
                  System.out.println(s.getName() + "," + s.getAge());
              }
          }
      }
      

2.3比较器排序Comparator的使用【应用】

  • 案例需求

    • 存储学生对象并遍历,创建TreeSet集合使用带参构造方法
    • 要求:按照年龄从小到大排序,年龄相同时,按照姓名的字母顺序排序
  • 实现步骤

    • 用TreeSet集合存储自定义对象,带参构造方法使用的是比较器排序对元素进行排序的
    • 比较器排序,就是让集合构造方法接收Comparator的实现类对象,重写compare(T o1,T o2)方法
    • 重写方法时,一定要注意排序规则必须按照要求的主要条件和次要条件来写
  • 代码实现

    • 学生类

      public class Student {
          private String name;
          private int age;
      
          public Student() {
          }
      
          public Student(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;
          }
      }
      
    • 测试类

      public class TreeSetDemo {
          public static void main(String[] args) {
              //创建集合对象
              TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
                  @Override
                  public int compare(Student s1, Student s2) {
                      //this.age - s.age
                      //s1,s2
                      int num = s1.getAge() - s2.getAge();
                      int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
                      return num2;
                  }
              });
      
              //创建学生对象
              Student s1 = new Student("xishi", 29);
              Student s2 = new Student("wangzhaojun", 28);
              Student s3 = new Student("diaochan", 30);
              Student s4 = new Student("yangyuhuan", 33);
      
              Student s5 = new Student("linqingxia",33);
              Student s6 = new Student("linqingxia",33);
      
              //把学生添加到集合
              ts.add(s1);
              ts.add(s2);
              ts.add(s3);
              ts.add(s4);
              ts.add(s5);
              ts.add(s6);
      
              //遍历集合
              for (Student s : ts) {
                  System.out.println(s.getName() + "," + s.getAge());
              }
          }
      }
      

2.4成绩排序案例【应用】

  • 案例需求

    • 用TreeSet集合存储多个学生信息(姓名,语文成绩,数学成绩),并遍历该集合
    • 要求:按照总分从高到低出现
  • 代码实现

    • 学生类

      public class Student {
          private String name;
          private int chinese;
          private int math;
      
          public Student() {
          }
      
          public Student(String name, int chinese, int math) {
              this.name = name;
              this.chinese = chinese;
              this.math = math;
          }
      
          public String getName() {
              return name;
          }
      
          public void setName(String name) {
              this.name = name;
          }
      
          public int getChinese() {
              return chinese;
          }
      
          public void setChinese(int chinese) {
              this.chinese = chinese;
          }
      
          public int getMath() {
              return math;
          }
      
          public void setMath(int math) {
              this.math = math;
          }
      
          public int getSum() {
              return this.chinese + this.math;
          }
      }
      
    • 测试类

      public class TreeSetDemo {
          public static void main(String[] args) {
              //创建TreeSet集合对象,通过比较器排序进行排序
              TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
                  @Override
                  public int compare(Student s1, Student s2) {
      //                int num = (s2.getChinese()+s2.getMath())-(s1.getChinese()+s1.getMath());
                      //主要条件
                      int num = s2.getSum() - s1.getSum();
                      //次要条件
                      int num2 = num == 0 ? s1.getChinese() - s2.getChinese() : num;
                      int num3 = num2 == 0 ? s1.getName().compareTo(s2.getName()) : num2;
                      return num3;
                  }
              });
      
              //创建学生对象
              Student s1 = new Student("林青霞", 98, 100);
              Student s2 = new Student("张曼玉", 95, 95);
              Student s3 = new Student("王祖贤", 100, 93);
              Student s4 = new Student("柳岩", 100, 97);
              Student s5 = new Student("风清扬", 98, 98);
      
              Student s6 = new Student("左冷禅", 97, 99);
      //        Student s7 = new Student("左冷禅", 97, 99);
              Student s7 = new Student("赵云", 97, 99);
      
              //把学生对象添加到集合
              ts.add(s1);
              ts.add(s2);
              ts.add(s3);
              ts.add(s4);
              ts.add(s5);
              ts.add(s6);
              ts.add(s7);
      
              //遍历集合
              for (Student s : ts) {
                  System.out.println(s.getName() + "," + s.getChinese() + "," + s.getMath() + "," + s.getSum());
              }
          }
      }
      

2.5不重复的随机数案例【应用】

  • 案例需求

    • 编写一个程序,获取10个1-20之间的随机数,要求随机数不能重复,并在控制台输出
  • 代码实现

    public class SetDemo {
        public static void main(String[] args) {
            //创建Set集合对象
    //        Set<Integer> set = new HashSet<Integer>();
            Set<Integer> set = new TreeSet<Integer>();
    
            //创建随机数对象
            Random r = new Random();
    
            //判断集合的长度是不是小于10
            while (set.size()<10) {
                //产生一个随机数,添加到集合
                int number = r.nextInt(20) + 1;
                set.add(number);
            }
    
            //遍历集合
            for(Integer i : set) {
                System.out.println(i);
            }
        }
    }
    

3.泛型

3.1泛型概述和好处【理解】

  • 泛型概述

    ​ 是JDK5中引入的特性,它提供了编译时类型安全检测机制,该机制允许在编译时检测到非法的类型

    它的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数。一提到参数,最熟悉的就是定义方法时有形参,然后调用此方法时传递实参。那么参数化类型怎么理解呢?顾名思义,就是将类型由原来的具体的类型参数化,然后在使用/调用时传入具体的类型。这种参数类型可以用在类、方法和接口中,分别被称为泛型类、泛型方法、泛型接口

  • 泛型定义格式

    • <类型>:指定一种类型的格式。这里的类型可以看成是形参
    • <类型1,类型2…>:指定多种类型的格式,多种类型之间用逗号隔开。这里的类型可以看成是形参
    • 将来具体调用时候给定的类型可以看成是实参,并且实参的类型只能是引用数据类型
  • 泛型的好处

    • 把运行时期的问题提前到了编译期间
    • 避免了强制类型转换

3.2泛型类【应用】

  • 定义格式

    修饰符 class 类名<类型> {  }
    
  • 示例代码

    • 泛型类

      public class Generic<T> {
          private T t;
      
          public T getT() {
              return t;
          }
      
          public void setT(T t) {
              this.t = t;
          }
      }
      
    • 测试类

      public class GenericDemo {
          public static void main(String[] args) {
              Generic<String> g1 = new Generic<String>();
              g1.setT("林青霞");
              System.out.println(g1.getT());
      
              Generic<Integer> g2 = new Generic<Integer>();
              g2.setT(30);
              System.out.println(g2.getT());
      
              Generic<Boolean> g3 = new Generic<Boolean>();
              g3.setT(true);
              System.out.println(g3.getT());
          }
      }
      

3.3泛型方法【应用】

  • 定义格式

    修饰符 <类型> 返回值类型 方法名(类型 变量名) {  }
    
  • 示例代码

    • 带有泛型方法的类

      public class Generic {
          public <T> void show(T t) {
              System.out.println(t);
          }
      }
      
    • 测试类

      public class GenericDemo {
          public static void main(String[] args) {
      		Generic g = new Generic();
              g.show("林青霞");
              g.show(30);
              g.show(true);
              g.show(12.34);
          }
      }
      

3.4泛型接口【应用】

  • 定义格式

    修饰符 interface 接口名<类型> {  }
    
  • 示例代码

    • 泛型接口

      public interface Generic<T> {
          void show(T t);
      }
      
    • 泛型接口实现类

      public class GenericImpl<T> implements Generic<T> {
          @Override
          public void show(T t) {
              System.out.println(t);
          }
      }
      
    • 测试类

      public class GenericDemo {
          public static void main(String[] args) {
              Generic<String> g1 = new GenericImpl<String>();
              g1.show("林青霞");
      
              Generic<Integer> g2 = new GenericImpl<Integer>();
              g2.show(30);
          }
      }
      

3.5类型通配符【应用】

  • 类型通配符的作用

    ​ 为了表示各种泛型List的父类,可以使用类型通配符

  • 类型通配符的分类

    • 类型通配符:<?>
      • List<?>:表示元素类型未知的List,它的元素可以匹配任何的类型
      • 这种带通配符的List仅表示它是各种泛型List的父类,并不能把元素添加到其中
    • 类型通配符上限:<? extends 类型>
      • List<? extends Number>:它表示的类型是Number或者其子类型
    • 类型通配符下限:<? super 类型>
      • List<? super Number>:它表示的类型是Number或者其父类型
  • 类型通配符的基本使用

    public class GenericDemo {
        public static void main(String[] args) {
            //类型通配符:<?>
            List<?> list1 = new ArrayList<Object>();
            List<?> list2 = new ArrayList<Number>();
            List<?> list3 = new ArrayList<Integer>();
            System.out.println("--------");
    
            //类型通配符上限:<? extends 类型>
    //        List<? extends Number> list4 = new ArrayList<Object>();
            List<? extends Number> list5 = new ArrayList<Number>();
            List<? extends Number> list6 = new ArrayList<Integer>();
            System.out.println("--------");
    
            //类型通配符下限:<? super 类型>
            List<? super Number> list7 = new ArrayList<Object>();
            List<? super Number> list8 = new ArrayList<Number>();
    //        List<? super Number> list9 = new ArrayList<Integer>();
    
        }
    }
    

4.可变参数

4.1可变参数【应用】

  • 可变参数介绍

    ​ 可变参数又称参数个数可变,用作方法的形参出现,那么方法参数个数就是可变的了

  • 可变参数定义格式

    修饰符 返回值类型 方法名(数据类型… 变量名) {  }
    
  • 可变参数的注意事项

    • 这里的变量其实是一个数组
    • 如果一个方法有多个参数,包含可变参数,可变参数要放在最后
  • 可变参数的基本使用

    public class ArgsDemo01 {
        public static void main(String[] args) {
            System.out.println(sum(10, 20));
            System.out.println(sum(10, 20, 30));
            System.out.println(sum(10, 20, 30, 40));
    
            System.out.println(sum(10,20,30,40,50));
            System.out.println(sum(10,20,30,40,50,60));
            System.out.println(sum(10,20,30,40,50,60,70));
            System.out.println(sum(10,20,30,40,50,60,70,80,90,100));
        }
    
    //    public static int sum(int b,int... a) {
    //        return 0;
    //    }
    
        public static int sum(int... a) {
            int sum = 0;
            for(int i : a) {
                sum += i;
            }
            return sum;
        }
    }
    

4.2可变参数的使用【应用】

  • Arrays工具类中有一个静态方法:

    • public static List asList(T… a):返回由指定数组支持的固定大小的列表
    • 返回的集合不能做增删操作,可以做修改操作
  • List接口中有一个静态方法:

    • public static List of(E… elements):返回包含任意数量元素的不可变列表
    • 返回的集合不能做增删改操作
  • Set接口中有一个静态方法:

    • public static Set of(E… elements) :返回一个包含任意数量元素的不可变集合
    • 在给元素的时候,不能给重复的元素
    • 返回的集合不能做增删操作,没有修改的方法
  • 示例代码

    public class ArgsDemo02 {
        public static void main(String[] args) {
            //public static <T> List<T> asList(T... a):返回由指定数组支持的固定大小的列表
    //        List<String> list = Arrays.asList("hello", "world", "java");
    //
            list.add("javaee"); //UnsupportedOperationException
            list.remove("world"); //UnsupportedOperationException
    //        list.set(1,"javaee");
    //
    //        System.out.println(list);
    
            //public static <E> List<E> of(E... elements):返回包含任意数量元素的不可变列表
    //        List<String> list = List.of("hello", "world", "java", "world");
    //
            list.add("javaee");//UnsupportedOperationException
            list.remove("java");//UnsupportedOperationException
            list.set(1,"javaee");//UnsupportedOperationException
    //
    //        System.out.println(list);
    
            //public static <E> Set<E> of(E... elements) :返回一个包含任意数量元素的不可变集合
    //        Set<String> set = Set.of("hello", "world", "java","world"); //IllegalArgumentException
            //Set<String> set = Set.of("hello", "world", "java");
    
    //        set.add("javaee");//UnsupportedOperationException
    //        set.remove("world");//UnsupportedOperationException
    
            //System.out.println(set);
        }
    }
    

请添加图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值