Java 常用实现类

Java 常用实现类

一、Math

字段类型功能
PIstatic double比任何其他值都更接近 pi(即圆的周长与直径之比)的 double 值。
方法方法类型功能
random()static double返回[0, 1)之间的小数。
abs(double a)
abs(float a)
abs(int a)
abs(long a)
static double
static float
static int
static long
返回 double 值的绝对值。
返回 float 值的绝对值。
返回 int 值的绝对值。
返回 long 值的绝对值。
ceil(double a)static double返回最小的(最接近负无穷大)double 值,该值大于等于参数,并等于某个整数。
floor(double a)static double返回最大的(最接近正无穷大)double 值,该值小于等于参数,并等于某个整数。
round(float a)
round(double a)
static int
static long
返回最接近参数的 int
返回最接近参数的 long
max(int a, int b)
max(long a, long b)
max(float a, float b)
max(double a, double b)
static int
static long
static float
static double
返回两个 int 值中较大的一个。
返回两个 long 值中较大的一个。
返回两个 float 值中较大的一个。
返回两个 double 值中较大的一个。
min(int a, int b)
min(long a, long b)
min(float a, float b)
min(double a, double b)
static int
static long
static float
static double
返回两个 int 值中较小的一个。
返回两个 long 值中较小的一个。
返回两个 float 值中较小的一个。
返回两个 double 值中较小的一个。
pow(double a, double b)static double返回第一个参数的第二个参数次幂的值
public class MathDemo {public static void main(String[] args) {double num =Math.random();System.out.println(num);//随机获取一个[num1,num2ran)之间的整数// int num =(int)(Math.random()*(num2-num1)+num1);double pi=Math.PI;System.out.println(pi);System.out.println(Math.abs(-35));//ceil(double a):返回一个比a大的离a最近的整数System.out.println(Math.ceil(-3.4));//floor(double a):返回一个比a小的离a最近的整数System.out.println(Math.floor(3.9));//round():根据四色五入的原则返回数据System.out.println(Math.round(3.5));System.out.println(Math.max(12, 25));System.out.println(Math.min(56, 23));//pow(double a,double b):返回a的b次方结果System.out.println(Math.pow(3, 4));}
}

二、Ramdom

Random rand=new Random(); //创建一个Random对象
for(int i=0;i<20;i++){//随机生成20个随机整数,并显示int num=rand.nextInt(10);//返回下一个伪随机数,整型的   	System.out.println("第"+(i+1)+"个随机数是:"+num);
} 

三、String

方法功能
length()获取字符串的长度。
equals()比较两个字符串的内容是否相同,英文字母区分大小写。
equalsIgnoreCase()比较两个字符串的内容是否相同,英文字母不区分大小写。
toLowerCase()将大写英文字母转换为小写。
toUpperCase()将小写英文字母转换为大写。
concat()连接两个字符串。
public class StringDemo01 {public static void main(String[] args) {String str = "abcdefgabcqwerabdkg";// length():获取字符串的长度System.out.println(str.length());String pwd = "aidsjfksj";if (pwd.length() < 6 || pwd.length() > 18) {System.out.println("密码长度应该在6-18之间,请重新输入");}System.out.println("------------------");// equals():比较两个字符串的内容是否相同,英文字母区分大小写String str1 = "abcdefg";String str2 = "abcdefG";System.out.println(str1.equals(str2));// false// equalsIgnoreCase():比较两个字符串的内容是否相同,英文字母不区分大小写System.out.println(str1.equalsIgnoreCase(str2));// true// toLowerCase()方法:将大写英文字母转换为小写// toUpperCase()方法:将小写英文字母转换为大写String str3 = "ABCDqwert";System.out.println(str3.toLowerCase());// abcdqwertSystem.out.println(str3.toUpperCase());// ABCDQWERT// 字符串的连接:+ concat()String str4 = "大湖名城";String str5 = "创新高低";System.out.println(str4+","+str5);String result = str4.concat(str5);System.out.println(result);}
}
方法功能
indexOf(int ch)搜索第一个出现的字符ch(或字符串value),如果没有找到,返回-1。
indexOf(String value)搜索第一个出现的字符ch(或字符串value),如果没有找到,返回-1。
lastIndexOf(int ch)搜索最后一个出现的字符ch(或字符串value),如果没有找到,返回-1。
lastIndexOf(String value)搜索最后一个出现的字符ch(或字符串value),如果没有找到,返回-1。
substring(int index)提取从位置索引开始的字符串部分。
substring(int beginindex, int endindex)提取beginindex和endindex之间的字符串部分,包括开始索引的字符,不包括结束索引的字符。
trim()返回一个前后不含任何空格的调用字符串的副本。
split(String regex)根据拆分规则对字符串进行拆分。
public class StringDemo02 {public static void main(String[] args) {// public int indexOf(int ch)搜索第一个出现的字符ch(或字符串value),如果没有找到,返回-1// public int indexOf(String value)搜索第一个出现的字符ch(或字符串value),如果没有找到,返回-1// 常用ASCII码值 A:65 a:97 0:48String str = "abcdefghijk0lAmnabc";int num = str.indexOf(48);System.out.println(num);// 12int index = str.indexOf("a");System.out.println(index);// 0// public int lastIndexOf(int ch):搜索最后一个出现的字符ch(或字符串value),如果没有找到,返回-1// public int lastIndexOf(String value):搜索最后一个出现的字符ch(或字符串value),如果没有找到,返回-1System.out.println(str.lastIndexOf("a"));// public String substring(int index):提取从位置索引开始的字符串部分String newStr = str.substring(3);System.out.println(str);System.out.println(newStr);// public String substring(int beginindex, int endindex):提取beginindex和endindex之间的字符串部分,包括开始索引的字符,不包括结束索引的字符String newStr2 = str.substring(3, 6);System.out.println(str);System.out.println(newStr2);// public String trim():返回一个前后不含任何空格的调用字符串的副本String str3 = "   abc    qwert    ";String newStr3 = str3.trim();System.out.println(str3);System.out.println(newStr3);System.out.println("-----------------");// String[] split(String regex) :根据拆分规则对字符串进行拆分String song = "长亭外,古道边,芳草碧连天,晚风拂,柳笛声残,夕阳山外山";String[] strs = song.split(",");for (String string : strs) {System.out.println(string);}// 我爱你你不爱我但是我很爱你可我就是不爱你String love = "我爱你你不爱我但是我很爱你可我就是不爱你";String[] loves = love.split("爱");for (String string : loves) {System.out.println(string);}System.out.println("--------------");char ch = love.charAt(1);System.out.println(ch);boolean result = love.endsWith("爱你ya");System.out.println(result);byte[] bytes = love.getBytes();System.out.println(bytes[0]);//-50char result2 = (char) bytes[0];System.out.println(result2);
//		for (int i = 0; i < bytes.length; i++) {
//			System.out.println(bytes[i]);
//		}}
}

四、StringBuffer

public class StringBufferDemo {public static void main(String[] args) {StringBuffer sb = new StringBuffer("qwertyuiop");System.out.println(sb);StringBuffer sb2 = sb.append("asdfg");System.out.println(sb2);//qwertyuiopasdfgSystem.out.println(sb);//qwertyuiopasdfgString str = sb.toString();// 转换成字符串。System.out.println(str);StringBuffer sb3 =sb.insert(3, "你好啊");// 在指定位置添加字符串。System.out.println(sb3);StringBuffer sb4 =sb.replace(5, 8, "爱");// 使用给定 String 中的字符替换此序列的子字符串中的字符。System.out.println(sb4);StringBuffer sb5=	sb.reverse();// 将此字符序列用其反转形式取代。System.out.println(sb5);}
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部