当前位置: 代码迷 >> 综合 >> 【Java知识】第九章 Arrays类和Math类
  详细解决方案

【Java知识】第九章 Arrays类和Math类

热度:45   发布时间:2024-02-07 09:34:52.0

第九章 Arrays类和Math类

Arrays类

是一个与数组相关的类,里面提供大量静态方法,用来实现数组常见的操作

1)toString方法;2)sort方法

public static void main(String[] args) {int[] array={1,4,2,6,5};//toString()方法String s = Arrays.toString(array);System.out.println(s);//sort()方法Arrays.sort(array);System.out.println(Arrays.toString(array));}

结果:

[1, 4, 2, 6, 5]
[1, 2, 4, 5, 6]

 Math类

是一个与数学相关的类,里面提供大量静态方法,用来实现一些数学常见的操作

public static void main(String[] args) {float a=-1.234F;System.out.println("原数="+a);//abs()取绝对值float absA = Math.abs(a);System.out.println("abs="+absA);System.out.println("===================");//ceil()向上取整float b=1.45F;System.out.println("原数="+b);double ceil = Math.ceil(b);System.out.println("ceil="+ceil);System.out.println("===================");//floor()向下取整double floor = Math.floor(b);System.out.println("原数="+b);System.out.println("floor="+floor);System.out.println("===================");//round()四舍五入int round = Math.round(b);System.out.println("原数="+b);System.out.println("round="+round);}

结果:

原数=-1.234
abs=1.234
===================
原数=1.45
ceil=2.0
===================
原数=1.45
floor=1.0
===================
原数=1.45
round=1