JAVA开发系列之:经常使用类

一:enum枚举数组

       枚举的两种写法:安全

1:普通写法app

public enum week{dom

    one,two,three,four,five,six,seven;工具

}性能

取值方法:week.one;ui

2:高级写法this

public enum week{线程

      one(1),two(2),three(3),four(4),five(5),six(6),seven(7);orm

      private int value;

      public void setValue(int value){

              this.value=value;

     }

     public int getValue{

            return this.value;

     }

     public week(int value){

          this.value=value;

     }

}

取值方法:week.one.getValue();

二:封装类

封装类是指基本数据类型的封装类:
byte--Byte
short--Short
int--Integer
long--Long
float--Float
double--Double
char--Character
经常使用方法:
intValue,valueOf,parseInt

三:math类的经常使用方法

数学类的经常使用方法:
//生成一个介于0-1之间的随机数
double num0 = Math.random();
//四舍五入
double num1 = Math.round(233.23);
//绝对值
double num2 = Math.abs(233.233);
//向上舍入
double num3 = Math.ceil(233.23);
//向下舍入
double num4 = Math.floor(233.23);
 //取大的数
double num5 = Math.max(233.233, 245.268);
 //取小的数
 double num6 = Math.min(233.233, 245.268);
//求屡次方
double num7 = Math.pow(2, 4);
//取平方根
double num8 = Math.sqrt(4);

四:string类的经常使用方法

经常使用方法:
String str = "Hello world";
//求字符串的长度
int leg = str.length();
//比较字符串
boolean bl = str.equals("你好");
//忽略大小写的比较字符串的写法
boolean bl2 = str.equalsIgnoreCase("HELLO WORLD");
//将字符串转成小写
String str1 = str.toLowerCase();
//将字符串转成大写
String str2 = str.toUpperCase();
//字符串拼接返回一个全新的字符串
String str3 = str.concat("你好世界!");
//得到字符串中特定字符的下标
int index = str.indexOf("e");
//得到字符的最后一个的下标
int index1 = str.lastIndexOf("o");
//从输入位置到结束位置截取字符串
String str4 = str.substring(3);
//从输入开始位置到输入结束位置截取字符串
String str5 = str.substring(3, 6);
//返回一个先后不含空字符的字符串
String str6 = str.trim();
//返回一个字符串的数组
String[] str7 = str.split("");
//返回当前下标处的字符
str.charAt(1);

五:StringBuffer与StringBuilder类

经常使用方法:
toString,append,insert
stringBuffer与stringBuilder区别:
stringBuffer线程安全,操做效率低;
stringBuilder非线程安全,操做效率高

六:日期类

经常使用类:
Date,simpleDateFormate,Calendar 
经常使用方法:
//获取时间对象
Date date = new Date();
//将时间对象按照规定的格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String str = sdf.format(date);
//日历对象
Calendar cal = Calendar.getInstance();
//获得年
cal.get(Calendar.YEAR);
//获得月
cal.get(Calendar.MONTH);
//日
cal.get(Calendar.DAY_OF_MONTH);
//星期
cal.get(Calendar.DAY_OF_WEEK);
//时
cal.get(Calendar.HOUR);
//分
cal.get(Calendar.MINUTE);
//秒
cal.get(Calendar.SECOND);
注:日期类的初始化通常比较消耗性能,因此通常写在工具类里面,提供静态方法供调用。
七:Random类

random类经常使用方法: ran.nextInt,ran.nextInt(10)

相关文章
相关标签/搜索