• 实用类以及枚举


    枚举

            枚举指由一组固定的常量组成的类型

            特点:

                    类型安全

                    易于输入

                    代码清晰

    常用JavaAPI查找相关知识

    包装类

            

            包装类把基本类型数据转换为对象

                    每个基本类型在java.lang包中都有一个相应的包装类

            包装类的作用

                    提供了一系列实用的方法 集合不允许存放基本数据类型数据,存放数字时,要用包装类型 

    所有包装类都可将与之对应的基本数据类型作为参数,来构造它们的实例

    除Character类外,其他包装类可将一个字符串作为参数构造它们的实例

    注意事项

            Boolean类构造方法参数为String类型时,若该字符串内容为true(不考虑大小写),则该Boolean对象表示true,否则表示false

            当Number包装类构造方法参数为String 类型时,字符串不能为null,且该字符串必须可解析为相应的基本数据类型的数据,否则编译不通过,运行时会抛出NumberFormatException异常

    包装类的常用方法

    XXXValue():包装类转换成基本类型

    toString():以字符串形式返回包装对象表示的基本类型数据(基本类型->字符串)

    parseXXX():把字符串转换为相应的基本数据类型数据(Character除外)(字符串->基本类型)

    valueOf() 所有包装类都有如下方法(基本类型->包装类)public static Type valueOf(type value)

    valueOf():除Character类外,其他包装类都有如下方法(字符串->包装类)public static Type valueOf(String s)

    1. package demo01;
    2. public class Demo01 {
    3. public static void main(String[] args) {
    4. //包装类常用方法:XXXValue():包装类转换成基本类型
    5. Byte byte1 = new Byte("12");
    6. byte num1=byte1.byteValue();
    7. System.out.println(num1);
    8. Boolean bool = new Boolean("True");
    9. boolean num2 =bool.booleanValue();
    10. System.out.println(num2);
    11. Character ch = new Character('a');
    12. char num3 =ch.charValue();
    13. System.out.println(num3);
    14. //toString():以字符串形式返回包装对象表示的基本类型数据(基本类型->字符串)
    15. byte num4 = 10;
    16. String str1 = Byte.toString(num4);
    17. System.out.println(str1);
    18. String str2 = Boolean.toString(false);
    19. System.out.println(str2);
    20. //parseXXX():把字符串转换为相应的基本数据类型数据(Character除外)(字符串->基本类型)
    21. int num5 = Integer.parseInt("30");
    22. System.out.println(num5);
    23. boolean num6 =Boolean.parseBoolean("qwe");
    24. System.out.println(num6);
    25. //valueOf():所有包装类都有如下方法(基本类型->包装类)public static Type valueOf(type value)
    26. Integer int1 = Integer.valueOf(100);
    27. System.out.println(int1);
    28. //valueOf():除Character类外,其他包装类都有如下方法(字符串->包装类)public static Type valueOf(String s)
    29. Long long1 =Long.valueOf("200");
    30. System.out.println(long1);
    31. }
    32. }

    装箱和拆箱 

            装箱和拆箱:实现的是基本数据类型和包装类类型之间的自动转换

            装箱:基本数据类型直接赋值给包装类类型的对象

            拆箱:包装类类型数据直接赋值给基本类型的变量

    1. package demo01;
    2. public class Demo02 {
    3. public static void main(String[] args) {
    4. byte num1 =10;
    5. //Byte byte1 =num1;
    6. //装箱:基本数据类型直接赋值给包装类类型的对象
    7. Byte byte1 = num1;
    8. //拆箱:包装类类型数据直接赋值给基本类型的变量
    9. Integer int1 = new Integer("123");
    10. int num2 =int1;
    11. Integer int2 =new Integer("255");
    12. int num3 =300;
    13. int result =int2+num3;
    14. Integer int3 =int2 +num3;
    15. }
    16. }

            包装类的特点

                    JDK1.5后,允许基本数据类型和包装类型进行混合数学运算

                            包装类并不是用来取代基本数据类型的

                    在基本数据类型需要用对象表示时使用

     Math类

          Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。

    Random类

            生成随机数的其他方式

    1. package demo02;
    2. public class MathDemo01 {
    3. public static void main(String[] args) {
    4. //Math类:Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
    5. double pi =Math.PI;
    6. System.out.println(pi);
    7. System.out.println(Math.max(100, 200));
    8. System.out.println(Math.min(99, 199));
    9. System.out.println(Math.abs(-10));
    10. System.out.println(Math.floor(9.8));//返回最大的(最接近正无穷大)double 值,该值小于等于参数,并等于某个整数。
    11. System.out.println(Math.ceil(9.1));//返回最小的(最接近负无穷大)double 值,该值大于等于参数,并等于某个整数。
    12. System.out.println(Math.round(10.5));//返回最接近参数的 int。
    13. //随机数方法:Math.random():随机返回一个介于0.0(包括)~1.0(不包括)之间的double类型的数据
    14. double num1 = Math.random();
    15. System.out.println(num1);
    16. /*随机返回一个[num1,num2)之间的int类型的整数(num2>num1)
    17. * int num = (int)(Math.random()*(num2-num1)+num1);
    18. */
    19. }
    20. }

    String类 

    String类位于java.lang包中,具有丰富的方法

            eg:计算字符串的长度、比较字符串、连接字符串、提取字符串

    常用方法

    length()方法,确定字符串的长度

     总结:
               获取数组长度:数组名.lenght
               获取集合长度:集合名.size()
               获取字符串长度:字符串对象名.length()

    equals( )方法,比较存储在两个字符串对象的内容是否一致

    字符串比较的其他方法

            使用equalsIgnoreCase()方法

            使用toLowerCase()方法

            使用toUpperCase()方法

    1. package demo03;
    2. public class StringDemo01 {
    3. public static void main(String[] args) {
    4. String string1 = "qwertyuiop";
    5. //获取字符串的长度
    6. int length = string1.length();
    7. System.out.println("字符串长度:"+length);
    8. //比较两个字符串的内容是否相同
    9. String str1 = "qwe";
    10. String str2 = "qwe";
    11. System.out.println(str1.equals(str2));//true
    12. System.out.println(str1==str2);//true
    13. String str3 = new String("abc");
    14. String str4 = new String("abc");
    15. System.out.println(str3.equals(str4));//true
    16. System.out.println(str3==str4);//false
    17. //其他比较字符串的方法
    18. String str5 ="qwert";
    19. String str6 = "QWERT";
    20. System.out.println(str5.equals(str6));
    21. //equalsIgnoreCase()不区分大小写比较
    22. System.out.println(str5.equalsIgnoreCase(str6));//true
    23. String result = str6.toLowerCase();
    24. System.out.println(str6);//QWERT //字符串对象调用方法后,不会改变自身的内容,相当于是对它的复制品进行了修改
    25. System.out.println(result);//qwert
    26. System.out.println(str5.toUpperCase());
    27. }
    28. }

    字符串连接


            方法1:使用“+”

            方法2:使用String类的concat()方法

     

    1. package demo03;
    2. public class StringDemo02 {
    3. public static void main(String[] args) {
    4. System.out.println("qwert");
    5. int num = 10;
    6. System.out.println(num);
    7. //+是一个连接符,连接变量与字符串的值
    8. System.out.println("num里面存储的值是:"+num);
    9. System.out.println("我的班级是:"+"Java2217");//我的班机是:Java2217
    10. System.out.println(10+20+"good");//30good
    11. System.out.println(10+"good"+20);//10good20
    12. System.out.println("good"+10+20);//good1020
    13. String str1 ="qwert";
    14. String str2 ="yuiop";
    15. String result = str1.concat(str2);
    16. System.out.println(str1);//qwert
    17. System.out.println(str2);//yuiop
    18. System.out.println(result);//qwertyuiop
    19. }
    20. }

    字符串常用提取方法

            public int indexOf(int ch) 搜索第一个出现的字符ch(或字符串value),如果没有找到,返回-1
            public int indexOf(String value) 搜索第一个出现的字符ch(或字符串value),如果没有找到,返回-1 

            public int lastIndexOf(int ch) 搜索最后一个出现的字符ch(或字符串value),如果没有找到,返回-1
            public int lastIndexOf(String value)搜索最后一个出现的字符ch(或字符串value),如果没有找到,返回-1

            public String substring(int index)提取从位置索引开始的字符串到末尾部分

            public String substring(int beginindex, int endindex)提取beginindex(包括)和endindex(不包括)之间的字符串部分

            public String trim()返回一个前后不含任何空格的调用字符串的副本

    1. package demo03;
    2. import java.util.Arrays;
    3. public class StringDemo03 {
    4. public static void main(String[] args) {
    5. //字符串常用提取方法
    6. String str1 ="abcdefghijklmnabcd";
    7. System.out.println(str1.length());//14
    8. //public int indexOf(int ch) 搜索第一个出现的字符ch(或字符串value),如果没有找到,返回-1
    9. //public int indexOf(String value) 搜索第一个出现的字符ch(或字符串value),如果没有找到,返回-1
    10. int index = str1.indexOf(110);
    11. System.out.println(index);
    12. System.out.println(str1.indexOf("bc"));
    13. //public int lastIndexOf(int ch) 搜索最后一个出现的字符ch(或字符串value),如果没有找到,返回-1
    14. //public int lastIndexOf(String value)搜索最后一个出现的字符ch(或字符串value),如果没有找到,返回-1
    15. System.out.println(str1.lastIndexOf(97));
    16. System.out.println(str1.lastIndexOf("bcd"));
    17. //public String substring(int index)提取从位置索引开始的字符串到末尾部分
    18. System.out.println(str1);
    19. System.out.println(str1.substring(3));
    20. //public String substring(int beginindex, int endindex)提取beginindex(包括)和endindex(不包括)之间的字符串部分
    21. System.out.println(str1);
    22. System.out.println(str1.substring(3, 6));
    23. //public String trim()返回一个前后不含任何空格的调用字符串的副本
    24. String str2 =" qwert yuiop ";
    25. System.out.println(str2);
    26. System.out.println(str2.trim());
    27. //字符串拆分方法
    28. String str3 ="长亭外 古道边 芳草碧连天 晚风拂 柳笛声残 夕阳山外山";
    29. String[] strs =str3.split(" ");
    30. for (String string : strs) {
    31. System.out.println(string);
    32. }
    33. String str4 ="我爱你但是你不爱我可是我依然爱你";
    34. String[] strs2 =str4.split("爱");
    35. System.out.println(Arrays.toString(strs2));
    36. //判断字符串是否以指定内容结尾
    37. String str5 ="HelloWorld.java";
    38. System.out.println(str5.endsWith(".java"));
    39. //判断字符串是否以执行内容开头
    40. System.out.println(str5.startsWith("He"));
    41. //将字符串变成byte类型的数组
    42. byte[] bytes =str5.getBytes();
    43. for (byte b : bytes) {
    44. System.out.println((char)b);
    45. }
    46. }
    47. }

    StringBuffer类

    对字符串频繁修改(如字符串连接)时,使用StringBuffer类可以大大提高程序执行效率

            String是不可变对象

                    经常改变内容的字符串最好不要使用String

                    StringBuffer是可变的字符串

                    字符串经常改变的情况可使用StringBuffer,更高效

                    JDK5.0后提供了StringBuilder,等价StringBuffer

    1. package demo04;
    2. public class StringBufferDemo01 {
    3. public static void main(String[] args) {
    4. String str1 ="qwertyuiop";
    5. StringBuffer sb1 = new StringBuffer("qwert");
    6. //String类对象调用方法之后,对原来String对象中的内容没有影响
    7. System.out.println(str1);
    8. System.out.println(str1.substring(2));
    9. System.out.println(str1);
    10. System.out.println("-------------------");
    11. //StringBuffer类对象调用方法返回结果还是Stringuffer的,这时候会改变StringBuffer对象里的内容
    12. System.out.println(sb1);
    13. sb1.substring(2);
    14. System.out.println(sb1.insert(2, "qwe"));
    15. System.out.println(sb1);
    16. System.out.println(sb1.toString());
    17. //在末尾追加内容
    18. System.out.println(sb1);
    19. System.out.println(sb1.append("asdfghjkl"));
    20. System.out.println(sb1);
    21. }
    22. }

    Date类 

     

    1. package demo05;
    2. import java.text.SimpleDateFormat;
    3. import java.util.Date;
    4. public class DataDemo01 {
    5. public static void main(String[] args) {
    6. //创建Date类对象
    7. Date date = new Date();
    8. System.out.println(date);
    9. //Date类中很多方法都已经过时,使用Calender类替代
    10. // System.out.println(date.getYear()+1900);
    11. // System.out.println(date.getMonth()+1);
    12. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    13. String str = sdf.format(date);
    14. System.out.println(str);
    15. }
    16. }

    Calendar类

            抽象类,java.util.Calendar

    Calendar是一个抽象类,不能直接实例化,可以通过该类中一个静态方法创建其引用

    用于设置和获取日期/时间数据

    1. package demo06;
    2. import java.util.Calendar;
    3. public class CalendarDemo01 {
    4. public static void main(String[] args) {
    5. Calendar cal =Calendar.getInstance();
    6. //获取年
    7. int year = cal.get(Calendar.YEAR);
    8. System.out.println(year);
    9. //获取月
    10. int month = cal.get(Calendar.MONTH);
    11. System.out.println(month);
    12. //获取日
    13. int date = cal.get(Calendar.DATE);
    14. System.out.println(date);
    15. //获取时、分、秒
    16. System.out.println(cal.get(Calendar.HOUR)+":"+cal.get(Calendar.MINUTE)+":"+cal.get(Calendar.SECOND));
    17. //获取星期
    18. int day_of_week = cal.get(Calendar.DAY_OF_WEEK);
    19. System.out.println(day_of_week);
    20. //获取当前日期是这一年中的第多少天
    21. System.out.println(cal.get(Calendar.DAY_OF_YEAR));
    22. }
    23. }
  • 相关阅读:
    Simulink仿真封装中的参数个对话框设置
    Python获取Pandas列名的几种方法(含代码示例)
    IT运维:使用数据分析平台监控Windows Eventlog
    Windows 11 Beta 预览版用户已经可以体验 Android 应用
    FPGA工程师面试试题集锦51~60
    c语言中的字符函数
    【CSDN 每日一练 ★☆☆】【蛮力/双指针】删除排序链表中的重复元素
    Shell编程三剑客之sed
    [Linux系统编程]_进程(二)
    机器学习基础:用 Lasso 做特征选择
  • 原文地址:https://blog.csdn.net/qq_51810428/article/details/126352422