小白学java-基础篇(常用类库)

1、StringBuffer类:
大部分方法与String相同,String类型的内容是不可修改的,StringBuffer 类型的内容可以修改。如果用到频繁修改内容,最好使用StringBuffer 类的方法;
a、public StringBuffer append()字符串连接,String用“+”,StringBuffer用append;
b、public int indexOf(String str)查找指定字符串是否存在;
c、public int indexOf(String str,int fromIndex)从指定位置开始查找指定字符串是否存在;
d、public StringBuffer insert(int offset,String str)在指定位置插入字符串;
e、public StringBuffer reverse()将内容反转保存;
f、public StringBuffer replace(int start,int end,String str)指定内容替换;
!String类替换用的是repalceAll()
g、public int length()返回字符长度;
h、public StringBuffer delete(int start,int end)删除指定范围;
i、public StringBuffer substring(int start)从指定位置开始截取字符串;
j、public StringBuffer substring(int start,int end)截取指定范围的字符串;
k、public String toString()将内容转换为字符串;

2、Runtime类:
Runtime的构造方法时私有化(单例设计),因此要取得实例,只能通过
Runtime run = Runtime.getRuntime()
a、public static Runtime getRuntime()取得Runtime实例;
b、public long freeMemory()返回JVM虚拟机空闲的内存量;
c、public long maxMemory()返回JVM虚拟机最大内存量;
e、public void gc()运行垃圾回收器;
f、public Process exec(String command) throws IOException执行本机命令;

3、国际化语言:
locale类:
a、public locale(String language)根据语言代码构造一个语言环境;
b、public locale(String language,String country)根据语言和国家代码构造一个语言环境;
ResourceBundle类:
a、public static final ResourceBundle getBundle(String baseName)取得ResourceBundle实例,并指定要操作的资源文件名称;
b、public static final ResourceBundle getBundle(String baseName,Locale locale)取得ResourceBundle实例,并指定要操作的资源文件名称和区域;
c、public final String getString(String key)根据key从资源文件中取出对应的value;
!!!如何实现国际化语言待定!!!

4、System类:
a、public static void exit(int status)如果status为非0,表示系统退出;
b、public static void gc()运行垃圾回收器,调用的是Runtime的gc方法;
c、public static long currentTimeMillis()返回以毫秒为单位的当前时间;
d、public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)数组复制操作;
e、public static Properties getProperties()返回当前系统的全部属性;
f、public static String getProperties(String key)根据key值取得属性的具体内容;

5、日期操作类:
Calender类(比较复杂,不推荐):
这个类是抽象类,需要通过子类GergorianCalender子类多态实例化;
全局常量有 YEAR(年)、MONTH(月)、DAY_OF_MONTH(日)、HOUR_OF_DAY(24小时制)、MINUTE(分)、SECOND(秒)、MILLISECOND(毫秒),用get方法获取;
a、public static Calender getInstance()根据默认时区实例化对象;
b、public boolean after(Object when)判断日期是否在指定日期之后;
c、public boolean before(Object when)判断日期是否在指定日期之前;
d、public int get(int field)返回给定日历字段的值,用于获取全局常量;

DateFormat类:
这个类也是抽象类,但是提供了静态方法,可以取得实例;
a、public static final DateFormat getDateInstance()得到默认的对象;
b、public static final DateFormat getDateInstance(int style,Locale aLocale)根据Locale得到对象;
c、public static final DateFormat getDateTimeInstance()得到日期时间对象;
d、public static final DateFormat getDateTimeInstance(int dateStyle,int timeStyle,Locale aLocale)根据Locale得到日期时间对象;

SimpleDateFormat类:
DateFormat格式化后的日期格式比较固定,若要显示自己想要的格式,需要用到DateFormat子类SimpleDateFormat,常用语将String类型变为date类型;
a、public SimpleDateFormat(String pattern)通过指定的模板构造对象;
b、public Date parse(String source) throws ParseException将一个包含日期的字符串变为date类型;
c、public final String format(Date date)将date类型变为字符串。

