• java学习(Arrays类和System类)


    目录

    目录

    一.Arrays类

    二.System常见方法

    三、Biglnteger和BigDecimal(高精度)

    1.Biglnter的常用方法

    2.BigDecimal常见方法

    3.日期类

    1)第一代日期类

    2)第二代日期类

    3)第三代日期类



    一.Arrays类

    Arrays包含了一系

    静态方法,用于管理或操作数组(比如排序和搜索)

    1. Integer[] s={1,2,3};
    2. //1.Arrays.toString方法,遍历数组
    3. //2.Arrays.sort
    4. Arrays.sort(s);//默认排序(从小到大排序)
    5. //定制排序(类似于c++的sort(a,a+n,cmp),自行定义排序规则
    6. Arrays.sort(s, new Comparator() {
    7. @Override
    8. public int compare(Integer o1, Integer o2) {
    9. Integer n1=(Integer)o1;
    10. Integer n2=(Integer)o2;
    11. return n2-n1;
    12. }
    13. });
    14. //System.out.println(Arrays.toString(s));
    15. //3.binarySearch通过二分搜索法进行查找,要求必须排好序
    16. //注:如果在数组中找不到该元素,则返回return-(low+1)
    17. //low表示的是该元素本该在数组中的位置
    18. //例如在{1,2,5,8}中找6,那它的位置本应该是5后面,8前面,即low为3.
    19. //4.copyOf(数组元素的复制)
    20. //若拷贝的长度大于s.length,新数组就相当于在原数组后面加null(对于Integer类的数组来说)
    21. Integer[] s1=Arrays.copyOf(s,s.length);//将s数组的s.length个元素复制到s1中
    22. //5.fill(数组的填充)
    23. Arrays.fill(s,100);//将s数组中的所有元素全部填充为100.
    24. //6.equals(比较两个数组元素内容是否一致)Arrays.equals(数组1,数组2)返回的是boolean类型
    25. //asLIst(将一组值转换为list)
    26. List asList=Arrays.asList(2,5,6,8);

    二.System常见方法

      1. //1.exit 退出当前程序
      2. //System.exit(0);//括号中的数字表示一个状态,0表示正常的状态
      3. //2.arraycopy:复制数组元素,比较适合底层调用,一般使用Arrays.copyOf完成复制数组
      4. int[] s1={1,2,3};
      5. int[] s2=new int[3];
      6. //其含义为从s1数组索引为零的位置开始拷贝3个字符到s2数组(从s2数组的索引为0的位置开始)
      7. //拷贝的长度不能大于s2的长度,否则程序会报错
      8. System.arraycopy(s1,0,s2,0,3);
      9. System.out.println(Arrays.toString(s2));
      10. //3.currentTimeMillens (返回当前时间距离1970-1-1的毫秒数)
      11. System.out.println(System.currentTimeMillis());
      12. //4.gc(运行垃圾回收机制)
      13. System.gc();

    三、Biglnteger和BigDecimal(高精度)

    1.Biglnter的常用方法

    1. BigInteger bigInteger = new BigInteger("999999999999999999999");
    2. BigInteger bigInteger1 = new BigInteger("111111111111");
    3. //加
    4. BigInteger add=bigInteger.add(bigInteger1);
    5. //减
    6. BigInteger subtract=bigInteger.subtract(bigInteger1);
    7. //乘
    8. BigInteger multiply=bigInteger.multiply(bigInteger1);
    9. //除
    10. BigInteger divide=bigInteger.divide(bigInteger1);

    2.BigDecimal常见方法

    1. BigDecimal bigDecimal = new BigDecimal("1.1111111111111111111");
    2. BigDecimal bigDecimal1 = new BigDecimal("0.111111111");
    3. //进行加、减、乘运算时同BigInteger相同
    4. //进行除法运算时,要注意结果可能会出现无限循环小数的结果。
    5. //因此在小数进行除法运算时,应设置精度
    6. //如果结果是无限循环小数,就会保留被除数的精度.
    7. BigDecimal s1=bigDecimal.divide(bigDecimal1,BigDecimal.ROUND_CEILING);

    3.日期类

    1)第一代日期类

    1. Date d=new Date();//获得当前系统时间
    2. //通过指定毫秒数得到时间
    3. System.out.println(d);
    4. //输出形式为Sun Mar 10 10:00:29 CST 2024
    5. Date d1=new Date(344567443);
    6. SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss E");
    7. String fun=sdf.format(d);//将日期转换成指定格式的字符串.
    8. //将一个格式化的String转换成对应的 Date
    9. String s1="2024年01月01日 10:20:30 星期一";
    10. Date parse= sdf.parse(s1);//这里有一个转换异常,可以使用throws抛出。

    2)第二代日期类

    1. //Calender是一个抽象类,构造器的属性为private
    2. Calendar n=Calendar.getInstance();//通过getInstance获取实例
    3. // 以下是获取日历对象的某个日历字段
    4. int year=n.get(Calendar.YEAR);//年
    5. //由于Calendar在返回月份时,是按照0开始编号,所以需要在后面加上1
    6. int month=n.get(Calendar.MONTH+1);//月
    7. int day=n.get(Calendar.DAY_OF_MONTH);//日
    8. int hour=n.get(Calendar.HOUR_OF_DAY);//小时
    9. int min=n.get(Calendar.MINUTE);//分钟
    10. int second=n.get(Calendar.SECOND);//秒
    11. //Calendar不同于date,没有专门的格式化的方法,我们自己来设置

    3)第三代日期类

    ---LocalDate:只包含日期,可以获取日期字段

    ---LocalTime:只包含时间,可以获取时间字段

    ---LocalDateTime包含日期+时间,可以获取日期和时间字段.

    1. //LocalTime和LocalDateTime使用方法同LocalDate相似.
    2. LocalDate n1= LocalDate.now();//获取当前时间
    3. System.out.println(n1.getYear());
    4. System.out.println(n1.getMonth());
    5. System.out.println(n1.getDayOfWeek());

    DateTimeFormatter格式日期类

    1. LocalDateTime fun=LocalDateTime.now();
    2. System.out.println(fun);//输出结果为 2024-03-10T10:02:08.822
    3. //类似于第一代日期类的SimpleDateFormatter
    4. //DateTimeFormatter
    5. //先定义一个标准格式
    6. DateTimeFormatter n=DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH小时mm分钟ss秒");
    7. String s=n.format(fun);

    Instant时间戳

  • 相关阅读:
    PE结构学习(2)_PE结构的组成
    杭电多校-Darnassus-(最小生成树本质+预处理+链式向前星)
    安全基础 --- MySQL数据库的《锁》解析
    Scala 访问修饰符
    常用短信平台一览,记得收藏哦
    计算机毕业设计 大学生选修选课系统的设计与实现 Javaweb项目 Java实战项目 前后端分离 文档报告 代码讲解 安装调试
    大数据开发(Hadoop面试真题-卷三)
    [攻防世界]BABYRE
    web前端期末大作业——贵州山地旅游介绍网页1页 HTML旅游网站设计与实现
    Transformer 综述 & Transformers in Vision: A Survey
  • 原文地址:https://blog.csdn.net/2301_79721847/article/details/136588852