java中Arrays类和Math类常用API简介
生活随笔
收集整理的這篇文章主要介紹了
java中Arrays类和Math类常用API简介
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Arrays類:
java.util.Arrays是一個與數組相關的工具類,提供了許多靜態方法對數組進行操作,直接通過類點出來使用,無需創建對象。
// 1.Arrays類存在與java.util下,需要導包: import java.util.Arrays; public class ArraysMethod{public static void main(String[] args){// 2.Arrays下toString(數組)方法可以將數組轉換為字符串格式,如:int[] arrInt = {1,3,5,2,4};String arrStr = Arrays.toString(arrInt);System.out.println(arrStr);//[1, 3, 5, 2, 4],數組無法直接打印,這里打印的是字符串// 3.Arrays下sort方法可以對數組元素進行正序排序:數字按照從小到大,字母按照從前到后,字符串按照字節碼從小到大,如:int[] array = {1,4,3,2,5};Arrays.sort(array);//sort方法返回值為void類型,可以直接打印原數組即可,如:String arrSortStr = Arrays.toString(array);//轉換為字符串后在打印System.out.println(arrSortStr);//[1, 2, 3, 4, 5]}; }Math類:
Math類存在于java.lang下,適用于基本的數學運算,可以不用創建對象直接使用靜態方法即可,如:
// 1.導包:存在java.lang下的包可以省略導包步驟:import java.lang.Math; public class MathTest{public static void main(String[] args){// 2-1:Math.abs(number),用于取number的絕對值,如:System.out.println(Math.abs(-2));//2System.out.println(Math.abs(-2.5));//2.5System.out.println(Math.abs(2.5));//2.5System.out.println(Math.abs(2));//2// 2-2:Math.ceil(number),用于number向上取整,如:System.out.println(Math.ceil(5.2));//6.0System.out.println(Math.ceil(-5.2));//-5.0System.out.println(Math.ceil(-5));//-5.0System.out.println(Math.ceil(5));//5.0System.out.println(Math.ceil(5.8));//6.0System.out.println(Math.ceil(-5.8));//-5.0// 2-3:Math.floor(number),用于number向下取整,如:System.out.println(Math.floor(2.8));//2.0System.out.println(Math.floor(-2.8));//-3.0System.out.println(Math.floor(3));//3.0System.out.println(Math.floor(-3));//-3.0System.out.println(Math.floor(6.2));//6.0System.out.println(Math.floor(-6.2));//-7.0// 2-4:Math.round(number),用于number四舍五入,如:System.out.println(Math.round(2.3));//2System.out.println(Math.round(-2.3));//-2System.out.println(Math.round(3.6));//4System.out.println(Math.round(-3.6));//-4System.out.println(Math.round(5));//5System.out.println(Math.round(-5));//-5// 2-5:Math.PI,一個接近圓周率π的常量值,System.out.println(Math.PI);//3.141592653589793// 2-6:Math.max(num1,num2),返回num1和num2中較大的值,如:System.out.println(Math.max(2,6));//6System.out.println(Math.max(2,-6));//2System.out.println(Math.max(-2,-6));//-2System.out.println(Math.max(-2.1,-6.1));//-2.1// 2-7:Math.min(num1,num2),返回num1和num2中較小的值,如:System.out.println(Math.min(2,6));//2System.out.println(Math.min(2,-6));//-6System.out.println(Math.min(-2,-6));//-6System.out.println(Math.min(-2.1,-6.1));//-6.1// 更過方法請到官方文檔查閱。} }提示:本文圖片等素材來源于網絡,若有侵權,請發郵件至郵箱:810665436@qq.com聯系筆者刪除。
筆者:苦海
總結
以上是生活随笔為你收集整理的java中Arrays类和Math类常用API简介的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java中关键字、标识符、常量、变量、数
- 下一篇: 小程序向java后台发送图片_微信小程序