Java學習記錄(補充七:一些工具類)

來源:互聯網
上載者:User

標籤:last   size   對象   abc   substring   imp   ext   scl   util   

Math類
package Box;import java.util.ArrayList;import java.util.Collections;import java.util.List;
public class TestBox { public static void main(String[] args) { //八個封裝類 /*Integer i=5;//在棧裡面開闢了一個記憶體空間(基礎資料型別 (Elementary Data Type)) Integer n = new Integer(5);//用new在堆裡面開闢了一個記憶體空間(對象) int m = 5; Byte b = new Byte("1"); Boolean bool=new Boolean(true); Long lon = new Long(1000); Short s = new Short("5"); Double d = new Double(100.0); Float f = new Float(66.00); Character c = new Character(‘c‘); */ //Math類 System.out.println(Math.ceil(6.9));//向上取整 System.out.println(Math.floor(6.9));//向下 System.out.println(Math.round(6.3));//四捨五入 System.out.println(Math.pow(8, 3));//求次冪 double r = 5.5; double s = Math.PI*Math.pow(r, 2); System.out.println(s); //產生隨機數 List<Integer> list = new ArrayList<>(50); for (int i = 0; i < 50; i++) { list.add(i); } Collections.shuffle(list);//collections工具類的一個方法shuffle() list.forEach(System.out::println); }}

結果圖:

 

String類
package Box;
public class StringTest { public static void main(String[] args) { String s = " abfd";//String不是基礎資料型別 (Elementary Data Type),是非常特殊的一個類 String s1 = new String("assd"); //trim()去掉字串"兩端"的空格(中間的不能去除) System.out.println(s.length()); System.out.println(s.trim().length()); //substring:截取字串 String s2 = "再來一瓶"; System.out.println(s2.substring(2)); System.out.println(s2.substring(1, 2)); //indexOf:找到子字串在當前字元中的首字母的索引,找不到就返回-1 String s3 = "123ab45ab67"; System.out.println(s3.indexOf("ab")); System.out.println(s3.indexOf(97));//97為Ascll碼 System.out.println(s3.indexOf("ab", 5)); //找到字串在當前字串中最後出現的索引位置 String s4 = "123321"; System.out.println(s4.lastIndexOf("2")); //replace 替換指定字元 String s5 = "a b c d "; System.out.println(s5.replace(" ","")); System.out.println("*****************************************"); //split 按指定格式分割字串,返回字串數組 String s6 = "a b c d ef "; String []s7 = s6.split(" "); for(String str:s7){ System.out.println(str); } String s8 = "abcdbdfg"; System.out.println(s8.charAt(1));//超過會報錯 }}

結果圖:

 

StingBuffer 和 StringBuilder(注意兩者使用的區別)
package Box;
//String是常量 StringBuffer是變數(頻繁的拼接需要用到)public class StringBufferTest { public static void main(String[] args) { StringBuffer sb = new StringBuffer("abc");//安全性高 sb.append("de"); sb.append("fg"); String s9 = sb.toString(); System.out.println(s9); StringBuilder sb1 = new StringBuilder("abd");//速度快但安全性低(可以 // 自己加鎖) sb1.append("de"); String s10 = sb1.toString(); System.out.println(s10); }}

結果圖:

 

Radom類
public
class RandomTest { public static void main(String[] args) { Random random = new Random(); int n=random.nextInt(10); System.out.println(n); boolean bool=random.nextBoolean(); System.out.println(bool); for(int i = 0;i<10;i++){ double n1 = random.nextDouble(); System.out.println(n1*9+1); } for(int i = 0;i<10;i++){ boolean b1 = random.nextBoolean(); System.out.println(b1); } }}

結果圖:

Java學習記錄(補充七:一些工具類)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.