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;//在栈里面开辟了一个内存空间(基本数据类型) 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不是基本数据类型,是非常特殊的一个类 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); } } }
结果图: