第21天学习Java的笔记-数学工具类Arrays,Math
生活随笔
收集整理的這篇文章主要介紹了
第21天学习Java的笔记-数学工具类Arrays,Math
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
27天!
數(shù)學(xué)工具類
1.1數(shù)組工具類Arrays
package Demo2101;/* 注意事項:是Arrays進(jìn)行調(diào)用,不是變量進(jìn)行調(diào)用!!!* java.util.Arrays是一個與數(shù)組相關(guān)的工具類,里面提供了大量的靜態(tài)方法(不用new,直接.調(diào)用), * 用來實(shí)現(xiàn)數(shù)組的常見操作。 * * public static String toString(數(shù)組):將參數(shù)數(shù)組{元素1,元素2,元素3...}變成字符串(按照默認(rèn)格式:[元素1,元素2,元素3...]) * public static void sort(數(shù)組):按照默認(rèn)升序(從小到大)對數(shù)組的元素進(jìn)行排序。備注: 1.如果是數(shù)值,sort默認(rèn)按照升序從小到大 2。如果是字符串,sort默認(rèn)按照字母升序 3.如果是自定義類型,那么這個自定義的類需要有Comparable或者Comparator接口的支持。(今后學(xué)習(xí))* */import java.util.Arrays;public class Demo01Arrays {public static void main(String[] args) {//Arrays.toStringint[] intArray = {2,3,4,5,6};String string1 = Arrays.toString(intArray);System.out.println(string1);//[2, 3, 4, 5, 6]//Arrays.sortint[] sortArray = {4,2,3,6,1,5};Arrays.sort(sortArray);//沒有返回值,這時候已經(jīng)在原來數(shù)組上對其排好了//將排好的數(shù)組按照默認(rèn)格式輸出System.out.println(Arrays.toString(sortArray));//[1, 2, 3, 4, 5, 6]String[] stringArray = {"aaa","ccc","bbb"};Arrays.sort(stringArray);System.out.println(Arrays.toString(stringArray));//[aaa, bbb, ccc]} }1.2 Arrays:字符串倒序排列
題目:“avhdbjd”–>[a, b, d, d, h, j, v]
package Demo2101;/* * 題目:"avhdbjd"-->[a, b, d, d, h, j, v] * 請使用Arrays相關(guān)的API,將一個隨機(jī)字符串中的所有字符升序排列,并倒序打印 * 關(guān)于這道題有對于toCharArray()的問題: * 關(guān)于該方法的疑問:按理說返回值應(yīng)該是一個數(shù)組{1,2,3},但是返回值只有123字符,沒有數(shù)組格式? * */import java.util.Arrays;public class Demo02ArraysPractice {public static void main(String[] args) {//1.定義一個字符串String str1 = "avhdbjd";//2.轉(zhuǎn)換為字符數(shù)組char[] chars = str1.toCharArray();//System.out.println(chars);//avhdbjd//3.升序排列字符數(shù)組Arrays.sort(chars);Arrays.toString(chars);//此時chars已經(jīng)變成了升序的字符數(shù)組//System.out.println(Arrays.toString(chars));//[a, b, d, d, h, j, v]//4.倒序輸出打印[v, j, h, d, d, b, a]System.out.print("[");for (int i = chars.length - 1; i >= 0 ; i--) {if (i != 0) {System.out.print(chars[i] + ", ");//注意逗號后面有空格}else {System.out.print(chars[i] + "]");}}} }2.1 數(shù)學(xué)工具類Math
package Demo2101;/* * java.util.Math類是數(shù)學(xué)相關(guān)的工具類,里面提供了大量的靜態(tài)方法,完成與數(shù)學(xué)運(yùn)算的相關(guān)操作。 * * public static double abs(double num):獲取絕對值 * public static double ceil(double num):向上取整,所謂的向上,是靠數(shù)軸的右邊取值。-10.9———》-10 * public static double floor(double num):向下取整 * public static round(double num):四舍五入 * * Math.PI代表圓周率的近似值(double) * */public class Demo03Math {public static void main(String[] args) {//1.獲取絕對值System.out.println(Math.abs(-3));//3//2.向上取整System.out.println(Math.ceil(3.4));//4.0System.out.println(Math.ceil(34));//34.0//3.向下取整System.out.println(Math.floor(3.4));//3.0System.out.println(Math.floor(34));//34.0//4.四舍五入System.out.println(Math.round(3.4));//3System.out.println(Math.round(34));//34} }2.2 Math:小學(xué)數(shù)學(xué)真題
計算在-10.8到5.9之間,絕對值大于6或者絕對值小于2.1的整數(shù)有多少個?
package Demo2101;/* 自增運(yùn)算符也可以用于double中!! * 題目: * 計算在-10.8到5.9之間,絕對值大于6或者絕對值小于2.1的整數(shù)有多少個? * * 思路: * 1.與操作運(yùn)算符,兩者條件滿足其一則增加1 * */public class Demo04MathPractice {public static void main(String[] args) {int num = 0;for (double i = -10.8; i <= 5.9 ; i++) {if (Math.abs(i) > 6.0 || Math.abs(i) < 2.1){num++;}}System.out.println("絕對值大于6或者小于2.1的整數(shù)有:" + num + "個");//絕對值大于6或者小于2.1的整數(shù)有:9個} } 與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的第21天学习Java的笔记-数学工具类Arrays,Math的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第20天学习Java的笔记-static
- 下一篇: 第22天学习Java的笔记-继承