6、Math类:
Math都是静态方法,直接引用即可。

7、Random类:
a、public boolean nextBoolean()随机生成boolean值;
b、public double nextBooleanDouble()
c、public float nextFloat()
d、public int nextInt()
e、public int nextInt(int n)随机生成给定最大值的int值;
f、public long nextLong()

8、NumberFormat类:
与DateNumber类似,也是抽象类,提供了静态方法,可以直接调用实例化;
a、public static Locale[] getAvailableLocales()返回所有语言环境的数组;
b、public static final NumberFormat getInstance()返回当前默认语言环境的数字格式;
c、public static NumberFormat getInstance(Locale inLocale)返回指定语言环境的数字格式;
d、public static final NumberFormat getCurrencyInstance()返回当前默认环境的货币格式;
e、public static NumberFormat getCurrencyInstance(Locale inLocale)返回指定语言环境的货币格式;

DecimalFormat类与SimpleDateFormat类似,可以用于自定义格式,但需要模板。

9、BigInteger类:
适用于大数字的基本运算;
Bigdecimal类适用于精确计算,详情学习JDK文档。

10、对象克隆:
用于建立对象的副本,与原对象指向不同的内存地址,调用的是Object类中的clone()方法;
要实现对象克隆,必须实现Cloneable接口。
!!!克隆分为浅克隆和深克隆两种,日后需要学习

11、Array类:
a、public static boolean equals(int[] a1,int[] a2)判断两个数组是否相等;
b、public static void fill(int[] a,int val)将内容填空到指定位置;
c、public static void sort(int[] a)数组排序;
d、public static int binarySearch(int[] a1,int key)对数组排序后进行检索;
e、public static String toString(int[] a)输出数组信息。

12、比较器:
Comparable类:返回的是int类型,只有1、0、-1三种结果;
Comparator类与Comparable类相比,区别在于需要传入两个参数,调用的都是comparaTo()方法。

13、正则表达式:
Pattern类主要是进行正则规范,Matcher类主要是执行规范;
要取得Pattern类实例,必须要调用compile()方法
Pattern类:
a、public static Pattern compile(String regex)指定正则表达式规则;
b、public Matcher matcher(CharSequence input)返回Matcher实例;
c、public String[] split(CharSequence input)字符串拆分;

Matcher类:
a、public boolean matchers()执行验证;
b、public String replaceAll(String replacement)字符串替换。
!正则表达式语法日后单独记录

14、定时调度:
Timer类:
每一个Timer对象对应的是一个线程,通常与TimerTask抽象类配合使用;
a、public Timer()创建一个计时器,并启动;
b、public void cancel()终止计时器,并放弃所有已安排的任务,对正在进行的任务没有影响;
c、public int purge()将已取消的任务移除,释放内存;
d、public void schedule(TimerTask task,Date time)安排一个任务在指定时间执行,若超过这个时间,则立即执行;
e、public void schedule(TimerTask task,Date firstTime,long period)安排一个任务在指定时间执行,然后以固定的频率重复执行;
f、public void schedule(TimerTask task,long delay)安排一个任务在一段时间后执行;
g、public void schedule(TimerTask task,long delay,long period)安排一个任务在一段时间后执行,然后以固定的频率重复执行;
h、public void scheduleAtFixedRate(TimerTask task,Date firstTime,long period)安排一个任务在指定时间执行,然后以近似的频率重复执行;
i、public void scheduleAtFixedRate(TimerTask task,long delay,long period)安排一个任务在一段时间后执行,然后以近似的频率重复执行;

TimerTask类:
a、public void cancel()终止任务,若有个正在执行的任务,任务完成后不再执行;
b、public void run()任务所要执行的具体操作,方法未接口Runnable接口中的方法,子类需要覆写该方法;
c、public long schedule ExecutionTime()返回最近一次执行任务的时间,一般用于判断是否有足够的时间来完成当前任务。

猜你喜欢

转载自blog.csdn.net/qq_29070549/article/details/79423843